Java Paint Bucket Tool


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Java Paint Bucket Tool

Hybrid View

  1. #1
    Join Date
    Sep 2005
    Posts
    5

    Java Paint Bucket Tool

    Hi. Im working on a simple paint program and Im having some trouble with the bucket or filling tool. This tool must fill any shape drawn by the user with the current color (the user clicks on a given point and the tool replaces the color of the area surrounding that point with the new color). I have used several algorithms for this (flood fill, boundary fill, etc..) but non of them seem to work. The recursive ones cause an out of stack or out of memory error, and iterative versions are too slow and never finish (only good for filling very small areas) .
    I was wondering if there is any way in Java to go around this.
    Thanks.

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    If you cast the Graphics to Graphics2D in your paint method, and have your
    shapes stored as Polygon objects you can use the fill(Shape s) method of
    Graphics2D.
    eschew obfuscation

  3. #3
    Join Date
    Sep 2005
    Posts
    5
    But how do I keep track of Shapes? The user can draw with a pencil tool anything he wants, at any given time. It is like MS Paint so you have an idea.
    How can I make Shape objects based on all the user draws?

  4. #4
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Like this

    This is a very basic setup. Currently it only deals with Polygons, so
    you may want to include variables and objects for handling basic curves and
    lines. You may then want to include a popupMenu for selecting stuff
    like "Draw Shape" and checking for closed polygons and color selection
    and .....


    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    
    class DrawPanel extends JPanel implements MouseMotionListener, MouseListener {
      Point lastPoint=null;
      boolean isDragging=false;
      Polygon currentPoly=null;
      ArrayList polyList=new ArrayList(); // finished shapes
      ArrayList currPoints=new ArrayList(); // current drawing shape
    
      public DrawPanel() {
        this.addMouseListener(this);
        this.addMouseMotionListener(this);
      }
      public void update(Graphics g) {
        Color c=g.getColor();
        // draw background
        g.setColor(Color.white);
        g.fillRect(0,0,getWidth(),getHeight());
        Graphics2D g2D=(Graphics2D)g;
        // draw finished shapes
        for (int i=0; i<polyList.size(); i++) {
          Polygon pol=(Polygon)polyList.get(i);
          g2D.setColor(Color.black);
          g2D.drawPolygon(pol);
          g2D.setColor(Color.blue);
          g2D.fill(pol);
        }
        // draw current shape (if any)
        g2D.setColor(Color.black);
        for (int i=0; i<currPoints.size(); i++) {
          if (i < currPoints.size()-1) {
            Point p0=(Point)currPoints.get(i);
            Point p1=(Point)currPoints.get(i+1);
            g2D.drawLine(p0.x,p0.y,p1.x,p1.y);
          }
        }
        g.setColor(c);
      }
      public void paint(Graphics g) {
        update(g);
      }
      public void mouseDragged(MouseEvent e) {
        if (!isDragging) {
          currentPoly=new Polygon();
          currentPoly.addPoint(lastPoint.x,lastPoint.y);
          currPoints.add(new Point(e.getX(),e.getY()));
        }
        isDragging=true;
    
        currentPoly.addPoint(e.getX(),e.getY());
        currPoints.add(new Point(e.getX(),e.getY()));
    
        repaint();
      }
      public void mouseMoved(MouseEvent e) {}
    
      // deletes shape(s) if rightclicked inside
      public void mouseClicked(MouseEvent e) {
        if (SwingUtilities.isRightMouseButton(e)) {
          for (int i=polyList.size()-1; i>=0; i--) {
            Polygon pol=(Polygon)polyList.get(i);
            if (pol.contains(e.getPoint())) {
              polyList.remove(i);
            }
          }
        }
        repaint();
      }
      
      public void mousePressed(MouseEvent e) {
        lastPoint=e.getPoint();
      }
      public void mouseReleased(MouseEvent e) {
        if (isDragging) {
          polyList.add(currentPoly); // add new shape to list
          currPoints.clear(); // initialize current drawing shape
        }
        isDragging=false;
        repaint();
      }
      public void mouseEntered(MouseEvent e) {
      }
      public void mouseExited(MouseEvent e) {
      }
    
    }
    public class DrawTool extends JFrame {
      public DrawTool() {
      }
      public static void main(String[] args)  {
        DrawTool dt = new DrawTool();
        dt.getContentPane().setLayout(new GridLayout());
        dt.getContentPane().add(new DrawPanel());
        dt.setBounds(100,100,400,300);
        dt.addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
            System.exit(0);
          }
        });
        dt.setVisible(true);
      }
    
    }
    Last edited by sjalle; 09-02-2005 at 11:50 AM.
    eschew obfuscation

  5. #5
    Join Date
    Sep 2005
    Posts
    5
    Thanks!! I think this helps.

Similar Threads

  1. Java vs. .Net. A questionnaire
    By Basil in forum .NET
    Replies: 1
    Last Post: 05-13-2005, 07:46 AM
  2. learning c# very confusing.
    By Mike Tsakiris in forum .NET
    Replies: 11
    Last Post: 10-04-2002, 06:32 PM
  3. The JADE Open Framework, an open source RAD tool for the J2EE!
    By Dan Dubinsky in forum java.announcements
    Replies: 0
    Last Post: 06-27-2002, 10:02 PM
  4. Simple tool to help you create Java class
    By zfqjava in forum java.announcements
    Replies: 2
    Last Post: 01-08-2002, 05:26 AM
  5. Re: VB vs. Visual Age for Java
    By JJ in forum Enterprise
    Replies: 1
    Last Post: 07-06-2000, 05:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center