2016-02-12 70 views
1

在第一時間通過內容,我插入從列表BufferedImage小號到我JPanel從我的擴展類:複製一個JPanel的上一個BufferedImage

@Override 
protected void paintComponent(Graphics g){ 
    super.paintComponent(g); 

    if (controlWhichImage == 1){ 
     for (BufferedImage eachImage : docList){ 
      g.drawImage(eachImage, 0,inty,imageWidth,imageHeight,null); 
      intx += eachImage.getWidth(); 
      inty += eachImage.getHeight() * zoomAdd; 
     } 

     if (intx >= this.getWidth() || inty >= this.getHeight()){ 
      inty = 0; 
     } 

下一次我要複製的內容所述JPanelBufferedImage

public void recordImage(){ 
    controlWhichImage = 2; 
    this.createdImage = new BufferedImage(this.getWidth(), this.getHeight(), BufferedImage.TYPE_INT_ARGB); 

    Image halfWay = this.createImage(this.getWidth(), this.getHeight()); 
    //now cast it from Image to bufferedImage 
    this.createdImage = (BufferedImage) halfWay; 
} 

然後,取改性BufferedImage和繪製它放回JPanel

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

這第二次我得到一個空白麪板。

我希望這是明確的,任何幫助感激地收到。

對不起,我的不好解釋。我會盡量讓自己更清楚。

在每次迭代中,用戶都可以在Jpanel中對圖像進行繪製。

我想要做的就是將用戶更改的jpanel複製到一個緩衝的圖像,然後將在Jpanel中由用戶再次編輯。

這一直持續到用戶選擇打印。

因此,除了我在這裏放置的代碼是用戶繪製的控件之外,目前我正在努力將原始Jpanel的初始更新映像放入一個bufferedImage,然後返回到JPanel。 希望這可以讓它更清晰

+2

請澄清,首先告訴我們你試圖達到什麼樣的行爲/效果?你想要達到什麼用戶體驗?接下來,如果您可以創建併發布有效的[mcve],那將極大地幫助***。這可能會問你一些工作,但值得一提的是我們可以更充分地理解你的代碼和你的問題。請查看[mcve]鏈接瞭解詳細信息。 –

+0

是的,我很抱歉,我試圖儘可能保持簡單。 – Gerry

+0

第一次通過後,我在JPanel中的緩衝圖像上繪製矩形。然後在第二次嘗試顯示更改的圖像以進行更多編輯。我希望這更清楚 – Gerry

回答

1

要繪製到BufferedImage,你可以做一些類似於你已經在paintComponent方法中做過的事情,但是使用你的BufferedImage。也許就像方法:

// imgW and imgH are the width and height of the desired ultimate image 
public BufferedImage combineImages(List<BufferedImage> docList, int imgW, int imgH) { 
    // first create the main image that you want to draw to 
    BufferedImage mainImg = new BufferedImage(imgW, imgH, BufferedImage.TYPE_INT_ARGB); 

    // get its Graphics context 
    Graphics g = mainImage.getGraphics(); 

    int intx = 0; 
    int inty = 0; 

    // draw your List of images onto this main image however you want to do this 
    for (BufferedImage eachImage : docList){ 
      g.drawImage(eachImage, 0,inty,imageWidth,imageHeight,null); 
      intx += eachImage.getWidth(); 
      inty += eachImage.getHeight() * zoomAdd; 
     } 
    } 

    // anything else that you need to do 

    g.dispose(); // dispose of this graphics context to save resources 

    return mainImg; 
} 

然後,您可以存儲圖像返回到varaible,並根據需要把它收回去你的JPanel,或將其寫入到磁盤。

如果這不能回答您的問題,那麼您將再次告訴我們您的問題,並告訴我們您的MCVE