A Java Canvas is just a place on the screen to display graphics, but it never remembers those graphics, so for example, if you painted something onto it, and then drag another window over it, it would be wiped clean until the next time you paint onto it (either by its overridden paintComponent, or by the BufferStrategy). The canvas can never give you the graphics that are displayed on it.
I don’t think you are overriding the paint method as you say you are. that’s not how a bufferstrategy works.
Because you are using a BufferStrategy insted of overriding the canvas paintComponent() method, if you want to get what your canvas is displaying into an image, you have to do the same rendering to the image graphics as you do to the BufferStragegy graphics.
In this example, where a simple rendering loop is used:
private void renderLoop() {
while(running) {
do {
do {
Graphics2D g = (Graphics2D)bufferStrategy.getDrawGraphics();
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g.setColor(Color.white);
g.fillRect(0, 0, canvasWidth, canvasHeight);
g.drawImage(image1, 0, 0, null);
g.drawImage(image2, 30, 50, null);
g.dispose();
} while (bufferStrategy.contentsRestored());
bufferStrategy.show();
} while(bufferStrategy.contentsLost());
}
}
If you want to be able to render it onto an image whenever you like, you could replace it with:
private void renderLoop() {
while(running) {
do {
do {
Graphics2D g = (Graphics2D)bufferStrategy.getDrawGraphics();
renderSingleFrame(g);
g.dispose();
} while (bufferStrategy.contentsRestored());
bufferStrategy.show();
} while(bufferStrategy.contentsLost());
}
}
private void takeScreenShot(BufferedImage into) {
Graphics2D g = (Graphics2D)into.getGraphics();
renderSingleFrame(g);
g.dispose();
}
private void renderSingleFrame(Graphics2D g) {
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g.setColor(Color.white);
g.fillRect(0, 0, canvasWidth, canvasHeight);
g.drawImage(image1, 0, 0, null);
g.drawImage(image2, 30, 50, null);
}