Why am I getting a black background when saving a JPG image?

I have set the background color JPanel

to white. However, when I save it as JPG or other format, the background is black. I posted this code TYPE_INT_ARGB

but it doesn't work. How do I set the background to a different color? like blue, white, etc.

    public void paintComponent(Graphics g) {
       int width = getWidth();
       int height = getHeight();

       // Create a buffered image in which to draw
       BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

       // Create a graphics contents on the buffered image
       Graphics2D g2d = bufferedImage.createGraphics();     
       g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
       g2d.setStroke(new BasicStroke(1)); // set the thickness of polygon line
       g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.00f));
       g2d.setPaint(Color.black);//color of the polygon line
       g2d.setBackground(Color.WHITE);

       //draw polygon
       for (Polygon triangle : triangles)  
         g2d.drawPolygon(triangle);

       try {
           File file = new File("newimage.jpg");
           ImageIO.write(bufferedImage, "jpg", file);
       } catch (IOException e) {
         }          
 }//public void paint(Graphics g)   

      

+2


source to share


4 answers


I realize this is a very old question, but I had a similar problem and since I found the answer (it's in the javadocs) I thought I'd post it here anyway.

When you set the background color on a graphic, it will only take effect when you clear the region. Hence, clear the entire area at once and you will have a background in your preferred color:



graphics.clearRect(0, 0, width, height);

      

+4


source


You are giving the solution in your question. You are setting the background of the panel to white, not the BufferedImage. You are saving the image as a JPEG, not a panel, so there is a default background in JPEG that appears as black.



0


source


what do you expect to have as background when saving as jpeg? JPEG is for photographs, it cannot have transparent areas, so they must be converted to some color, so you have black (I suppose). Why don't you save the image as PNG? Or, if you want to use the first JPEG fill area with white, then start painting on it ...

0


source


Your approach to creating an image back if you ask me. All other questions related to drawing polygons on the panel. Now do you change the code to draw the image?

When extending the JPanel and calling super.paintComponent (), guess what's going on? The background is colored! Then you make your own polygon painting. In the above code, you simply create an image and then draw polygons.

A simpler approach is to simply create a subroutine to paint the panel with the image, then you can reuse the code without overriding the paintComponent method for each component.

The ScreenImage class does this for you.

0


source







All Articles