2015-09-27 23 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")); 
} 

下一次,只記得,總是寫在方法或構造函數語句,並注意可能的例外是一些方法將拋出的。