Hello!
I’m making a platform game engine for a school project, but it isn’t quite fast enough. I had the idea of rendering the whole display to a single image that returns when you call a method in my engine class, and indeed, that is sufficiently fast (from start to end the method takes ~15ms).
The bottleneck seems to be when I actually draw the rendered image to the screen, when the time it takes doubles or even triples. I think that there must be a faster way to put an image on the screen, but I’ve tried and tried to no avail. Here’s how my main code looks (leaving out the window listener code and other stuff that doesn’t seem to be related to the problem):
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("test");
Canvas canvas= new Canvas();
canvas.setSize(640,480);
frame.add(canvas);
frame.pack();
frame.setResizable(false);
frame.setVisible(true);
Graphics frameGraphics=canvas.getGraphics();
Level levelOne= new Level ("level.png","tiles.png","tilecolors.png"); //creates a new instance of my Level class. Just disregard this.
Image background=levelOne.getBackground();
while(true) {
frameGraphics.drawImage(levelOne.getImage(), 0, 0,null); //levelOne.getImage() is the method that returns the current screen Image
}
}
}
Anyone know how to make this faster? I appreciate any hints or help, but please keep in mind that I’m very new to Java and OOP (with a couple of years experience in C).