为什么我的代码只打印出此图像的一部分?

时间:2016-09-15 02:23:13

标签: java

目前我从这篇文章中获取了代码:Merging two images

它工作正常!但是,这就是我的图像所发生的情况:组合部分没有完全渲染,但图像尺寸仍然合适。 http://imgur.com/T6BlQLV

感谢任何帮助,谢谢你们!

最后,这是我的代码:

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

}

1 个答案:

答案 0 :(得分:-1)

找出原因,显然在主体中具有生成部件同时还具有框架和面板设置导致其不能完全渲染。我创建了一个生成图像的新方法,并创建了一个按钮来调用该方法,它起作用了!