0

I am trying to convert the contents of a JPanel into a BufferedImage. After looking around I have got this code.

    BufferedImage image = new BufferedImage(this.getWidth(), this.getHeight(), BufferedImage.TYPE_INT_RGB);
    Graphics g = image.getGraphics();
    this.paint(g);

I iterate through the image looking for pixels that are colored black using the following.

for(int i = 0; i < image.getWidth(); i++){
        for(int j = 0; j < image.getHeight(); j++){
            Color tempColor = new Color(image.getRGB(i, j));
            if(tempColor == Color.BLACK){
                System.out.println(tempColor); //Debugging
            }
        }
    }

The JPanel contains many pixels that were painted using Color.BLACK (so yes they are black), although when running this code, it never prints the debugging line.

I believe the error in my code has to do with the way I am copying the contents of the JPanel into the BufferedImage, I can't seem to figure out what I am doing wrong. Any help is greatly appreciated, thanks.

6
  • Use print or printAll instead of paint. Make sure you call g.dispose() when your done with it Nov 10, 2015 at 23:22
  • 1
    "I believe the error in my code has to do with the way I am copying the contents of the JPanel into the BufferedImage, I can't seem to figure out what I am doing wrong. Any help is greatly appreciated, thanks." - Probably, consider providing a runnable example which demonstrates your problem. This is not a code dump, but an example of what you are doing which highlights the problem you are having. This will result in less confusion and better responses Nov 10, 2015 at 23:23
  • 1
    The result of new Color(…) can never be the same object as Color.BLACK thus the == check will always be false. Use equals or simply omit dealing with Color objects and check whether image.getRGB(i, j)==0
    – Holger
    Nov 10, 2015 at 23:27
  • Tried, still getting the same error. I didn't believe a runnable example was appropriate in this case because there is no visible evidence of the problem. I have a JPanel with a variety of Color.WHITE, and Color.BLACK pixels, when converting the JPanel to a BufferedImage, the BufferedImage only contains Color.WHITE pixels. @MadProgrammer
    – user3196284
    Nov 10, 2015 at 23:27
  • Thanks @Holger that solved it.
    – user3196284
    Nov 10, 2015 at 23:30

2 Answers 2

0

You are performing a reference equality test when testing tempColor == Color.BLACK. But new Color(…) always creates a new object which can never be the same object as the predefined Color.BLACK instance, thus the == check will always be false.

Use equals or simply omit dealing with Color objects at all and just check whether image.getRGB(i, j) == 0 or if you don't want to use zero for black you can also use image.getRGB(i, j) == Color.BLACK.getRGB()

0

Thanks to @Holger for this answer.

for(int i = 0; i < image.getWidth(); i++){
    for(int j = 0; j < image.getHeight(); j++){
        Color tempColor = new Color(image.getRGB(i, j));
        if(tempColor.equals(Color.BLACK)){ // Error was here
            System.out.println(tempColor); //Debugging
        }
    }
}

Originally I had the code

if(tempColor == Color.BLACK)

instead of

if(tempColor.equals(Color.BLACK))

What I had to begin with will always evaluate to false, which was the error.

Your Answer

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