File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Problem linking button to mouse event - Please help! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Problem linking button to mouse event - Please help!" Watch "Problem linking button to mouse event - Please help!" New topic
Author

Problem linking button to mouse event - Please help!

Jack Nicholas
Greenhorn

Joined: Nov 05, 2005
Posts: 6
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.
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Jack Nicholas
Greenhorn

Joined: Nov 05, 2005
Posts: 6
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
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19790
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jack Nicholas
Greenhorn

Joined: Nov 05, 2005
Posts: 6
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

Joined: Oct 27, 2005
Posts: 19790
    
  20

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

Joined: Nov 05, 2005
Posts: 6
Hi,

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

Many thanks,
Jack
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
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

Joined: Nov 05, 2005
Posts: 6
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem linking button to mouse event - Please help!