2015-09-27 63 views
0

我正在使用下面的代码将两个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 
+0

您应该将错误消息添加到您的帖子。 – Renzo

回答

0

代码语法看起来非常好我不明白为什么它给你错误,你得到的错误是什么? PS:我会在我的电脑上运行的代码,我会使用编辑很快跟进

编辑: 好了,所以有2个问题,

  1. 的代码是不是在一个构造函数/函数,所以这混淆了IDE(我在示例中添加了构造函数,但应该将代码放在函数中以便更好地练习)
  2. 那里有一些未处理的IOException,您可以通过两种方式修复它:是使构造函数(或函数)抛出一个IOException或者围绕着一个ImageIO.write() d ImageIO.read()功能有try/catch块

这里对我来说是什么工作: 包imageEditor;

import java.awt.Graphics; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 

import javax.imageio.ImageIO; 

public class ImageEditor15092703{ 

    public ImageEditor15092703() 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")); 
    } 

} 

正如我说的这个代码代码应工作,但更好的做法用我上面

+0

@Sweeper只是回答,只要你这样做,这是一个答案 –

+0

你不应该发布一个答案告诉OP你要添加一个答案。只要回答,当你知道答案 – Sweeper

+0

我知道,但我想跟帖,我没有足够的代表评论我知道我可以用最喜欢的,但它当时滑倒了我的脑海,它并不重要现在呢? –

1

建议的替代方案,您有这样的错误,因为你应该写语句在方法,而不是。你看,你创建了一个类,并且你可以立即在类中写入语句。您应该在方法中编写语句,并且一些语句会抛出异常,因此您应该像这样添加​​;

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")); 
} 

下一次,只记得,总是写在方法或构造函数语句,并注意可能的例外是一些方法将抛出的。