1

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?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
haz hazzz
  • 103
  • 2
  • 8

1 Answers1

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