Results 1 to 6 of 6
  1. #1
    swedishfished is offline Member
    Join Date
    Sep 2012
    Posts
    42
    Rep Power
    0

    Default MouseListener help please

    hey I'm not really understanding why something works and the other doesn't work in this code. basically its going to take the coords of a click and then draw something there.


    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.*;
    
    import java.util.ArrayList;
    
    public class changeColor extends JFrame{
    	
    ArrayList<Point> posArray = new ArrayList<Point>();
    	
    	changeColor(){
    		super("whats up gais");
    		addMouseListener(new MouseClass());
    	}
    
    	public class MouseClass extends MouseAdapter{
    		public void mouseClicked(MouseEvent event){
    			if(event.isMetaDown()==false && event.isAltDown()==false){
    				
    				int y = event.getY();
    				int x = event.getX();
    				posArray.add(new Point(x,y));
    				
    			}
    		}
    		
    	}
    }
    if i get rid of "extends mouse adapter" why does it not work? what is the difference? please explain thanks!

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,923
    Rep Power
    5

    Default Re: MouseListener help please

    Quote Originally Posted by swedishfished View Post
    if i get rid of "extends mouse adapter" why does it not work? what is the difference? please explain thanks!

    Because the method is typed to accept an implementation of MouseListener, which MouseAdapter implements (even though the
    methods are empty). This is done to ensure that you are passing the correct listener at compile time. If you remove the adapter
    extension, then the compiler should print an error that an incorrect argument is passed. It is very similar to trying to pass a String
    argument when an Integer is expected.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    swedishfished is offline Member
    Join Date
    Sep 2012
    Posts
    42
    Rep Power
    0

    Default Re: MouseListener help please

    ok thanks. so is it correct for me to use the adapter extension even if I'm only going to be doing one thing?

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,923
    Rep Power
    5

    Default Re: MouseListener help please

    Yep! And if you override the MouseMotionListener methods then you could simply use the same instance
    to the addMouseMotionListener() method. Same for the addWheelListener() method. In fact, you could do it
    now but since those methods are empty, nothing would happen. But for clarity about interfaces, each instance
    of any given class may be referred to by any interface it implements. And that same instance may
    also be passed to any method that expects one of those implemented interfaces.

    Regards,
    Jim
    Last edited by jim829; 08-23-2014 at 04:48 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    swedishfished is offline Member
    Join Date
    Sep 2012
    Posts
    42
    Rep Power
    0

    Default Re: MouseListener help please

    ok. one more question I'm using JFrame and I'm trying to paint on it how do i add the mouse listener to a JPanel and could i do
    Java Code:
    						public void paintComponent(Graphics g){
    							
    							
    							
    							super.paintComponent(g);
    							g.setColor(Color.BLUE);
    							g.fillRect(120, 20, 100, 40);
    in a for loop?

  6. #6
    swedishfished is offline Member
    Join Date
    Sep 2012
    Posts
    42
    Rep Power
    0

Similar Threads

  1. MouseListener for everything?
    By DFTBA in forum New To Java
    Replies: 2
    Last Post: 07-22-2012, 02:07 PM
  2. Help with mouselistener
    By kastraki2000 in forum Java Applets
    Replies: 2
    Last Post: 04-30-2011, 04:56 PM
  3. can't get x and y from mouselistener
    By j2me64 in forum Java 2D
    Replies: 3
    Last Post: 04-24-2010, 05:57 PM
  4. MouseListener & GUI
    By Suurisa in forum New To Java
    Replies: 2
    Last Post: 10-27-2009, 01:52 AM
  5. i need help for MouseListener
    By sfaxianovic in forum New To Java
    Replies: 2
    Last Post: 08-21-2008, 04:30 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
  •