gpt4 book ai didi

java - 使用图形的语法错误

转载 作者:行者123 更新时间:2023-12-01 11:07:19 24 4
gpt4 key购买 nike

我使用下面的代码将两个 PNG 合并在一起,尽管在以 g.drawImage 开头的两行上都出现语法错误。这来自 Merging two images 的示例但我不能对此发表评论,因为我刚刚在这里注册。

package imageEditor;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;

public class ImageEditor15092703 {
File path = new File("C:/Users/Colton/Desktop/JavaImageEditor/"); // base path of the images

// load source images
BufferedImage image = ImageIO.read(new File(path, "image.png"));
BufferedImage overlay = ImageIO.read(new File(path, "overlay.png"));

// create the new image, canvas size is the max. of both image sizes
int w = Math.max(image.getWidth(), overlay.getWidth());
int h = Math.max(image.getHeight(), overlay.getHeight());
BufferedImage combined = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);

// paint both images, preserving the alpha channels
Graphics g = combined.getGraphics();
g.drawImage(image, 0, 0, null);
g.drawImage(overlay, 0, 0, null);

// Save as new image
ImageIO.write(combined, "PNG", new File(path, "combined.png"));
}

谢谢

编辑

到目前为止,我通过制定方法和异常得到了进一步的帮助。它现在可以编译并运行,但不会创建新的 png 文件。我觉得抛出了异常,导致程序无法执行其应该执行的操作。

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class ImageEditor15092705{

public void ImageEditor15092705() throws IOException{
File path = new File("C:/Users/Colton/Desktop/JavaImageEditor/"); // base path of the images

// load source images
BufferedImage image = ImageIO.read(new File(path, "image.png"));
BufferedImage overlay = ImageIO.read(new File(path, "overlay.png"));

// create the new image, canvas size is the max. of both image sizes
int w = Math.max(image.getWidth(), overlay.getWidth());
int h = Math.max(image.getHeight(), overlay.getHeight());
BufferedImage combined = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);

// paint both images, preserving the alpha channels
Graphics g = combined.getGraphics();
g.drawImage(image, 0, 0, null);
g.drawImage(overlay, 0, 0, null);

// Save as new image
ImageIO.write(combined, "PNG", new File(path, "combined.png"));
}

public static void main (String[] args)
{
ImageEditor15092705 foo = new ImageEditor15092705();
}//end main

} //end image editor class

最佳答案

出现此错误是因为您应该在方法中编写语句,而不是。你看,你创建了一个类,并且你立即在类中编写语句。您应该在方法中编写语句,并且某些语句会抛出异常,因此您应该添加 throw IOException,如下所示;

public static void mergeImage (String p_basePath, String p_image, String p_overlay) throws IOException {
File path = new File(p_basePath); // base path of the images

// load source images
BufferedImage image = ImageIO.read(new File(path, p_image));
BufferedImage overlay = ImageIO.read(new File(path, p_overlay));

// create the new image, canvas size is the max. of both image sizes
int w = Math.max(image.getWidth(), overlay.getWidth());
int h = Math.max(image.getHeight(), overlay.getHeight());
BufferedImage combined = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);

// paint both images, preserving the alpha channels
Graphics g = combined.getGraphics();
g.drawImage(image, 0, 0, null);
g.drawImage(overlay, 0, 0, null);

// Save as new image
ImageIO.write(combined, "PNG", new File(path, "combined.png"));
}

或者您可以添加参数:

public static void mergeImage () throws IOException {
File path = new File("C:/Users/Colton/Desktop/JavaImageEditor/"); // base path of the images

// load source images
BufferedImage image = ImageIO.read(new File(path, "image.png"));
BufferedImage overlay = ImageIO.read(new File(path, "overlay.png"));

// create the new image, canvas size is the max. of both image sizes
int w = Math.max(image.getWidth(), overlay.getWidth());
int h = Math.max(image.getHeight(), overlay.getHeight());
BufferedImage combined = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);

// paint both images, preserving the alpha channels
Graphics g = combined.getGraphics();
g.drawImage(image, 0, 0, null);
g.drawImage(overlay, 0, 0, null);

// Save as new image
ImageIO.write(combined, "PNG", new File(path, "combined.png"));
}

下次,请记住,始终在方法或构造函数中编写语句,并注意某些方法可能抛出的异常。

关于java - 使用图形的语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32805635/

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