Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create image in Java

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();
    }
}
like image 769
dorothy Avatar asked Dec 06 '13 13:12

dorothy


3 Answers

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.

like image 103
jonhopkins Avatar answered Oct 16 '22 07:10

jonhopkins


Try this

public void paint(Graphics g) {     
  super.paint(g);
  BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
  g = Image.createGraphics();     // it should be createGraphics
  g.setBackground(Color.black);
  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();
}

}
like image 2
rachana Avatar answered Oct 16 '22 06:10

rachana


BufferedImage bufferedImage = new BufferedImage(width, height, 
              BufferedImage.TYPE_INT_RGB);

Graphics2D g2d = bufferedImage.createGraphics();

Font font = new Font("Georgia", Font.BOLD, 18);
g2d.setFont(font);

RenderingHints rh = new RenderingHints(
       RenderingHints.KEY_ANTIALIASING,
       RenderingHints.VALUE_ANTIALIAS_ON);

rh.put(RenderingHints.KEY_RENDERING, 
       RenderingHints.VALUE_RENDER_QUALITY);

g2d.setRenderingHints(rh);

GradientPaint gp = new GradientPaint(0, 0, 
Color.red, 0, height/2, Color.black, true);

g2d.setPaint(gp);
g2d.fillRect(0, 0, width, height);

g2d.setColor(new Color(255, 153, 0));
like image 2
Amit Kumar user3037405 Avatar answered Oct 16 '22 07:10

Amit Kumar user3037405