I am creating and updating a BufferedImage on a background thread. The image is then drawn onto a JPanel by overriding the panels paint method as shown.
@Override
public void paint(Graphics g) {
g.drawImage(image, 0, 0, null);
}
The background thread periodically updates the image then calls the panels invalidate method.
Will I run into any issues with this approach? Will the swing thread reading and the background thread updating the image cause any problems?
Thanks
Ben
The two threads must synchronize access to the shared data.
SwingWorkeris the most reliable approach with which to periodically publish theBufferedImage; there’s an example here usingDouble. You might also look at the example here, which relies on the (implicit) ordering afforded by heEventQueueandrepaint(). In either case, the limiting factor is the timer frequency relative to the repaint time.