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.
09-02-2005, 09:02 AM
sjalle
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.
09-02-2005, 09:12 AM
gjab13
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?
09-02-2005, 10:46 AM
sjalle
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 .....
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);
}