Copy the contents of a JPanel onto a BufferedImage

蓝咒 提交于 2019-12-23 02:17:20

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/35363892/copy-the-contents-of-a-jpanel-onto-a-bufferedimage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!