0

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);
    }
}

. . .

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72

1 Answers1

0

Swing is easily doublebuffered via setDoubleBuffered(true) if that is all you wanted to achieve. There is nothing wrong with double-buffering, it just uses more memory, call it on the parentcontainer of your swingcomponents. You have no control over the repaint, the OS calls it whenever it deems necessary unless you call setIgnoreRepaint on the JFrame.

arynaq
  • 6,710
  • 9
  • 44
  • 74