Results 1 to 9 of 9

Thread: Mouse Listener

  1. #1
    Quizerno is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default 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:
    Java 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
    
    	}
    
    }
    my Drawing class is

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

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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?

  3. #3
    Quizerno is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    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?
    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.

  4. #4
    Junky's Avatar
    Junky is online now Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,761
    Rep Power
    7

    Default

    Quote Originally Posted by sunde887 View Post
    Using _ before variable names is not really the general practice
    Actually doing the opposite of what OP has done seems to be preferred in many places.
    Java Code:
    class Foo {
        String name;
        int value;
        Foo(String _name, int value) {
            // some prefer
            name = _name;
            // rather than
            this.value = value;
        }
    }
    @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".

    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.

  5. #5
    Quizerno is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default

    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.
    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.

    calling your class the same as that of a class in the Java API is only going to lead to confusion.
    I will do so in the future. I think I have learned my lesson well from this assignment.

  6. #6
    Junky's Avatar
    Junky is online now Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,761
    Rep Power
    7

    Default

    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).

  7. #7
    Quizerno is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    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?
    No. I did not know that was necessary. How and where do I do this?


    If you have, do any of the methods in the listener class fire?
    What is 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).
    That is what I thought I had done. So it looks like my first question is the solution.

  8. #8
    Junky's Avatar
    Junky is online now Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,761
    Rep Power
    7

    Default

    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).

  9. #9
    Quizerno is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default

    Tried adding "frame.addMouseListener(label)" to my App class.
    Also tried "_canvas.addMouseListener(_ms);" in my Drawing class.

    Neither worked.

Similar Threads

  1. Replies: 4
    Last Post: 02-23-2011, 09:25 PM
  2. Replies: 2
    Last Post: 03-01-2010, 03:19 PM
  3. Mouse Listener for mouse floating over object?
    By Krooger in forum AWT / Swing
    Replies: 1
    Last Post: 11-18-2009, 04:34 AM
  4. Tab Listener
    By teckno101 in forum AWT / Swing
    Replies: 2
    Last Post: 09-29-2009, 09:40 PM
  5. Replies: 6
    Last Post: 03-17-2009, 02:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •