Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Learning Regular Expressions this week in the General Computing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Problem with simple drawing tool  RSS feed

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I know this will be a simple problem for all you Java developers out there, but how do I keep the shapes drawn in the following program on the canvas when a user clicks the mouse to draw another shape.

At the moment, each new mouse click on the canvas is simply drawing a new shape.

Also, does anyone know how to add buttons to allow a user to increase the size of the shapes before they add a new shape?

The code for this class 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) {}
}

Help would really be appreciated!
 
Ranch Hand
Posts: 4632
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I've managed to solve this problem now. Thanks to all for their help!!

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!