2

Here's my code:

    Image partNumberImage = Toolkit.getDefaultToolkit().getImage("D:/partNumber.png");
    Image lotNumberImage = Toolkit.getDefaultToolkit().getImage("D:/lotNumber.png");
    Image dteImage = Toolkit.getDefaultToolkit().getImage("D:/dte.png");
    Image quantityImage = Toolkit.getDefaultToolkit().getImage("D:/quantity.png");

    BufferedImage combinedImage = new BufferedImage(486, 
                                          151, 
                                          BufferedImage.TYPE_INT_RGB);


    Graphics g = combinedImage.getGraphics();

    combinedImage.createGraphics().setBackground(Color.white);

    g.clearRect(0,0, 486, 151);
    g.drawImage(partNumberImage, x, 18, null);
    g.drawImage(lotNumberImage, x, 48, null);
    g.drawImage(dteImage, x, 58, null);
    g.drawImage(quantityImage, x, 68, null);


    g.dispose();

    Iterator writers = ImageIO.getImageWritersByFormatName("png");
    ImageWriter writer = (ImageWriter) writers.next();
    if (writer == null) {
        throw new RuntimeException("PNG not supported?!");
    }

     ImageOutputStream out = ImageIO.createImageOutputStream(
                                new File("D:/Combined.png" ));
    writer.setOutput(out);
    writer.write(combinedImage);
    out.close();
}

My problem is the code will output this image:

enter image description here

what I need is to have white background for the image. Thanks!

2 Answers 2

3

This looks risky to me:

Graphics g = combinedImage.getGraphics(); // Graphics object #1

combinedImage.createGraphics().setBackground(Color.white);  // Graphics object #2
// so now you've set the background color for the second Graphics object only

g.clearRect(0,0, 486, 151);  // but clear the rect in the first Graphics object
g.drawImage(partNumberImage, x, 18, null);
g.drawImage(lotNumberImage, x, 48, null);
g.drawImage(dteImage, x, 58, null);
g.drawImage(quantityImage, x, 68, null);

It appears to me that you may be creating two very distinct Graphics objects, one a Graphics2D object and one a Graphics object. And while you're setting the background color in the Graphics2D object, your clearing a rect in the Graphics object, so it could explain why your background is not white. Why not instead just create one Graphics2D object and use it for everything:

Graphics2D g = combinedImage.createGraphics(); 
g.setBackground(Color.white);

//  Now there is only one Graphics object, and its background has been set
g.clearRect(0,0, 486, 151);  // This now uses the correct background color
g.drawImage(partNumberImage, x, 18, null);
g.drawImage(lotNumberImage, x, 48, null);
g.drawImage(dteImage, x, 58, null);
g.drawImage(quantityImage, x, 68, null);
Sign up to request clarification or add additional context in comments.

Comments

2

Before you add the images, draw a white rectangle the size of your image:

g.clearRect(0,0, 486, 151);
g.setColor(Color.white);
g.fillRect(0,0,486,151);

g.drawImage(partNumberImage, x, 18, null);
g.drawImage(lotNumberImage, x, 48, null);
g.drawImage(dteImage, x, 58, null);
g.drawImage(quantityImage, x, 68, null);


g.dispose();

1 Comment

it only generates a white image.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.