Normally an applet will call different methods that will have a Graphics2D-object that ends on the screen.
In the process the methods will manipulate and draw things on this object.
But my problem is, that I have some very static images that needs to be calculated and drawn for each frame.
How can I build an Graphics-object that I can cache, and re-apply the moving objects? For an example I have this static background drawer, the Graphics2D-object is the one from the Applet.
private Graphics2D drawbackground(Graphics2D g2d) {
// Debug grid layer
int x = 0;
int y = 0;
for (int i = 0; i < 9; i++) {
if (x == 0 && y == 0 || y % 82 == 0) {
x = 0;
for (int t = 0; t <= 5; t++) {
g2d.setColor(Color.WHITE);
g2d.drawLine(x, y + 41, x + 41, y);
g2d.drawLine(x + 41, y, x + 82, y + 41);
g2d.drawLine(x, y + 41, x + 41, y + 82);
g2d.drawLine(x + 82, y + 41, x + 41, y + 82);
x += 82;
}
y += 41;
} else if (y % 41 == 0) {
x = 41;
for (int t = 0; t <= 5; t++) {
g2d.setColor(Color.WHITE);
g2d.drawLine(x, y + 41, x + 41, y);
g2d.drawLine(x + 41, y, x + 82, y + 41);
g2d.drawLine(x, y + 41, x + 41, y + 82);
g2d.drawLine(x + 82, y + 41, x + 41, y + 82);
x += 82;
}
y += 41;
}
}
}
Is it possible to generate these drawings, and re-use them in a cached copy, so my Applet doesn’t need to use time to run through these drawLines for each draw?
I agree with Raveline, BufferedImage is where I would go for this solution. However Double buffering is the concept that you need to get your head around before you go too deep into this.
http://docs.oracle.com/javase/tutorial/extra/fullscreen/doublebuf.html