简体   繁体   English

阻止JScrollPane重绘JPanel

[英]Stop JScrollPane from redraw JPanel

I have a JPanel with a JScrollPane sorounding it, the problem is that when i use the JScrollPane the JPanels redraw methode get called. 我有一个JPanel及其周围的JScrollPane,问题是当我使用JScrollPane时,会调用JPanels重绘方法。 I want to disable that because my JPanel redraw by its self at the right time. 我想禁用它,因为我的JPanel会在适当的时候自行重绘。

I want it so it just updateds the getClipBounds() for the paint methode but withoud calling the paint methode. 我想要它,所以它只更新了paint方法的getClipBounds(),而无需调用paint方法。

You can't do that - since the viewport displays different parts of the contained JPanel, depending on the position of the scrollbar, the areas that have to be repainted might in fact be newly revealed and might not have been painted before. 您不能这样做-由于视口显示所包含的JPanel的不同部分,具体取决于滚动条的位置,实际上必须重新显示必须重新绘制的区域,并且以前可能没有绘制过。

Since JScrollPane doesn't know how the contained Component is implemented and whether it repaints its entire area or only the area that needs repainting, it forces the contained Component to redraw itself upon scrolling. 由于JScrollPane不知道所包含的Component是如何实现的,以及它是否重绘整个区域还是仅重绘需要重绘的区域,因此它强制所包含的Component在滚动时重绘自身。

However, you can instead render the content you want to show to a bitmap, and then paint the bitmap in the paintComponent(Graphics) method. 但是,您可以将要显示的内容呈现为位图,然后在paintComponent(Graphics)方法中绘制位图。 Thus, you effectively buffer your painted content and can initiate an update to the buffered bitmap whenever it suits you. 因此,您可以有效地缓冲绘制的内容,并可以在需要时随时启动对缓冲位图的更新。

In order to paint onto a bitmap, you can do this: 为了绘制到位图上,可以执行以下操作:

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

Then, in your JPanel, you override the paintComponent method: 然后,在您的JPanel中,重写paintComponent方法:

public void paintComponent(Graphics g){
    g.drawImage(buffer, 0, 0, this);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM