Set BufferedImage to be a color in Java

前端 未结 5 984
既然无缘
既然无缘 2021-01-07 19:52

I need to create a rectangular BufferedImage with a specified background color, draw some pattern on the background and save it to file. I don\'t know how to cr

5条回答
  •  萌比男神i
    2021-01-07 20:02

    Get the graphics object for the image, set the current paint to the desired colour, then call fillRect(0,0,width,height).

    BufferedImage b_img = ...
    Graphics2D    graphics = b_img.createGraphics();
    
    graphics.setPaint ( new Color ( r, g, b ) );
    graphics.fillRect ( 0, 0, b_img.getWidth(), b_img.getHeight() );
    

提交回复
热议问题