Currently I have a JPanel
with its paintComponent
overridden with lots of image processing based on various states. When an event occurs (infrequent) many of the states change and the images that are drawn change. It doesn't seem the best way to keep doing all the processing every time the paintComponent
is it possible to do something like when an event occurs draw everything to a Graphics2D
instance and then merge this with the paintComponent
one? Is this also the best way to do it?
Asked
Active
Viewed 48 times
1

Andrew Thompson
- 168,117
- 40
- 217
- 433

haz hazzz
- 103
- 2
- 8
-
3Paint it to a `BufferedImage`, then paint the `BufferedImage` within the `paintComponent` method – MadProgrammer Apr 26 '15 at 11:45
-
Some examples [here](http://stackoverflow.com/a/21266240/2891664). – Radiodef Apr 26 '15 at 12:02
1 Answers
0
As MadProgrammer suggested, storing rendered output can help you.
When the event that might change the image occurs, you can draw stuff to a BufferedImage
like following.
private BufferedImage renderedImage;
public void triggerEvent(int width, int height) {
this.renderedImage = new BufferedImage(width, height, TYPE_INT_ARGB);
Graphics2D g = this.renderedImage.createGraphics();
// Paint things on g.
}
@Override
public void paintComponent(Graphics g) {
g.drawImage(this.renderedImage, 0, 0, this);
}
Hope this helps.

Tanmay Patil
- 6,882
- 2
- 25
- 45