gpt4 book ai didi

java - 将 JPanel 的内容复制到 BufferedImage 上

转载 作者:行者123 更新时间:2023-11-30 07:36:14 25 4
gpt4 key购买 nike

第一次,我将列表中的 BufferedImage 插入扩展类的 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。希望这能让它更清楚

最佳答案

要绘制到 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 .

关于java - 将 JPanel 的内容复制到 BufferedImage 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35363892/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com