且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将JPanel的内容复制到BufferedImage上

更新时间:2023-12-05 11:06:16

为了绘制一个BufferedImage,你可以做一些类似于你已经在你的paintComponent方法中做,但用你的BufferedImage。也许是一种方法:

  // imgW和imgH是所需最终图像的宽度和高度
public BufferedImage combineImages(List< BufferedImage> docList,int imgW,int imgH){
//首先创建要绘制的主图像到
BufferedImage mainImg = new BufferedImage(imgW,imgH,BufferedImage.TYPE_INT_ARGB) ;

//得到它的Graphics上下文
Graphics g = mainImage.getGraphics();

int intx = 0;
int inty = 0;

//将您的图像列表绘制到此主图像上,但是您想为此执行此操作
(BufferedImage eachImage:docList){
g.drawImage(eachImage,0,inty ,imageWidth,imageHeight,NULL);
intx + = eachImage.getWidth();
inty + = eachImage.getHeight()* zoomAdd;
}
}

//你需要做的其他事情

g.dispose(); //处理这个图形上下文以节省资源

return mainImg;
}

然后,您可以将返回的图像存储到变量中,然后将其绘制到您的JPanel如果需要的话,或者把它写到磁盘上。



如果这不能回答你的问题,那么你将再次告诉我们你的 MCVE


On the first time through, I insert BufferedImages from a list onto my JPanel from my extended class:

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

The next time I want to copy the contents of the JPanel to a BufferedImage:

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

And then, take the modified BufferedImage and draw it back onto the JPanel:

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

This second time I get a blank panel.

I hope this is clear, any help gratefully received.

Sorry for my poor explanation. I will try to make myself clearer.

On each iteration the user is able to draw on the image in the Jpanel.

What I want to do is copy the user altered jpanel into a buffered image which will then be in the Jpanel to be edited again by the user.

This continues until the user selects print.

So apart from the code that I have put here are the controls for drawing by the user, at the moment I am struggling with putting the initial updated image from the original Jpanel into a bufferedImage and then back to the JPanel. Hope this makes it somewhat clearer

To draw to a BufferedImage, you would do something similar to what you already do in your paintComponent method, but with your BufferedImage. Perhaps a method like:

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

You could then store the image returned into a varaible and then draw it in your JPanel if desired, or write it to disk.

If this doesn't answer your question, then again you'll want to tell more and show us your MCVE.