Forum Stats

  • 3,839,855 Users
  • 2,262,546 Discussions
  • 7,901,081 Comments

Discussions

FloodFill method in Java

User_AYF65
User_AYF65 Member Posts: 135 Red Ribbon
edited Oct 4, 2015 12:28PM in New To Java

Hi,

Somebody please guide me whether there is any floodfill method in java? I have made a polygon using g.drawLine(...). Now i want to fill that polygon.

Color greenColor= new Color(0, 255, 0);

                 g.drawLine(0,250,250,250);

  //g.drawLine(250,250,280,479);

  g.drawLine(250,250,280,479);

  //g.drawLine(280,479,0,479);

  g.drawLine(280,479,0,479);

  //g.drawLine(0,479,0,250)

  g.drawLine(0,479,0,250);

In Visual C++, I was doing it:

pDC->FloodFill(30,270,RGB(0,255,0));


Somebody please tell me about the replacement of this command in JAVA.


Zulfi.

Best Answer

  • User_AYF65
    User_AYF65 Member Posts: 135 Red Ribbon
    edited Oct 4, 2015 10:02AM Answer ✓

    Hi,

    Thanks for your guidance. It worked.

                     Color greenColor= new Color(0, 255, 0);

                     g.setColor(greenColor);

                     /*g.drawLine(0,250,250,250);

      //g.drawLine(250,250,280,479);

      g.drawLine(250,250,280,479);

      //g.drawLine(280,479,0,479);

      g.drawLine(280,479,0,479);

      //g.drawLine(0,479,0,250)

      g.drawLine(0,479,0,250);*/

                       int xpoints[] = {0, 250, 280, 0, 0};

                       int ypoints[] = {250, 250, 479, 479, 250};

                       int npoints = 5;

       

        g.fillPolygon(xpoints, ypoints, npoints);

                     

    Zulfi.

Answers

  • TPD-Opitz
    TPD-Opitz Member Posts: 2,465 Silver Trophy
    edited Oct 4, 2015 5:08AM

    In java we don't need FloodFill.

    You could have looked up the Type of g in the public Java API. There you could have found the fill(Shape) method which does your requirement in one step.

    bye

    TPD

  • User_AYF65
    User_AYF65 Member Posts: 135 Red Ribbon
    edited Oct 4, 2015 10:02AM Answer ✓

    Hi,

    Thanks for your guidance. It worked.

                     Color greenColor= new Color(0, 255, 0);

                     g.setColor(greenColor);

                     /*g.drawLine(0,250,250,250);

      //g.drawLine(250,250,280,479);

      g.drawLine(250,250,280,479);

      //g.drawLine(280,479,0,479);

      g.drawLine(280,479,0,479);

      //g.drawLine(0,479,0,250)

      g.drawLine(0,479,0,250);*/

                       int xpoints[] = {0, 250, 280, 0, 0};

                       int ypoints[] = {250, 250, 479, 479, 250};

                       int npoints = 5;

       

        g.fillPolygon(xpoints, ypoints, npoints);

                     

    Zulfi.

  • Unknown
    edited Oct 4, 2015 12:28PM

    As I already mentioned in my reply to your other thread I suggest you go through The Java Tutorials trail for 'Getting Started with Graphics'.

    It shows you, with WORKING code, how to draw and fill in Java.

    https://docs.oracle.com/javase/tutorial/2d/basic2d/

    Lesson: Getting Started with Graphics

    The Java 2D API is powerful and complex. However, the vast majority of uses for the Java 2D API utilize a small subset of its capabilities encapsulated in the java.awt.Graphics class. This lesson covers the most common needs of applications developers. Less common needs are described later in the Advanced topics in the Java 2D API.
    Most methods of the Graphics class can be divided into two basic groups:
    
    Draw and fill methods, enabling you to render basic shapes, text, and images
    Attributes setting methods, which affect how that drawing and filling appears
    
    Methods such as setFont and setColor define how draw and fill methods render.
    

    There are MANY such tutorials on the web that will make it much easier to learn how to use the graphics functionality.

This discussion has been closed.