I wanted to store the current look of my JPanel in order to re-use it as the background for the next iterations of my game loop. I've tried several things that were mentionned on the Internet, but I wanted to do it in a very simple way. I'm currently using this to story the current Image of the JPanel:
if(needImage){
Graphics g = tmp.getGraphics();
g = getGraphics();
needImage = false;
}
and this to draw it:
g.drawImage(tmp, 0, 0, this.getWidth(), this.getHeight(), null);
Any ideas ?
Edit:
I tried this method, found at Can I create a BufferedImage from a JPanel...:
private BufferedImage getScreenShot(JPanel panel){
BufferedImage bi = new BufferedImage(
panel.getWidth(), panel.getHeight(), BufferedImage.TYPE_INT_ARGB);
panel.paint(bi.getGraphics());
return bi;
}
But it doesn't work right. I'm drawing 1 pixel large rectangles in a loop, in order to draw lines when using the move keys, but it seems to only capture the last point with this method.
g.setColor(new Color(hexaR, hexaV, hexaB));
g.fillRect(x, y, 1, 1);
I've taken some screenshots to show you the result: Original:
Original Image
Image after some modification
As you can see, the original image as been deleted, but it remains the final dots of the "cursor" (x and y inputs of the keyboard). Basically, what I want it to do is to stop the drawing of the lines when I press space, but I want to be able to look where the dots I'm moving are.
I don't know if it clear or not, please tell me. I'll try my best to provide clearer explanations.