【问题标题】:Copy the contents of a JPanel onto a BufferedImage将 JPanel 的内容复制到 BufferedImage
【发布时间】:2016-02-12 13:30:45
【问题描述】:

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

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

下次想将JPanel的内容复制到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;
}

然后,将修改后的BufferedImage 拉回JPanel

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

第二次我得到一个空白面板。

我希望这很清楚,任何帮助都非常感激。

对不起,我的解释很糟糕。我会尽量让自己更清楚。

在每次迭代中,用户都可以在 Jpanel 中的图像上绘图。

我想要做的是将用户更改的 jpanel 复制到缓冲图像中,然后该图像将在 Jpanel 中由用户再次编辑。

这会一直持续到用户选择打印。

因此,除了我放在这里的代码之外,还有用于用户绘图的控件,目前我正在努力将原始 Jpanel 中的初始更新图像放入 bufferedImage,然后再放回 JPanel。 希望这能让它更清楚一些

【问题讨论】:

  • 请澄清,首先告诉我们您要达到什么行为/效果?你想达到什么样的用户体验?接下来,如果您可以创建并发布一个有效的minimal reproducible example,那将非常有用。这将要求您做一些工作,但值得我们能够更全面地理解您的代码和您的问题。详情请查看minimal reproducible example链接。
  • 是的,我很抱歉我尽量保持简单。
  • 第一次通过后,我在 JPanel 中的缓冲图像上绘制矩形。然后第二次我尝试显示更改后的图像以进行更多编辑。我希望这会让它更清楚
  • 再次,我试图让您从编程术语的角度来解释这一点,而不是从用户的角度。所以,如果我错了,请纠正我——您想向用户显示图像,允许用户以某种方式对其进行编辑,然后使用用户生成的更改保存图像,并能够再次生成它以后如果需要的话——对吗?您是否尝试将图像保存到磁盘?要变量?如何?再次,我强烈敦促您为我们创建并发布有效的minimal reproducible example
  • 将 Component.createImage 返回的对象强制转换为 BufferedImage 是不安全的。仅仅因为它现在返回一个 BufferedImage 并不意味着未来的 Java 版本会,或者其他操作系统也会。

标签: java swing graphics bufferedimage


【解决方案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;
}

然后,您可以将返回的图像存储到变量中,然后根据需要将其绘制到 JPanel 中,或者将其写入磁盘。

如果这不能回答您的问题,那么您需要再次提供更多信息并向我们展示您的MCVE

【讨论】:

    猜你喜欢
    • 2015-03-12
    • 2014-08-15
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 2016-10-04
    • 1970-01-01
    • 2017-09-16
    相关资源
    最近更新 更多