Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5968459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:05:35+00:00 2026-05-22T20:05:35+00:00

I have been playing with image flood fill that I found here on Stack

  • 0

I have been playing with image flood fill that I found here on Stack Overflow.

I think the code is not the problem. Though if you have a better one I would be glad to see (or even better if you know a library which has this type of image manipulations).

My problem is that after I run the algorithm on this image the guy’s helmet instead of being green is light grey.

I have tried it on a silly example created in Paint and it works fine.
Thus, I think there must be some image setting or something of that kind which changes the rgb value I set to it in the algorithm.

Do you have any suggestions to what should be set in the code (please see below)?

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class FloodFillTest extends JPanel
{
    private static final long serialVersionUID = 1L;
    private BufferedImage bI;
    public FloodFillTest()
    {
        try
        {
            this.bI = ImageIO.read(new File("images/brother.png"));
            Color targetColor = Color.WHITE;
            Color replacementColor = Color.GREEN;
            System.out.println("targetColor="+targetColor+"; replacementColor="+replacementColor);
            floodFill(125, 90, targetColor, replacementColor, bI);      
            setPreferredSize(new Dimension(bI.getWidth(), bI.getHeight()));
            System.out.println("bI.getWidth()="+bI.getWidth()+"; bI.getHeight()="+bI.getHeight());
        }catch(IOException ex)
        {
            Logger.getLogger(FloodFillTest.class.getName()).log(Level.SEVERE, null, ex);
        }
    }   

    /**
     * Fills a color in the image with a different color.
     * @param x x coordinate of starting point.
     * @param y y coordinate of starting point.
     * @param targetColor color we want to replace.
     * @param replacementColor the color which is used as the replacement.
     * @param image the image where we fill the color.
     */
    public static void floodFill(int x, int y, Color targetColor, Color replacementColor,
            BufferedImage image)
    {
        if(image.getRGB(x, y) != targetColor.getRGB()) 
            return;
        image.setRGB(x, y, replacementColor.getRGB());
        System.out.println("after set image.getRGB(x,y)="+ new Color(image.getRGB(x,y)).toString());
        floodFill(x - 1, y, targetColor, replacementColor, image);
        floodFill(x + 1, y, targetColor, replacementColor, image);
        floodFill(x, y - 1, targetColor, replacementColor, image);
        floodFill(x, y + 1, targetColor, replacementColor, image);
    }
    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);        
        Graphics2D g2 = (Graphics2D) g;
        g2.drawImage(bI, 0,0, null);
    }   

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {               
                System.out.println("Color.WHITE="+Color.WHITE +"; Color.BLACK="+Color.BLACK);
                JPanel p = new FloodFillTest();
                p.setBackground(Color.CYAN);
                JPanel contentPane = new JPanel();
                contentPane.add(p);
                JFrame f = new JFrame();
                f.setContentPane(contentPane);
                f.setSize(800, 600);
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setVisible(true);
            }
        });
    }
}

This is the image I am using in tests. The image I am using in tests.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T20:05:36+00:00Added an answer on May 22, 2026 at 8:05 pm

    You got a grayscale image there. You cannot use green color on a grayscale image. That’s why it turns up as light gray.

    You need to either:

    • convert the image to RGB beforehand
    • make sure that you read/convert the image as RGB within Java

    The last option is more safe as it will not fail on future images. Here is some code I found on the web that is said to do conversion to Grayscale. A small modification and you have what you need to ensure you are working on a color image:

    public static BufferedImage convertToGrayscale(BufferedImage source) { 
         BufferedImageOp op = new ColorConvertOp(
           ColorSpace.getInstance(ColorSpace.CS_GRAY), null); 
         return op.filter(source, null);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been playing with the demo code from this msdn article by Jeffrey
I have been playing around with a search control and i have noticed that
I am making a game that needs a crosshair. I have been playing with
I have been playing with some of Apple's example code for customizing UITableViewCells. I
I have been playing with the Ruby library shoes. Basically you can write a
I have been playing with this for a while, but the closest I have
I have been playing with the Linq to Sql and I was wondering if
I have been playing around with the postgresql.conf file for a couple days now.
I have been playing with javascript arrays and I have run into, what I
I have been playing around with the EF to see what it can handle.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.