2016-02-13 32 views
0

我從列表(this.docList)中有兩個BufferedImages。在我的paintComponent方法中,我將它們繪製到JPanel中,並同時在'this.createdImage上繪製它們。如何將JPanel的內容複製到BufferedImage

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

我的問題是,當我去使用createdImage我得到的是一個空白的面板。

if (controlWhichImage == 2){ 
    g.drawImage(this.createdImage,this.getWidth(),this.getHeight(),null); 
} 
+2

不要執行昂貴像在paint方法內創建一個BufferedImage一樣的操作。 paintComponent(和所有的繪畫方法)可以每秒調用幾次! – VGR

+0

複製[*將JPanel的內容複製到BufferedImage *上](http://stackoverflow.com/q/35363892/230513)。 – trashgod

回答

2

每次paintComponent被調用時,您創建的BufferedImage一個新實例,如果controlWhichImage1它描繪的圖像到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實例時controlWhichImage1

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); 
+0

好的,我已經將它改爲: – Gerry

+0

if(createdImage == null){this.createdImage = new BufferedImage(this.getWidth(),this.getHeight(),BufferedImage.TYPE_INT_ARGB); } – Gerry

+0

但在2上它仍然是空白的。但是,謝謝你的回覆 – Gerry