2012-11-26 77 views
0

我想绘制2个图像,一个在另一个之上。第1个图像是一个箭头(在最终图像中应该显示为标题)。第一个图像(箭头)是32x32像素,而第二个图像是24x24像素。绘制两个覆盖图像

理想情况下,我想从第1个图像的右下角开始在第1个顶部绘制第2个图像。

目前我使用这样的代码

// load source images 
     BufferedImage baseImage = ImageIO.read(new File(baseImg.getFileLocation())); 
     BufferedImage backgroundImage = ImageIO.read(new File(backgroundImg.getFileLocation())); 

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

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

     int index = baseImg.getFileLocation().lastIndexOf(".png"); 
     String newFileName = baseImg.getFileLocation().substring(0, index); 
     // Save as new image 
     ImageIO.write(combined, "PNG", new File(newFileName + "_combined.png")); 

但是这对我来说不是很的工作,因为最终的结果是与第二图象只被绘制的32×32的图像。

任何帮助表示赞赏。

谢谢!

回答

1

看起来问题在于你最后绘制的是32x32背景图像,这意味着它将被打印在另一幅图像的顶部,使得它看起来好像24x24图像从未被绘制过。

如果将这两行交换,应该会看到两个图像。来源:

g.drawImage(baseImage, 0, 0, null); 
g.drawImage(backgroundImage, 0, 0, null); 

到:

g.drawImage(backgroundImage, 0, 0, null); 
g.drawImage(baseImage, 0, 0, null); 


然而,这将以此为24x24的图像中的左上角,你说你想它在右下角。这可以通过一些基本的减法来完成:

g.drawImage(baseImage, w - baseImage.getWidth(), h - baseImage.getHeight(), null);