How can I optimize my 2d java game rendering? - java

My problem is, I have a game with a random generated map and it has only 30-40 fps because of the number of blocks.(You can imagine my game like a 2d minecraft).
First I search for the first tile what is in screen. Then start a loop render tile next tile... until I reach the last tile what you can see.
(I don't use any of the Java classes like graphics/graphics2d I use my own code what is an int[] with the rows of teh screen in it and when I render a tile I change the int[x+y*width] position of the screen to the correct pixel of the block)
I think logically this is the best way to render my map and i don't understend why is the low fps. I am wrong or I need to search for some other problem in my code? Or there is any better rendering method?
If I skip the rendering of the world, there is stabile 120 fps what is capped there. What can be the problem?

I know you dont use the Gaphics functions and choose to manipulate a pixel-array instead.
Try to change your game to use the Graphics object since there is no (easy and efficient) way around it. If you still choose not to do so, try to add
System.setProperty("sun.java2d.opengl", "true");
at the very begining of your code just after
public static void main(String[] args) {
I tried to do it the same way as you back when i first made simple games but you later come to realize that the built-in Graphics functions are vastly superior in performance and ease of use.
EDIT:
A short explanaition on how a basic game might use the Graphics object:
Suppose you created a JFrame. If you then add a class that extends from Canvas and add that to it. If you want to use a menu, you might even create a JPanel first and add the Canvas into the Jpanel so you can hide it more easily.
Here you have an example how we create a usable canvas:
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
public class Game extends Canvas implements Runnable{
private static final long serialVersionUID = 1L;
public static final int WIDTH = 1920;
public static final int HEIGHT = WIDTH * 9 / 16;
public static final String TITLE = "YOUR GAMES NAME";
public static final int TICKSPERS = 120;
public static final boolean ISFRAMECAPPED = false;
public static JFrame frame;
private Thread thread;
private boolean running = false;
public int frames;
public int lastFrames;
public int ticks;
public Game(){
Dimension size = new Dimension(WIDTH, HEIGHT);
setPreferredSize(size);
setMaximumSize(size);
setMinimumSize(size);
}
public void render(){
frames++;
BufferStrategy bs = getBufferStrategy();
if (bs == null){
createBufferStrategy(2);
return;
}
Graphics g = bs.getDrawGraphics();
g.setColor(new Color(79,194,232));
g.fillRect(0, 0, getWidth(), getHeight());
//Call your render funtions from here
g.setColor(Color.BLACK);
g.fillRect(120,70,35,90);
g.dispose();
bs.show();
}
public void tick(){
}
public synchronized void start(){
if(running) return;
running = true;
thread = new Thread(this, "Thread");
thread.start();
}
public synchronized void stop(){
if(!running) return;
running = false;
try {
System.exit(1);
frame.dispose();
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void init(){
}
public void run() {
init();
//Tick counter variable
long lastTime = System.nanoTime();
//Nanoseconds per Tick
double nsPerTick = 1000000000D/TICKSPERS;
frames = 0;
ticks = 0;
long fpsTimer = System.currentTimeMillis();
double delta = 0;
boolean shouldRender;
while(running){
shouldRender = !ISFRAMECAPPED;
long now = System.nanoTime();
delta += (now - lastTime) / nsPerTick;
lastTime = now;
//if it should tick it does this
while(delta >= 1 ){
ticks++;
tick();
delta -= 1;
shouldRender = true;
}
if (shouldRender){
render();
}
if (fpsTimer < System.currentTimeMillis() - 1000){
System.out.println(ticks +" ticks, "+ frames+ " frames");
ticks = 0;
lastFrames = frames;
frames = 0;
fpsTimer = System.currentTimeMillis();
}
}
}
public static void main(String[] args){
Game game = new Game();
frame = new JFrame(TITLE);
frame.add(game);
frame.pack();
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
game.start();
}
}
This class can be your base. Its tick method is called 120 times a second and its render method can render stuff onto the screen.
If you are not familiar with the functions of the Graphics Object i suggest reading a little about them.
You can't reach the Graphics object from the outside. You need to call the render functions from inside the games render function before the Graphics object gets disposed. Try to seperate your game logic from the render functions.

I use a slightly different architecture than what's stated above. I actually create a separate Renderer object which is basically a deferred renderer.
It's structured like this
public class App {
JFrame window;
Renderer renderer;
Engine engine; //implementation is a nested class within App
Dimension window_dimension; //stored for later use
public App() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
window = new JFrame("MyGame");
boolean full_screen = true;
window_dimension = initializeWindow(window, full_screen);
renderer = new Renderer(window_dimension);
window.add(renderer);
engine = new Engine(renderer);
engine.start();
}
});
}
}
Renderer.java :
import java.awt.*;
import java.util.concurrent.CopyOnWriteArrayList;
public class Renderer extends JPanel {
Dimension dim;
private CopyOnWriteArrayList<Drawable> drawables = new CopyOnWriteArrayList<Drawable>();
Renderer(Dimension dim) {
this.dim = dim;
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.clearRect(0, 0, getWidth(), getHeight());
for (Drawable drawable : drawables) {
drawable.paint(g2d);
}
g2d.dispose();
drawables.clear();
}
public synchronized void render() {
repaint();
}
public synchronized void submit(Drawable drawable) {
drawables.add(drawable);
}
public synchronized void submitBackground(Drawable drawable) {
drawables.add(0,drawable);
}
}
Drawable.java :
import java.awt.*;
abstract class Drawable {
protected Stroke stroke;
protected Color color, stroke_color;
public Dimension size;
public float sub_pixel_x;
public float sub_pixel_y;
public Drawable(Color color) {
setColor(color);
setStrokeColor(new Color(0));
sub_pixel_x = 0.0f;
sub_pixel_y = 0.0f;
size = new Dimension(10, 10);
}
public void setStroke(float width) {
stroke = new BasicStroke(width);
}
public void noStroke() {
stroke = null;
}
public void setColor(Color color) {
this.color = color;
}
public void setStrokeColor(Color color) {
this.stroke_color = color;
}
public void setLocation(float x, float y) {
sub_pixel_x = x;
sub_pixel_y = y;
}
protected abstract void paint(Graphics2D g2d);
}
AbstractEngine.java :
import java.awt.*;
abstract class AbstractEngine implements Runnable {
Renderer renderer;
Dimension dimension;
boolean running;
Thread thread;
public AbstractEngine(Renderer renderer) {
this.renderer = renderer;
dimension = renderer.dim;
}
public void start() {
if (running) return;
running = true;
thread = new Thread(this, "Tread");
thread.start();
}
public void stop() {
if(!running) return;
running = false;
try {
System.exit(1);
thread.join();
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
public abstract void handleInput();
public abstract void update();
public abstract void render();
#Override
public void run() {
final int UPS = 120;
final int FPS = 60;
long initialTime = System.nanoTime();
final double timeU = 1000000000 / UPS;
final double timeF = 1000000000 / FPS;
double deltaU = 0, deltaF = 0;
int frames = 0, ticks = 0;
long timer = System.currentTimeMillis();
while (running) {
long currentTime = System.nanoTime();
deltaU += (currentTime - initialTime) / timeU;
deltaF += (currentTime - initialTime) / timeF;
initialTime = currentTime;
if (deltaU >= 1) {
handleInput();
update();
ticks++;
deltaU--;
}
if (deltaF >= 1) {
render();
renderer.render();
frames++;
deltaF--;
}
if (System.currentTimeMillis() - timer > 1000) {
frames = 0;
ticks = 0;
timer += 1000;
}
}
}
}
Input is handled in the extended Engine class. I have a MouseState object that holds mouse positions and button state. I then create a MouseAdapter that updates the date in MouseState and attach it to the renderer. It's a bit weird, I know, but works nicely.

Related

First Java Game Not Rendering

I decided I wanted to experiment with making a game and I like Java, so I started following a tutorial here. I did deviate from the video a few times when I felt it was cleaner while being synonymous with the tutorial's code, but not in any way I thought would affect how the code worked. For example, it made sense to me that there should only ever be one instance of the Renderer, and Object Registry so I made them Singletons.
The code I have so far is supposed to create a window with a black background, and a blue square in the middle of the window representing the player, that much is working. However, it should also be sliding around in response to the wasd keys, and even more slowly drifting in one direction regardless. Instead it's doing nothing.
I spent no less than an hour trying to figure out why it wasn't working. It seems to be ticking just fine and the data looks like it's updating properly, but even though my render methods are also being called, the screen just isn't changing.
This is all the code I have so far, since I'm out of ideas as to the problem. I apologize for listing a whole project.
public class Game implements Runnable {
private final Thread thread;
private boolean running = false;
Game() {
thread = new Thread(this);
}
public static void main(String[] args) {
Game game = new Game();
game.start();
new Player(600,450,0));
}
private void start() {
thread.start();
running = true;
}
#Override
public void run() {
double tps = 10.0;
double nsPerTick = 1000000000 / tps;
double delta = 0;
int frames = 0;
long timer = System.currentTimeMillis();
long lTime = System.nanoTime();
long now;
while (running) {
now = System.nanoTime();
delta += (now - lTime) / nsPerTick;
lTime = now;
while (delta >= 1) {
Registry.getInstance().tick();
delta--;
}
if (running) Renderer.getInstance().run();
frames++;
if (System.currentTimeMillis() - timer > 1000) {
timer += 1000;
System.out.println("FPS: " + frames);
frames = 0;
}
}
stop();
}
private void stop() {
try {
thread.join();
} catch (Exception e) {
e.printStackTrace();
}
running = false;
}
}
public class Renderer extends Canvas {
private static final Renderer renderer = new Renderer();
private final Window window;
private final BufferStrategy bs;
private final Graphics g;
boolean black = true;
private Renderer() {
window = new Window(1200, 900, "First Game", this);
this.createBufferStrategy(2);
bs = this.getBufferStrategy();
g = bs.getDrawGraphics();
addKeyListener(Controller.getInstance());
}
public static Renderer getInstance() {return renderer;}
public void run() {
g.setColor(Color.BLACK);
//this was to see if even the background would update, it wouldn't
//g.setColor(black ? Color.BLACK : Color.WHITE);
//black = !black;
g.fillRect(0,0,1200, 900);
Registry.getInstance().render();
g.dispose();
bs.show();
}
public Graphics getGraphics() {return g;}
private static class Window extends Canvas {
private Window(int width, int height, String title, Renderer renderer) {
JFrame frame = new JFrame(title);
frame.setPreferredSize(new Dimension(width, height));
frame.setMinimumSize(new Dimension(width, height));
frame.setMaximumSize(new Dimension(width, height));
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.add(renderer);
frame.setVisible(true);
}
}
}
public class Registry {
private static final Registry reg = new Registry();
private final LinkedList<GameObject> objects = new LinkedList<>();
private Registry() {
}
public static Registry getInstance() { return reg; }
public void tick() { //System.out.println("tick");
objects.forEach(GameObject::tick); }
public void render() { objects.forEach(GameObject::render); }
public void add(GameObject gameObject) { objects.add(gameObject); }
public void remove(GameObject gameObject) { objects.remove(gameObject);}
}
public class Controller extends KeyAdapter {
private static final Controller controller = new Controller();
private final HashMap<Character,Boolean> keyStates = new HashMap<>();
private Controller() {
}
public static Controller getInstance() {
return controller;
}
public void keyPressed(KeyEvent e) {
if (!keyStates.getOrDefault(e.getKeyChar(), true)) System.out.println(e.getKeyChar() + " down");
keyStates.put(e.getKeyChar(),true);
}
public void keyReleased(KeyEvent e) {
keyStates.put(e.getKeyChar(),false);
System.out.println(e.getKeyChar() + " up " + keyStates.size());
}
public boolean isKeyDown(char c) {return keyStates.getOrDefault(c,false);}
}
public abstract class GameObject {
protected Graphics graphics = Renderer.getInstance().getGraphics();
protected final ObjectType type;
protected float x,y,r;
protected GameObject(ObjectType objectType, float x, float y, float r) {
this.type = objectType;
this.x = x;
this.y = y;
this.r = r;
Registry.getInstance().add(this);
}
public abstract void tick();
public abstract void render();
public void destroy() { Registry.getInstance().remove(this); }
public float getX() { return x; }
public void setX(float x) { this.x = x; }
public float getY() { return y; }
public void setY(float y) { this.y = y; }
public float getR() { return r; }
public void setR(float r) { this.r = r; }
}
public class Player extends GameObject {
private final Controller controller;
public Player(float x, float y, float r) {
super(ObjectType.PLAYER, x, y, r);
controller = Controller.getInstance();
}
#Override
public void tick() {
this.x += 1;
if (controller.isKeyDown('w')) x += 2;
if (controller.isKeyDown('a')) y -= 2;
if (controller.isKeyDown('s')) x -= 2;
if (controller.isKeyDown('d')) y += 2;
}
#Override
public void render() {
graphics.setColor(Color.BLUE);
graphics.fillRect((int) (this.x-12),(int) (this.y-12), 24,24);
}
}
The problem lies in your handling of Graphics. There's only ONE active Graphics object you can effectively address.
Refactor your code, so that you pass the current Graphics object via parameter through the target methods (like here: Player.render() should become Player.render(Gpahics g). And get rid of the Gameobject.graphics member variable. That is the culprit.
Adding to that, the best way to do simple rendering is to override the paint(Graphics g) method or the paintComponents(Graphics g), and from outside call repaint() on the JPanel/Canvas. This way the UI instigates drawing itself, takes care of the actual frequency, and draws default components/design too if there is some.

Why is my eclipse not setting my background color?

I have designed a code for a game. The problem is that the background won't change to any of my selected colors I have picked from graphics color library.
I need someone to figure this out with the code i have provided (please don't make a whole new code). idk why java/ eclipse won't display it??? am i missing something?? The program here should display a GUI with a background color blue. instead i get white.
public class MainApp extends Canvas implements Runnable {
private static final long serialVersionUID = 8928635543572788908L;
private static final int WIDTH= 648, HEIGHT= WIDTH/ 12 * 9;
private Thread thread;
private boolean running= false;
public MainApp()
{
new Window(WIDTH, HEIGHT, "App", this);
}
public synchronized void start()
{
thread= new Thread(this);
thread.start();
running= true;
}
public void run()
{
long lastTime= System.nanoTime();
double amountOfTicks= 60.0;
double ns= 1000000000 / amountOfTicks;
double delta= 0;
long timer= System.currentTimeMillis();
int frames= 0;
while(running){
long now= System.nanoTime();
delta += (now- lastTime) / ns;
lastTime= now;
while(delta >= 1){
tick();
delta--;
}
if(running)
render();
frames++;
if(System.currentTimeMillis() - timer > 1000)
{
timer += 1000;
System.out.print("FPS: " + frames);
frames= 0;
}
}
stop();
}
public synchronized void stop()
{
try
{
thread.join();
running= false;
}catch(Exception e){e.printStackTrace();}
}
public void tick()
{
}
public void render()
{
BufferStrategy bs= this.getBufferStrategy();
if(bs== null)
{
this.createBufferStrategy(3);
return;
}
Graphics g= bs.getDrawGraphics();
g.setColor(Color.BLUE);
g.fillRect(0, 0, WIDTH, HEIGHT);
g.dispose();
bs.show();
}
public static void main(String[] args) {
new MainApp();
}
}
Your codes a little messed up, you shouldn't be making a new instance of Window from MainApp, the Window should be creating it (IMHO).
Also, you should be overriding the getPreferredSize method the the MainApp, as this is what should be controlling the viewable size of the window, this way, when you use pack on the JFrame, it will ensure that the window is larger then the preferredSize of it's contents, allowing the frame decorations to wrap around the outside of it.
BUT, the main problem you have, is adding the MainApp to the JFrame AFTER it's already been made visible
The following works for me...
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
public class MainApp extends Canvas implements Runnable {
private static final long serialVersionUID = 8928635543572788908L;
private static final int WIDTH = 648, HEIGHT = WIDTH / 12 * 9;
private Thread thread;
private boolean running = false;
public MainApp() {
new Window("App", this);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(WIDTH, HEIGHT);
}
public synchronized void start() {
thread = new Thread(this);
thread.start();
running = true;
}
public void run() {
long lastTime = System.nanoTime();
double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
long timer = System.currentTimeMillis();
int frames = 0;
while (running) {
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
while (delta >= 1) {
tick();
delta--;
}
if (running) {
render();
}
frames++;
if (System.currentTimeMillis() - timer > 1000) {
timer += 1000;
System.out.print("FPS: " + frames);
frames = 0;
}
}
stop();
}
public synchronized void stop() {
try {
thread.join();
running = false;
} catch (Exception e) {
e.printStackTrace();
}
}
public void tick() {
}
public void render() {
BufferStrategy bs = this.getBufferStrategy();
if (bs == null) {
this.createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.setColor(Color.BLUE);
g.fillRect(0, 0, WIDTH, HEIGHT);
g.dispose();
bs.show();
}
public static void main(String[] args) {
new MainApp();
}
public static class Window {
private Window(String title, MainApp app) {
JFrame frame = new JFrame(title);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(app);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
app.start();
}
}
}

Mouse click coordinates are always 0

As you can see I added the a mouse listener to the game.
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
public class Game implements Runnable
{
private Display display;
public int width, height;
public String title;
private boolean running = false;
private Thread thread;
private BufferStrategy bs;
private Graphics g;
// States
public static State gameState;
// Input
private InputManager inputManager;
private MouseHandler mouseHandler;
public Game(String title, int width, int height)
{
this.width = width;
this.height = height;
this.title = title;
inputManager = new InputManager();
mouseHandler = new MouseHandler();
}
/**
* Initializes all of the graphics
*/
private void initialize()
{
display = new Display(title, width, height);
display.getFrame().addKeyListener(inputManager);
display.getFrame().addMouseListener(mouseHandler);
Assets.loadAssets();
gameState = new GameState(this, 1);
State.setState(gameState);
}
/**
* Updates everything in the game loop
*/
private void update()
{
if (State.getState() != null)
State.getState().update();
}
private void render()
{
bs = display.getCanvas().getBufferStrategy();
// If this is the first time running initialize the buffer strategy
if (bs == null)
{
display.getCanvas().createBufferStrategy(3);
return;
}
g = bs.getDrawGraphics();
// Clear the screen
g.clearRect(0, 0, width, height);
// Drawing
if (State.getState() != null)
State.getState().render(g);
// End drawing
bs.show();
g.dispose();
}
public void run()
{
initialize();
// Updates the game loop 60 times every 1 second = 1,000,000,000
// nanoseconds
int fps = 60;
double timePerUpdate = 1000000000 / fps;
double timeElapsed = 0;
long now;
// Current time of computer in nanoseconds
long lastTime = System.nanoTime();
// Game loop
while (running)
{
now = System.nanoTime();
timeElapsed += (now - lastTime) / timePerUpdate;
lastTime = now;
if (timeElapsed >= 1)
{
update();
render();
timeElapsed--;
}
}
stop();
}
public synchronized void start()
{
// Do not make a new thread if it is already running
if (running)
return;
// Starts the game
running = true;
thread = new Thread(this); // this = Game class
thread.start(); // Goes to run
}
public synchronized void stop()
{
// In case stop gets called and it is already not running
if (!running)
return;
// Stops the game
running = false;
try
{
thread.join();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
public InputManager getInputManager()
{
return inputManager;
}
public MouseHandler getMouseHandler()
{
return mouseHandler;
}
public static void main(String[] args)
{
Game game = new Game("Game", 1024, 768);
game.start();
}
}
This is my mouse adapter class, basically all I want is the x,y coordinates of where the mouse is pressed
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class MouseHandler extends MouseAdapter
{
public int x, y;
public MouseHandler()
{
}
public void MousePressed(MouseEvent e)
{
x = e.getX();
y = e.getY();
}
}
When I try to get the X, and Y coordinates they are always 0 instead of where the mouse actually clicked. I have no clue why.
public int x, y;
int variables are always initialized to 0.
public void MousePressed(MouseEvent e)
Method names are case sensitive.
Your code is never executed since the method to override should be mousePressed(...). Fix the typo in your code.
Always use code like:
#Override
public void mousePressed(MouseEvent e)
Then if you make a typo the compiler will tell you.
When I try to get the X, and Y coordinates they are always 0
Since your code is never executed the default value is returned.

Java Fullscreen Image resizing?

Hello I am kind of new to java. I am working on a game. when the game starts a loading screen appears and the fades away that's it so far. My question is simple. Is there a way to change the image size to fit the size of the computer screen. I want it to fit all types of screen sizes and be a larger scale of the original image. Here is my Main class so far(there are other classes like player and title and play but they are irrelevant to the question:
package Main;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
public class Comp extends Canvas implements Runnable, KeyListener {
private static final long serialVersionUID = 1L;
public boolean run = true;
public Image screen, img;
public static double dir = 5;
public static int pixelSize = 1;
public static Dimension size = new Dimension(680, 500);
public static Dimension pixel = new Dimension(size.width / pixelSize,
size.height / pixelSize);
public static int mx;
public static manager m;
public static int my;
public static int fps;
public static boolean mr;
public static boolean ml;
JFrame frame = new JFrame();
public Thread t;
public boolean splashscreen = true;
public int time = 0;
public int timer = 140;
public int btime = 0;
public int btimer = 1;
private int FPS = 5;
private long targetTime = 1000 / FPS;
public Comp() {
setPreferredSize(size);
setFocusable(true);
addKeyListener(this);
addKeyListener(new listen());
addMouseListener(new listen());
addMouseMotionListener(new listen());
addMouseWheelListener(new listen());
m = new manager();
requestFocus();
// h = new InputHandler(this);
}
public void start() {
run = true;
frame.add(this);
frame.pack();
frame.requestFocus();
frame.setTitle("In The Maze");
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setIconImage(new ImageIcon("res/traps/beartrap1.png").getImage());
t = new Thread(this);
t.start();
}
public void stop() {
run = false;
}
public static void main(String[] args) {
Comp c = new Comp();
c.start();
}
public void render() {
BufferStrategy bs = this.getBufferStrategy();
if (bs == null) {
createBufferStrategy(2);
return;
}
Graphics g = bs.getDrawGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, 1000, 1000);
if (!splashscreen) {
m.render(g);
}
if(splashscreen){
ImageIcon i62 = new ImageIcon("res/splashscreen.png");
img = i62.getImage();
g.drawImage(img,(int)0 ,(int)0,Comp.size.width+10,Comp.size.height + 10,null);
time++;
}
g.dispose();
bs.show();
}
public void tick() {
if(time >= timer){
time = 0;
splashscreen = false;
}
if(!splashscreen){
m.tick();
}
Keys.update();
Esentials.tick();
}
public void run() {
screen = createVolatileImage(pixel.width, pixel.height);
long start;
long elapsed;
long wait;
long currentTime = System.currentTimeMillis();
while (run) {
start = System.nanoTime();
fps++;
if(System.currentTimeMillis() - currentTime >= 1000){
// System.out.println("fps:" + fps);
currentTime = System.currentTimeMillis();
fps = 0;
}
tick();
render();
elapsed = System.nanoTime() -start;
wait = targetTime = elapsed / 1000000;
try {
Thread.sleep(wait);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void keyTyped(KeyEvent i) {
}
public void keyPressed(KeyEvent i) {
Keys.keySet(i.getKeyCode(), true);
}
public void keyReleased(KeyEvent i) {
Keys.keySet(i.getKeyCode(), false);
}
}
Try to use Toolkit.getScreenSize()
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
double width = screenSize.getWidth();
double height = screenSize.getHeight();
For resizing the image you can use simple imgscalr library
BufferedImage scaledImage = Scalr.resize(myImage, 100);
Determining the screen size...
How to set present screensize in Java Swing?
Full-Screen Exclusive Mode API
Scaling the image...
Java: maintaining aspect ratio of JPanel background image
Quality of Image after resize very low -- Java
The Perils of Image.getScaledInstance()
I'd also discourage the use KeyListener and encourage the use of the Key Bindings API. See How to use key bindings for more details
Using the default Graphics object from the java.awt package, you can use the drawImage method with 10 parameters:
int destinationWidth = ...;
int destinationHeight = ...;
g.drawImage(originalImage, 0, 0, destinationWidth, destinationHeight, 0, 0, originalImage.getWidth(null), originalImage.getHeight(null), null);
That way your image will be scaled and painted to g. Note that simply by using as in the example provided will stretch and deform your image if the aspect ratio of your window is different from the aspect ratio of the image.

Graphics will not cover entire canvas

What I know: Canvas is displayed properly, due to the yellow. However, graphics will not cover the canvas completely, nor will my law class recognized it past the end of the black area...
So what is causing this to happen? And how could I draw on my currently undrawable section of canvas(yellow part), or should I just implement my graphics another way?
EDIT: The UI class creates a canvas and a buffer, then the graphics class takes over and starts drawing on them, however for some reason it cannot in the yellow section, nor will the Law Class which handles collision with the red cube and walls of the app, regogize the yellow area as a valid place to go. Even through the same variables for dimensions, were used everywhere.
Main Class
package app;
public class Main {
static final int X = 1024;
static final int Y = 680;
static final int sanic = 10;
int fps = 0;
int frames = 0;
long totalTime = 0;
long curTime = System.currentTimeMillis();
long lastTime = curTime;
static int[] pos;
Graphics graphics;
Law physics;
static int status;
boolean holdState;
public Main() {
pos = new int[5];
pos[1] = X;
pos[2] = Y;
}
public void launch() {
// Audio sound = new Audio();
graphics = new Graphics();
physics = new Law();
graphics.draw();
// sound.play();
handle();
}
public void update() {
graphics.cache();
physics.check();
graphics.render();
try {
Thread.sleep(10);
} catch (Exception e) {
} finally {
graphics.dump();
}
}
public void handle() {
while (!isOver()) {
if (!isPaused()) {
update();
}
}
}
boolean isOver() {
if (status == 1) {
status = 0;
return true;
}
return false;
}
boolean isPaused() {
if (status == 2) {
status = 0;
if (!holdState) {
holdState = true;
pos[3] = pos[1];
pos[4] = pos[2];
} else if (holdState) {
holdState = false;
pos[1] = pos[3];
pos[2] = pos[4];
}
}
return holdState;
}
public static void main(String[] args) {
Main game = new Main();
game.launch();
}
}
UI Class
package app;
import java.awt.*;
import java.awt.image.*;
import java.net.URL;
import javax.swing.*;
public class UI extends Main {
JFrame mainWin;
Canvas wall;
URL pic;
Toolkit kit;
Image img;
BufferStrategy Buffer;
Graphics2D shell;
Graphics2D ball;
public UI() {
mainWin = new JFrame("Game");
wall = new Canvas();
wall.addKeyListener(new Input());
}
void draw() {
frame();
icon();
canvas();
show();
prep();
}
void frame() {
mainWin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainWin.setBackground(Color.BLUE);
mainWin.setIgnoreRepaint(true);
}
void icon() {
pic = ClassLoader.getSystemResource("res/app.png");
kit = Toolkit.getDefaultToolkit();
img = kit.createImage(pic);
mainWin.setIconImage(img);
}
void canvas() {
wall.setBackground(Color.YELLOW);
wall.setIgnoreRepaint(true);
wall.setBounds(0, 0, X, Y);
}
void show() {
mainWin.add(wall);
mainWin.pack();
mainWin.setResizable(false);
mainWin.setLocationRelativeTo(null);
mainWin.setVisible(true);
}
void prep() {
wall.createBufferStrategy(2);
Buffer = wall.getBufferStrategy();
}
}
Graphics Class
package app;
import java.awt.*;
public class Graphics extends UI {
public void render() {
mask();
player();
fps();
Buffer.show();
}
void cache() {
shell = (Graphics2D) Buffer.getDrawGraphics();
ball = (Graphics2D) Buffer.getDrawGraphics();
}
void dump() {
shell.dispose();
ball.dispose();
}
void mask() {
shell.setColor(Color.black);
shell.fillRect(0, 0, X, Y);
}
void fps() {
lastTime = curTime;
curTime = System.currentTimeMillis();
totalTime += curTime - lastTime;
if (totalTime > 1000) {
totalTime -= 1000;
fps = frames;
frames = 0;
}
frames++;
shell.setColor(Color.GREEN);
shell.setFont(new Font("Courier New", Font.PLAIN, 12));
shell.drawString(String.format("FPS: %s", fps), 20, 20);
}
void player() {
ball.setColor(Color.RED);
ball.fillRect(pos[1], pos[2], 32, 32);
}
}
Law Class
package app;
public class Law extends Main {
public void check() {
out();
}
void out() {
if (pos[1] < 0) {
pos[1] = 0;
}
if (pos[2] < 0) {
pos[2] = 0;
}
if (pos[1] > X - 32) {
pos[1] = X - 32;
}
if (pos[2] > Y - 32) {
pos[2] = Y - 32;
}
}
}
This is a bug with Frame#setResizable. Don't ask me why, but it wants to add about 10 pixels to the width and height of the frame.
The best solution I know is to call setResizable BEFORE pack
void show() {
mainWin.add(wall);
mainWin.setResizable(false); // Call me first
mainWin.pack();
mainWin.setLocationRelativeTo(null);
mainWin.setVisible(true);
}
You're not interacting with Swing properly, and you're doing unsafe things with a default Graphics object you should not be touching. Here's the bare bones of what needs to happen, in order.
JFrame mainFrame = new JFrame()
MyCanvas myCanvas = new MyCanvas()
mainFrame.getContentPane().add(myCanvas, BorderLayout.CENTER)
mainFrame.setVisible(true);
new Thread(myThread).start()
Main.main() returns.
MyCanvas needs to look something like this:
public class MyCanvas extends Canvas {
public void paint(Graphics g) {
// drawing code goes here
} // g is now no longer valid. Don't hold onto it or dispose it or anything.
}
Here's the physics update thread:
public class MyThread implements Runnable {
public void run() {
while (keepRunning()) {
physics.update();
myCanvas.repaint(); // myCanvas.paint() will eventually be called
sleep(10);
}
System.exit(0);
}
}
You probably want to add constructors to these objects and pass in references to your other objects they need to reference.

Categories