Results 1 to 9 of 9
Thread: Mouse Listener
- 03-17-2011, 03:57 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 5
- Rep Power
- 0
Mouse Listener
Hi. I am new to these forums and a beginner at Java. I am working on my 5th major assignment.
There are many questions I'd like to ask, but I don't want to have you guys do my homework for me. So I figured I'd just ask about the problem that I am 100% stumped on.
I am trying to make a mouse listener, so that when I click my canvas my drawing turtle will appear. I've highlighted the code I've been messing with.
My mouse listener class is:
my Drawing class isJava Code:package turtlegraphics; import java.awt.event.MouseEvent; [B][COLOR="DarkOliveGreen"]public class MouseListener implements java.awt.event.MouseListener { private Drawing _drawing; //private Drawing _canvas; //private Drawing _turtle; public MouseListener(Drawing drawing) { _drawing = drawing; // TODO Auto-generated constructor stub } @Override public void mouseClicked(MouseEvent e) { _drawing.cursorSet(); //_t.setCenterLocation(e.getPoint()); } [/COLOR][/B] @Override public void mousePressed(MouseEvent e) { System.out.println("The frame was clicked."); // TODO Auto-generated method stub } @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } }
Java Code:package turtlegraphics; import java.awt.Point; /** * A drawing object. This object consists of a drawing canvas and one square at first. * Users can interact with the other components on the same frame as the drawing to add additional * elements to the drawing canvas and to manipulate the original square. * * @author <a href="mailto:adrienne@cse.buffalo.edu">Adrienne M Decker</a> * * Created on: Feb 17, 2010 * */ public class Drawing { /** * Create an instance of a drawing canvas and assign it to this instance variable, creating a * composition relationship between the Drawing and DrawingCanvas. */ private graphics.DrawingCanvas _canvas; private graphics.Rectangle _rectangle; private graphics.Rectangle _turtle; private graphics.Polygon _three; //private int _rotation; private javax.swing.JSlider _rotation; /** * Creates a new instance of Drawing, which means that the drawing canvas and the initial * square are created. The drawing canvas should be added to the column passed in as a * parameter to this constructor. The drawing canvas should be 400x400 and black. * The circle can have any size, location, and color that you would like. */ public Drawing(containers.Column column) { _canvas = new graphics.DrawingCanvas(); _canvas.setColor(new graphics.colors.Black()); _canvas.setDimension(new java.awt.Dimension(800,400)); column.add(_canvas); _turtle = new graphics.Rectangle(); _turtle.setColor(new graphics.colors.Green()); _turtle.setDimension(new java.awt.Dimension(15,15)); _turtle.setCenterLocation(new java.awt.Point(400, 200)); _canvas.add(_turtle); } /** * This method is called when the user selects the change location button. It changes the * location of the original circle on the screen to be a random location. */ public void Random() { _turtle.setCenterLocation(_canvas.randomPoint()); } [COLOR="Olive"][B] public void cursorSet(){ _turtle.setCenterLocation(_canvas.getMousePosition()); //_drawing.setCenterLocation(e.getPoint()); //_canvas.add(_turtle); System.out.println("The frame was clicked."); } /**[/B][/COLOR] * This method is called when the user selects the new yellow rectangle button. This method * creates a yellow rectangle with a random dimension with values for width and height between * 20 and 150, but the location of the rectangle is always (55, 62). */ public void Rotate() { Integer r = _turtle.getRotation(); _turtle.setRotation(r - 30); } /** * This method is called when the user selects the new green circle button. This method * creates a green circle with diameter of 45 at a random location on the screen. */ public void Center() { _turtle.setCenterLocation(new java.awt.Point(400, 200)); _turtle.setRotation(0); } /** * This method is called when the user selects the new square button. This method creates * a square with random side length, positioned randomly on the screen, with a random color. */ public void CreateOne() { _rectangle = new graphics.Rectangle(); _rectangle.setDimension(new java.awt.Dimension(80,80)); _rectangle.setCenterLocation(new java.awt.Point(250,250)); _rectangle.setColor(new graphics.colors.Red()); _canvas.add(_rectangle); } public void Erase() { _canvas.removeAllGraphics(); _canvas.add(_turtle); } public void CreateTwo() { graphics.Polygon _three = new graphics.Polygon(); _three.setDimension(new java.awt.Dimension(50,40)); _three.addPoint(new Point(30,0)); _three.addPoint(new Point(40,-40)); _three.addPoint(new Point(50,0)); _three.setColor(new graphics.colors.Yellow()); _three.setCenterLocation(new java.awt.Point(250,250)); _canvas.add(_three); } }
- 03-17-2011, 04:06 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Using _ before variable names is not really the general practice and you should probably avoid it. Did you happen to check out the graphics and mouse listener tutorials in the sun tutorials?
turtle is a rectangle it seems, you want it to just appear where you click?
- 03-17-2011, 04:17 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 5
- Rep Power
- 0
Could you direct me to the Sun tutorials? I looked over a tutorial here but I can't figure out what I'm doing differently.
I do want the rectangle to appear where I click, from there I am supposed to have shapes appear where the turtle is.
- 03-17-2011, 04:18 AM #4
Actually doing the opposite of what OP has done seems to be preferred in many places.
@OP: calling your class the same as that of a class in the Java API is only going to lead to confusion. If you cannot think of a suitable name at least preface your class name with "My".Java Code:class Foo { String name; int value; Foo(String _name, int value) { // some prefer name = _name; // rather than this.value = value; } }
Also, ask a specific question and you will get a specific answer. Do not just dump your code and expect us to know what you want or to provide you with the code.
- 03-17-2011, 04:22 AM #5
Member
- Join Date
- Mar 2011
- Posts
- 5
- Rep Power
- 0
Very well. My question is: What am I doing wrong with the mouse listener? When I click the canvas nothing happens, I can't figure out why.Also, ask a specific question and you will get a specific answer. Do not just dump your code and expect us to know what you what or to provide you with the code.
I will do so in the future. I think I have learned my lesson well from this assignment.calling your class the same as that of a class in the Java API is only going to lead to confusion.
- 03-17-2011, 04:32 AM #6
That is not very specific. "I'm building a plane but it won't fly. What am I doing wrong?"
Have you attached a listener to the canvas?
If you have, do any of the methods in the listener class fire?
If so what you then have to do is get the listener class to callback a method in the Drawing class (the method that will actually do the drawing).
- 03-17-2011, 04:38 AM #7
Member
- Join Date
- Mar 2011
- Posts
- 5
- Rep Power
- 0
No. I did not know that was necessary. How and where do I do this?
What is the listener class fireIf you have, do any of the methods in the listener class fire?
That is what I thought I had done. So it looks like my first question is the solution.If so what you then have to do is get the listener class to callback a method in the Drawing class (the method that will actually do the drawing).
- 03-17-2011, 04:46 AM #8
Using a Button as an example. When you click the button it fires off an event. Nothing will happen unless there is something listening for those events. Many of the GUI components in AWT and swing can have one or more listeners attached to them. They will each listen out for their particular event.
In your case you have to attached a MouseListener (or your mouse listener class) to your canvas. Look to see what methods the canvas class has that will enable you to do this. Don't forget to look at the methods in the parent class(es).
- 03-17-2011, 05:25 AM #9
Member
- Join Date
- Mar 2011
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
How to add mouse clicked listener on rows of a jtable
By pink123 in forum AWT / SwingReplies: 4Last Post: 02-23-2011, 09:25 PM -
inner class Mouse listener not working in MVC model
By Grant in forum AWT / SwingReplies: 2Last Post: 03-01-2010, 03:19 PM -
Mouse Listener for mouse floating over object?
By Krooger in forum AWT / SwingReplies: 1Last Post: 11-18-2009, 04:34 AM -
Tab Listener
By teckno101 in forum AWT / SwingReplies: 2Last Post: 09-29-2009, 09:40 PM -
change the delay between double click in mouse listener
By itaipee in forum AWT / SwingReplies: 6Last Post: 03-17-2009, 02:23 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks