如何用Java创建图像 [英] How to create image in Java

查看:92
本文介绍了如何用Java创建图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的程序中说,我有这个paint()方法.我的愿望是为绘制的矩形创建图像(使用for循环).我尝试了下面的方法,它确实给了我那些矩形(蓝色),但是背景全是黑色.当我运行程序而不创建图像时,仅在JFrame上绘制矩形,背景为白色.我怎样才能解决这个问题. ?

public void paint(Graphics g) {     
    super.paint(g);
    BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
    g = Image.getGraphics();  <<<----- is this correct?
    g.setColor(Color.blue);
    for ( ..... ) {
        g.fillRect(X , Y,  width , height);
            ....        
    }
    try {
    ImageIO.write(image, "jpg", new File("CustomImage.jpg"));
    }catch (IOException e) { 
       e.printStackTrace();
    }
}

解决方案

图像中的背景为黑色,因为除了矩形中的像素外,您没有给其他像素赋任何值. BufferedImage从具有(0,0,0)的RGB(黑色)的每个像素开始.要为整个图像提供白色背景,只需用白色填充整个图像矩形即可.

BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
g = image.createGraphics();  // not sure on this line, but this seems more right
g.setColor(Color.white);
g.fillRect(0, 0, 100, 100); // give the whole image a white background
g.setColor(Color.blue);
for( ..... ){
    g.fillRect(X , Y,  width , height );
        ....        
}

请注意,我的答案是关于将图像写入具有白色背景的文件,而不是有关绘制至具有黑色背景的JFrame.我不确定您想要哪一个.

say in my program, i have this paint() method. my wish is to create an image of the rectangles that are drawn (with the for loop). I tried the method below and it did give me those rectangles (blue color), but the background is all black. When I run program without creating image, just drawing the rect on a JFrame, the background is white. How can i fix this. ?

public void paint(Graphics g) {     
    super.paint(g);
    BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
    g = Image.getGraphics();  <<<----- is this correct?
    g.setColor(Color.blue);
    for ( ..... ) {
        g.fillRect(X , Y,  width , height);
            ....        
    }
    try {
    ImageIO.write(image, "jpg", new File("CustomImage.jpg"));
    }catch (IOException e) { 
       e.printStackTrace();
    }
}

解决方案

The background is black in your image because you are not giving any pixels a value except those in the rectangles. The BufferedImage is starting out with every pixel having RGB of (0, 0, 0), which is black. To give the entire image a white background, simply fill the entire rectangle that is the image with white.

BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
g = image.createGraphics();  // not sure on this line, but this seems more right
g.setColor(Color.white);
g.fillRect(0, 0, 100, 100); // give the whole image a white background
g.setColor(Color.blue);
for( ..... ){
    g.fillRect(X , Y,  width , height );
        ....        
}

Note that my answer is about writing the image to a file with a white background, not about drawing to the JFrame with a black background. I'm not entirely sure which one you wanted.

这篇关于如何用Java创建图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆