每次paintComponent
被調用時,您創建的BufferedImage
一個新實例,如果controlWhichImage
是1
它描繪的圖像到BufferedImage
,如果它是2
它畫,也沒有什麼。
基本上你的代碼是基本操作就像...
Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
this.createdImage = newBufferedImage(this.getWidth(),this.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics g2 = this.createdImage.getGraphics();
if (controlWhichImage == 1){
for(BufferedImage eachImage : docList){
g2.drawImage(eachImage,0,inty,imageWidth,imageHeight,null);
g.drawImage(eachImage, 0,inty,imageWidth,imageHeight,null);
intx += eachImage.getWidth();
inty += eachImage.getHeight() * zoomAdd;
}
if (intx >= this.getWidth() || inty >= this.getHeight()){
inty = 0;
}
} else if (controlWhichImage == 2){
g.drawImage(this.createdImage,this.getWidth(),this.getHeight(),null);
}
你應該只創造createdImaeg
實例時controlWhichImage
是1
Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
if (controlWhichImage == 1){
this.createdImage = newBufferedImage(this.getWidth(),this.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics g2 = this.createdImage.getGraphics();
for(BufferedImage eachImage : docList){
g2.drawImage(eachImage,0,inty,imageWidth,imageHeight,this);
g.drawImage(eachImage, 0,inty,imageWidth,imageHeight,this);
intx += eachImage.getWidth();
inty += eachImage.getHeight() * zoomAdd;
}
if (intx >= this.getWidth() || inty >= this.getHeight()){
inty = 0;
}
g2.dispose(); // This is kind of important...
} else if (controlWhichImage == 2){
g.drawImage(this.createdImage,this.getWidth(),this.getHeight(),this);
}
或當createdImage
爲空...
Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
if (createdImage == null){
this.createdImage = newBufferedImage(this.getWidth(),this.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics g2 = this.createdImage.getGraphics();
for(BufferedImage eachImage : docList){
g2.drawImage(eachImage,0,inty,imageWidth,imageHeight,this);
intx += eachImage.getWidth();
inty += eachImage.getHeight() * zoomAdd;
}
if (intx >= this.getWidth() || inty >= this.getHeight()){
inty = 0;
}
g2.dispose(); // This is kind of important...
}
g.drawImage(this.createdImage,this.getWidth(),this.getHeight(),this);
不要執行昂貴像在paint方法內創建一個BufferedImage一樣的操作。 paintComponent(和所有的繪畫方法)可以每秒調用幾次! – VGR
複製[*將JPanel的內容複製到BufferedImage *上](http://stackoverflow.com/q/35363892/230513)。 – trashgod