我有一个带JScrollPane排序的JPanel,问题是当我使用JScrollPane时,JPanels重绘方法会被调用。我想禁用它,因为我的JPanel会在正确的时间自动重绘。
我想要它,所以它只是更新画图方法的getClipBounds(),而不是调用画图方法。
发布于 2017-09-14 19:05:16
您不能这样做-因为视区显示所包含的JPanel的不同部分,取决于滚动条的位置,必须重新绘制的区域实际上可能是新显示的,并且可能以前没有绘制过。
由于JScrollPane不知道包含的Component是如何实现的,也不知道它是重绘整个区域还是只重绘需要重绘的区域,因此它会强制包含的Component在滚动时重绘自己。
但是,您可以将要显示的内容呈现为位图,然后在paintComponent(Graphics)方法中绘制该位图。因此,您可以有效地缓冲绘制的内容,并可以在适合您的时候启动对缓冲的位图的更新。
要在位图上绘制,可以执行以下操作:
BufferedImage buffer; // this is an instance variable
private void updateBuffer(){
// Assuming this happens in a subclass of JPanel, where you can access
// getWidth() and getHeight()
buffer=new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics g=buffer.getGraphics();
// Draw into the graphic context g...
g.dispose();
}然后,在您的JPanel中,覆盖paintComponent方法:
public void paintComponent(Graphics g){
g.drawImage(buffer, 0, 0, this);
}https://stackoverflow.com/questions/46217076
复制相似问题