Java – when I say red, bufferedimage draws white

It must be a very stupid solution, but I'm blind

I have this Code:

BufferedImage bi = new BufferedImage(800,600,BufferedImage.TYPE_INT_RGB);
bi.getGraphics().setColor(Color.red);
bi.getGraphics().fillRect(300,350,100,50);
ImageIO.write(bi,"jpeg",new File("image.jpg"));

I got this black 800 × 600 rectangle and a white rectangle Why?

Thank you:)

Solution

Every time getgraphics () is called on bufferedimage, a new graphics object will be obtained, so set the color to one, and it will not be set on the next So cache graphic objects

BufferedImage bi = new BufferedImage(800,BufferedImage.TYPE_INT_RGB);
Graphics g = bi.getGraphics();
g.setColor(Color.red);
g.fillRect(300,new File("/home/dave/image.jpg"));
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>