My program draws numerous rectangles in a JPanel; the location, dimensions and colors are determined by a random number generator. I want to save the panel contents as a jpg. I believe this requires converting the panel contents to a buffered image using something like this:
BufferedImage awtImage = new BufferedImage(canvasPanelWidth,canvasPanelHeight,BufferedImage.TYPE_INT_RGB); Graphics2D g2 = awtImage.createGraphics(); canvasPanel.print(g2);
This apparently causes my paintComponent method to execute, which generates a new set of randome numbers and a completely new set of rectangles and associated jpg file.
Is there another way to save the JPanel as a jpg that will not cause a repaint or do I have to generate the rectangles directly in a buffered image and display in a JLabel as an imageIcon?
Thanks for any suggestions.