I'm using both double buffering and Swing Events which seem to conflict. I'm using a JSlider and trying to double buffer. It actually does the double buffering draw, but the double buffering gets painted again and I lose my image. I'm using a JSlider to do the double buffering draw, and the event system seems to re-draw the frame (with 2 components, an image and the slider). How do I do this the right way? I've tried setting a repaint variable to signal not to repaint in the component but this does not work. Is there some sort of event switch to stop repainting of certain components? Should I not use double buffering? Here's a code snippet.
private void drawOneByOne(ImageComponent imgComponent, JFrame f,
MapObjects mapObjects, int number) {
f.createBufferStrategy(2);
BufferStrategy bufferStrategy = f.getBufferStrategy();
Graphics2D g = (Graphics2D)bufferStrategy.getDrawGraphics();
bufferStrategy = f.getBufferStrategy();
g = (Graphics2D)bufferStrategy.getDrawGraphics();
// draw the map and then the points
imgComponent.paint(g);
for (int i = 0; i < number; i++) {
imgComponent.drawPoint(mapObjects.get(i),g);
}
imgComponent.repaint = false;
bufferStrategy.show();
g.dispose();
imgComponent.repaint = true;
}
public void stateChanged(ChangeEvent e) {
JSlider source = (JSlider)e.getSource();
if (!source.getValueIsAdjusting()) {
int voterNumber = source.getValue();
System.out.println("Drawing One By One, " + voterNumber);
drawOneByOne(this.imgComponent, this.f, this.mapObjects, voterNumber);
}
}
. . .