-
Java Paint Bucket Tool
Hi. I´m working on a simple paint program and I´m 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.
-
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
-
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?
-
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 10:50 AM.
eschew obfuscation
-
Thanks!! I think this helps.
Similar Threads
-
Replies: 1
Last Post: 05-13-2005, 06:46 AM
-
By Mike Tsakiris in forum .NET
Replies: 11
Last Post: 10-04-2002, 05:32 PM
-
By Dan Dubinsky in forum java.announcements
Replies: 0
Last Post: 06-27-2002, 09:02 PM
-
By zfqjava in forum java.announcements
Replies: 2
Last Post: 01-08-2002, 04:26 AM
-
By JJ in forum Enterprise
Replies: 1
Last Post: 07-06-2000, 04: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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks