2016-02-12 73 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