|
-
keeping shapes on a canvas with a paint tool
Hi,
I'm trying to write a basic paint tool, and cannot seem to keep the shapes drawn on the canvas. Each time a user clicks on the canvas, it draws a new shape, and deletes the previous shape.
Any idea how to keep all the shapes drawn on the canvas?
Help me please!!!
Code so far is as follows:
import java.awt.*;
import java.awt.event.*;
public class DTF extends Frame implements WindowListener
{
DrawToolCanvas myCanvas; // member variable
public DTF()
{
setTitle("Draw Tool Frame");
addWindowListener(this);
Button square, circle;
Panel myPanel = new Panel();
square = new Button("square"); square.setActionCommand("square");
circle = new Button("circle"); circle.setActionCommand("circle");
myPanel.add(square); myPanel.add(circle);
add("South", myPanel);
// DrawToolCanvas myCanvas = new DrawToolCanvas(); // local variable
myCanvas = new DrawToolCanvas(); // member variable
add("Center", myCanvas);
square.addActionListener(myCanvas);
circle.addActionListener(myCanvas);
setSize(400,400);
setLocation(200,200);
setVisible(true);
}
public void windowClosing(WindowEvent event) { System.exit(0); }
public void windowOpened(WindowEvent event) {}
public void windowIconified(WindowEvent event) {}
public void windowDeiconified(WindowEvent event) {}
public void windowClosed(WindowEvent event) {}
public void windowActivated(WindowEvent event) {}
public void windowDeactivated(WindowEvent event) {}
public static void main(String[] args)
{
new DTF();
}
}
class DrawToolCanvas extends Canvas implements MouseListener, ActionListener
{
int x, y;
boolean drawCircle;
public DrawToolCanvas()
{
x = -40;
y = -40;
drawCircle = true;
addMouseListener(this);
}
public void paint(Graphics g)
{
super.paint(g);
if(drawCircle)
{
g.setColor(Color.blue);
g.fillOval( x, y, 40, 40 );
}
else
{
g.setColor(Color.red);
g.drawRect(x, y, 40, 40);
}
}
public void actionPerformed(ActionEvent event)
{
if (event.getActionCommand().equals("square"))
drawCircle = false;
else if (event.getActionCommand().equals("circle"))
drawCircle = true;
}
public void mousePressed(MouseEvent e)
{
x = e.getX();
y = e.getY();
repaint();
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
}
Similar Threads
-
Replies: 4
Last Post: 09-02-2005, 02:50 PM
-
Replies: 1
Last Post: 08-17-2005, 06:42 PM
-
By Actipro Software in forum dotnet.announcements
Replies: 0
Last Post: 08-25-2002, 11:11 PM
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