0

First of all, I wanted to say that I'm not very experienced Java programmer but was forced to learn things very quickly by school assignment. The assignment is to create a grid inside which we have a simple image. When a cell inside an image is clicked, then it's flood filled. The program needs to allow for different type of neighbourhood (4, 6 or 8). I managed to create a program that allows the user to paint the inside the image with different neighbourhoods, however now I'm trying to adjust the program so it would allow the flood fill. My question is - how can I set the boundaries for flood fill? I know that I can set the boundaries based on the color - that's why my image is painted in red, while I was planning to do the filling in black. However, I have no idea how can I put a condition using the color value. I checked different isssues on the net but couldn't quite apply anything to my program. Here's the code I'm using (just the class for paining):

    class MyCanvas extends JPanel implements MouseListener { 

    private int x,y;
    ArrayList<Point> points = new ArrayList<Point>();
    ArrayList<Point> points2 = new ArrayList<Point>();


    @Override
    public void mouseClicked(MouseEvent e) {
    }
    @Override
     public void mouseEntered(MouseEvent e) {
      // TODO Auto-generated method stub

     }

     @Override
     public void mouseExited(MouseEvent e) {
      // TODO Auto-generated method stub

     }


     @Override
        public void mousePressed(MouseEvent e) {
            x = e.getX();
            y = e.getY();

            int xSiatki = x/20;  
            int ySiatki = y/20;  //gets the rectangle coordinate of grid
            System.out.println("Siatka x: " + xSiatki + " " + "y: " + ySiatki );

            if(clickEight){
            points.add(new Point(xSiatki,ySiatki));
            }
            if(clickSix){
            points2.add(new Point(xSiatki,ySiatki));

            }
            repaint();          

            }

     @Override
     public void mouseReleased(MouseEvent e) {
      // TODO Auto-generated method stub

     }



     @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            Krata(g2d);
            Rysunek(g2d);

            if (clickStart){
                Krata(g2d);
                Rysunek (g2d);
                repaint();
                points.clear();
                points2.clear();}

            if (clickEight){
                Krata(g2d);
                Rysunek (g2d);
                for(Point p: points) {
                    int x,y;
                    x = (int)p.getX();
                    y = (int)p.getY();                      
                drawEightFlood(g2d, x, y);}



           }
            if (clickSix){
                Krata(g2d);
                Rysunek (g2d);
                for(Point p: points2) {
                    int x,y;
                    x = (int)p.getX();
                    y = (int)p.getY();

                drawSixFlood(g2d, x, y);}



           }
        }

        private void drawEightFlood (Graphics2D g2d, int x, int y){

            g2d.setColor(Color.BLACK);
            g2d.fillRect (20*x, 20*y, 20, 20);
            g2d.fillRect (20*x, 20*(y+1), 20, 20);
            g2d.fillRect (20*(x+1), 20*y, 20, 20);                                  
            g2d.fillRect (20*(x-1), 20*y, 20, 20);
            g2d.fillRect (20*x, 20*(y-1), 20, 20);                                  
            g2d.fillRect (20*(x-1), 20*(y+1), 20, 20);
            g2d.fillRect (20*(x+1), 20*(y-1), 20, 20);
            g2d.fillRect (20*(x-1), 20*(y-1), 20, 20);
            g2d.fillRect (20*(x+1), 20*(y+1), 20, 20);

            drawEightFlood (g2d, 20*x, 20*(y+1));
            drawEightFlood (g2d, 20*(x+1), 20*y);
            drawEightFlood (g2d, 20*(x+1), 20*(y+1));
            drawEightFlood (g2d, 20*(x-1), 20*(y+1));
            drawEightFlood (g2d, 20*x, 20*(y-1));
            drawEightFlood (g2d, 20*(x-1), 20*(y-1));
            drawEightFlood (g2d, 20*(x-1), 20*(y-1));
            drawEightFlood (g2d, 20*(x-1), 20*y);


            }

        private void drawSixFlood (Graphics2D g2d, int x, int y){

            g2d.setColor(Color.BLACK);
            g2d.fillRect (20*x, 20*y, 20, 20);
            g2d.fillRect (20*x, 20*(y-1), 20, 20);
            g2d.fillRect(20*x, 20*(y+1), 20, 20);
            g2d.fillRect(20*(x+1), 20*(y-1), 20, 20);           
            g2d.fillRect(20*(x+1), 20*y, 20, 20);
            g2d.fillRect(20*(x-1), 20*y, 20, 20);
            g2d.fillRect(20*(x-1), 20*(y+1), 20, 20);

            drawSixFlood (g2d, 20*x, 20*(y+1));
            drawSixFlood (g2d, 20*(x+1), 20*y);
            drawSixFlood (g2d, 20*(x-1), 20*(y+1));
            drawSixFlood (g2d, 20*x, 20*(y-1));
            drawSixFlood (g2d, 20*(x-1), 20*(y-1));
            drawSixFlood (g2d, 20*(x-1), 20*y);

        }

        public void Krata (Graphics2D g2d) {  //code for grid
        for(int i=0; i<54; i++) {
            for(int j=0; j<34; j++) {   
                g2d.setColor(Color.BLACK);
                g2d.drawRect (20*i, 20*j, 20, 20); 
            }
        }
        }

        public void Rysunek (Graphics2D g2d){  //code for the image
        for (int i=0; i<54;i++){
            for (int j=0; j<34; j++){
                if ((i == 0 || i == 53) && (j>12 && j<21 )){                            
                g2d.setColor(Color.RED);
                g2d.fillRect(20*i, 20*j, 20, 20);
        }
                if (((i>0 && i<11) || (i>42 && i<53)) &&(j == 13 || j == 20)){
                    g2d.setColor(Color.RED);
                g2d.fillRect(20*i, 20*j, 20, 20);   
        }
                if ((i > 16 && i < 37) && (j == 7 || j == 20)){                         
                    g2d.setColor(Color.RED);
                    g2d.fillRect(20*i, 20*j, 20, 20);   
            }

                if ((i > 10 && i < 17) && (j == (23-i))){                           
                    g2d.setColor(Color.RED);
                    g2d.fillRect(20*i, 20*j, 20, 20);   
            }
                if ((i > 36 && i < 43) && (j == (i-30))){                           
                    g2d.setColor(Color.RED);
                    g2d.fillRect(20*i, 20*j, 20, 20);   
            }
                if (((i > 10 && i < 17) || (i>36 && i<43)) && (j==23 || j==18)){                            
                    g2d.setColor(Color.RED);
                    g2d.fillRect(20*i, 20*j, 20, 20);   
            }
                if ((i == 11 || i == 16 || i == 37 || i == 42) && (j>18 && j<23)){                          
                    g2d.setColor(Color.RED);
                    g2d.fillRect(20*i, 20*j, 20, 20);   
            }

            }

    }
    }




}
Magda
  • 1
  • 5

1 Answers1

0

The code you present is pretty incomprehensible (no comments, method-names in foreign language - i guess polish or sth like that, overall bad code style, etc.).I'd recommend painting to a BufferedImage instead of directly painting to the screen. This way you can easily check the color of single pixels or modify it or paint to the image and keep changes consistent:

int type = BufferedImage.TYPE_4BYTE_ABGR;
BufferedImage image = new BufferedImage(width , height , type);

Graphics g = image.getGraphics();
//draw stuff with g, just like in paintComponent(Graphics)
g.dispose();

//get color of pixel at (px , py)
Color c = new Color(image.getRGB(px , py));

//set color at pixel (px , py) to red
image.setRGB(px , py , Color.red.getRGB());
  • Hi Paul. First of all, sorry for the sloppy coding, I'll try to improve that. I implemented BufferedImage class to paint the grid and the image. Everything seems fine, but when I try to use drawEightFlood, I'm getting the out of bounds error. I know there must be something wrong with the coordinates but cannot fix it. – Magda Jan 12 '16 at 20:01