将JPanel的内容复制到BufferedImage上 [英] Copy the contents of a JPanel onto a BufferedImage

查看:111
本文介绍了将JPanel的内容复制到BufferedImage上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在第一次通过时,我从我的扩展类中将 BufferedImage s从列表插入我的 JPanel 中:

  @Override 
保护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;
}

下一次我想复制 JPanel 到 BufferedImage

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

图片halfWay = this.createImage(this.getWidth(),this.getHeight());
//现在将它从Image转换为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。
希望这可以让它更清晰

解决方案

为了绘制一个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.

这篇关于将JPanel的内容复制到BufferedImage上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆