我正在使用似乎有冲突的双缓冲和摆动事件。我正在使用 JSlider 并尝试加倍缓冲。它实际上进行了双缓冲绘制,但是双缓冲再次被绘制,我失去了我的形象。我正在使用 JSlider 进行双缓冲绘制,并且事件系统似乎重新绘制了框架(带有 2 个组件,一个图像和滑块)。我该如何以正确的方式做到这一点?我尝试设置一个重绘变量来表示不在组件中重绘,但这不起作用。是否有某种事件开关来停止重新绘制某些组件?我不应该使用双缓冲吗?这是一个代码片段。
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);
}
}
. . .