• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem linking button to mouse event - Please help!

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I'm pretty new to Java, and as such still struggle from basic problems.

My problem is how to do I link an action event from a button to a mouse event. This is being used in a program to allow users to draw a square, oval etc on a canvas by clicking their mouse button. They choose which shape to draw from the corresponding button. My problem is that the button is not reflecting the mouse click and vice-versa.

My code so far is as follows,

1. DrawToolCanvas class

import java.awt.*;
import java.awt.event.*;

public class DrawToolCanvas extends Canvas implements MouseListener, ActionListener
{
MouseEvent e;

public void paint(Graphics g)
{
}

public void actionPerformed(ActionEvent event)
{ if (event.getActionCommand().equals("square"))
mouseClicked(e);
else if (event.getActionCommand().equals("circle"))
circle();
}

public void circle()
{ Graphics g = getGraphics();
int x=getX(), y=getY();
g.setColor(Color.blue);
g.fillOval( x, y, x = 40, y = 40 );
g.setColor(Color.black);

}

public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseClicked(MouseEvent e)
{
Graphics g = getGraphics();
int x=e.getX(), y=e.getY();
g.setColor(Color.red);
g.fillRect( x, y, x = 40, y = 40 );
g.setColor(Color.black);


}

}

2. DrawToolFrame class

import java.awt.*;
import java.awt.event.*;

public class DrawToolFrame extends Frame
implements WindowListener
{
DrawToolCanvas myCanvas;

public DrawToolFrame()
{
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();
add("Center", myCanvas);
square.addActionListener(myCanvas);
circle.addActionListener(myCanvas);
}


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) {}

}

Any help would be appreciated.
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Jack Nicholas
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Craig,

Thank you so much for your reply. I have one quick question though. How do I keep the squares and circles on the canvas, so the canvas shows all the mouse clicks and doesn't remove them?

Also, is there any way to implement buttons that will allow the user to change the size of the shapes before clicking the canvas with their mouse? It'd be fun if the shapes could be different sizes.

Many thanks,
Jack
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to keep all shapes, you'll have to redraw them all everytime. Therefore, you need to keep a collection (list) of objects to draw.
 
Jack Nicholas
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Thanks for the reply. I know this might sound like a simple question, but how do I keep a list of the shapes already drawn?

I've just started working with AWT and it's proving a bit frustrating at the moment.

Any help please!
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
List was already a hint - check out java.util.List. ArrayList would probably be the best choice.

You have to find a way to store the objects already drawn. You can store the events that triggered the creation of the shapes, but another (better) way is storing special shape objects. These objects store the information needed to draw it.

One hint: use polymorphism for these shape objects. Makes life a lot easier.
 
Jack Nicholas
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Thanks Rob for your replies. I shall try that out tomorrow.

Many thanks,
Jack
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For persistent drawings there are two general approaches. One is to keep the drawn objects/primitives in a List (as Rob mentioned) and the other is to draw them to a BufferedImage and render it in the 'paint' method. This second option works well for freehand drawing with the mouse. For this you can continue with the Graphics methods 'drawOval' and 'drawRect'.
For the storage option you will need some primitive objects you can store. The java.awt.geom package has these: Ellipse2D, Rectangle2D (which includes the Rectangle class), Line2D, and others. You can store both an Ellipse2D and a Rectangle2D as a Shape type since they both implement the Shape interface (see api). They also extend RectangularShape; a friend for later work.

You can make the circles and rectangles different sizes by giving the user a way to select/change the sizes. Decide how you want to receive user input to determine the size, collect the information in your (input devices) event listener and use it to set variables in the canvas/graphic component which (the variables) can control what is drawn in the 'paint' method. You can also use a MouseMotionListener to allow the user to drag/draw ellipses and rectangles onto the canvas.
 
Jack Nicholas
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Craig, thank you so much for all your help. I will continue to persevere with this code tomorrow, and hopefully try to implement your recommendations.

Have a great weekend.

Many thanks,
Jack
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic