[英]Why is my code printing out only part of this image?
Currently I took code from this post: Merging two images目前我从这篇文章中获取了代码: 合并两个图像
And it works fine!它工作正常! However, this is what happens to my image: the combined part doesn't render fully, however, the image size is still proper.但是,这就是我的图像发生的情况:组合部分没有完全渲染,但是,图像大小仍然合适。 http://imgur.com/T6BlQLV http://imgur.com/T6BlQLV
Any help is appreciated, thanks guys!任何帮助表示赞赏,谢谢大家!
Lastly, here's my code:最后,这是我的代码:
public class CAATemplate extends JPanel {
final static int frameWidth = 500;
final static int frameHeight = 500;
static JLabel descBackground = new JLabel("Background Type:");
static JButton make = new JButton("Generate");
static String bgPath = "Images/Backgrounds/"; // base path of the images
static String miPath = "Images/MainIcons/";
static String opPath = "Output/";
static BufferedImage image;
public void CAATemplate() {
}
public void paint(Graphics g) {
g.drawImage(image, 0, 0, Color.gray, this);
}
public static void main(String[] args) throws IOException {
JFrame frame = new JFrame();
JPanel p = new JPanel();
p.add(descBackground);
p.add(make);
frame.add(p);
frame.setBackground(Color.gray);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(frameWidth, frameHeight);
frame.setVisible(true);
frame.setLocation(200, 100);
// load source images
image = ImageIO.read(new File(bgPath + "RedBackground1.png"));
BufferedImage overlay = ImageIO.read(new File(miPath + "Mooncakes.png"));
// create the new image, canvas size is the max. of both image sizes
int w = 2750;
int h = 2125;
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, 437, 1483, null);
// Save as new image
ImageIO.write(combined, "png", new File(opPath + "combined.png"));
}
}
Found out why, apparently having the generating part in the main while also having the frame and panel setup causes it to not render fully.找出原因,显然在主中有生成部分,同时还有框架和面板设置会导致它无法完全渲染。 I made a new method for generating the image and made a button to call that method instead and it worked!我创建了一个用于生成图像的新方法,并创建了一个按钮来调用该方法,并且成功了!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.