Page 1 of 2 12 LastLast
Results 1 to 20 of 21
Like Tree1Likes

Thread: Won't go to mouse listener

  1. #1
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    92
    Rep Power
    0

    Default Won't go to mouse listener

    Okay,so problems over here.

    I'm trying to make it so that the window follows the mouse,and draws a square where it is over,just so i can track it right now.

    The problem,however,is that the MouseMoved/Dragged aren't actually being called.I thought that they were called automatically every frame by default,that's what they did on my other game. I've copied the code directly over,and nothing. Did something change from 1.6 to 1.7?

    Java Code:
    package Main;
    
    import java.awt.event.*;
    
    public class Listener implements MouseListener, MouseMotionListener, MouseWheelListener {
    
    	public void mouseDragged(MouseEvent e) {
    		Component.mse.setLocation(e.getX(), e.getY());
    		System.out.println("Moved");
    	}
    
    	public void mouseMoved(MouseEvent e) {
    		Component.mse.setLocation(e.getX(), e.getY());
    		System.out.println("Moved");
    	}
    
    	
    	public void mouseClicked(MouseEvent arg0) {
    		
    	}
    
    	
    	public void mouseEntered(MouseEvent arg0) {
    
    		
    	}
    
    	
    	public void mouseExited(MouseEvent arg0) {
    		
    		
    	}
    
    	
    	public void mousePressed(MouseEvent arg0) {
    		
    		
    	}
    
    	
    	public void mouseReleased(MouseEvent arg0) {
    		
    		
    	}
    
    	public void mouseWheelMoved(MouseWheelEvent arg0) {
    		
    	}
    
    }
    
    package Main;
    
    import java.awt.*;
    import javax.swing.*;
    import java.applet.*;
    
    class Component extends Applet implements Runnable{
    	  static JFrame frame = new JFrame();
    	  
    	  Listener listener = new Listener();
    	  
    	  public static Point mse = new Point(15,15);
    	  
    	  public Component(){
    		    addMouseListener(listener);
    			addMouseMotionListener(listener);
    			addMouseWheelListener(listener);
    	  }
    	
    	  public static void main(String[] args) {
    	    DrawingComponent fps = new DrawingComponent();
    	    Bee newb = new Bee(200, 100, 5, 5, Color.yellow);
    	     
    	    fps.addRect(newb);
    	     
    	    fps.animate();
    	 
    	    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	    fps.setPreferredSize(new Dimension(800, 600));
    	    frame.getContentPane().add(fps);
    	    frame.pack();
    	    frame.setLocationRelativeTo(null);
    	    frame.setVisible(true);
    	  }
    
    	public void run() {
    		
    	}
    }
    I thought you just did addMousMotionListener(listening); and then it called it once every frame. And I KNOW it's not calling the MouseMoved at all,because of the System.out.println() that I threw in there,which is not coming up in the console period. I tried calling it directly ( Listening.mouseMoved() ) But that just loops it wanting to be a static but can't. So,what's my problem?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default Re: Won't go to mouse listener

    Several comments about the posted code:
    Component is a java SE class. Use a different name for your class.
    applets do NOT use a main() method.
    Is the class's constructor ever called?
    where is the DrawingComponent class defined?
    Where is the Bee class defined?
    Too many static variables.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Won't go to mouse listener

    Well, I don't remember exactly but don't applets need an init or start invocation? Also, if your listener simply extends MouseAdapter you won't
    need to include the dummy methods.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    92
    Rep Power
    0

    Default Re: Won't go to mouse listener

    Quote Originally Posted by Norm View Post
    Several comments about the posted code:
    Component is a java SE class. Use a different name for your class.
    applets do NOT use a main() method.
    Is the class's constructor ever called?
    where is the DrawingComponent class defined?
    Where is the Bee class defined?
    Too many static variables.
    Those have nothing to do with what I'm asking about. I'll optimize code later.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default Re: Won't go to mouse listener

    You can do whatever you want.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    92
    Rep Power
    0

    Default Re: Won't go to mouse listener

    Quote Originally Posted by jim829 View Post
    Well, I don't remember exactly but don't applets need an init or start invocation? Also, if your listener simply extends MouseAdapter you won't
    need to include the dummy methods.

    Regards,
    Jim
    I didn't think I needed anything. My previous game(Which is now broken anyway) didn't need it. Then again,before it broke,it was having the exact same problem as this is now....

  7. #7
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    92
    Rep Power
    0

    Default Re: Won't go to mouse listener

    Quote Originally Posted by Norm View Post
    You can do whatever you want.
    I intend to. Now,back to topic,please.

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

    Default Re: Won't go to mouse listener

    We are on topic. Norm gave you suggestions. I gave you suggestions. It is up to you to implement them.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    92
    Rep Power
    0

    Default Re: Won't go to mouse listener

    I'll look into how to initiate it or whatever. thanks.

  10. #10
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    92
    Rep Power
    0

    Default Re: Won't go to mouse listener

    Okay,I derped. I'm sorry. Yes,it wasn't called at all. I think i fixed it now.

  11. #11
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    92
    Rep Power
    0

    Default Re: Won't go to mouse listener

    Okay,i've revised over it a bit,went and found a few tutorials,and I still can't get it.

    New code:
    Java Code:
    package Main;
    
    import java.awt.*;
    
    import javax.swing.*;
    
    import java.applet.*;
    
    class Main extends Applet implements Runnable{
    	  static JFrame frame = new JFrame();
    	  
    	  public Listener listener = new Listener();
    	  
    	  static Main main = new Main();
    	  
    	  boolean isRunning = true;
    	  
    	  public static Point mse = new Point(0,0);
    	  
    	  Main(){
    		    addMouseListener(listener);
    			addMouseMotionListener(listener);
    			System.out.println("MouseInit");
    	  }
    	
    	  public static void main(String[] args) {
    	    DrawingComponent fps = new DrawingComponent();
    	    Bee newb = new Bee(200, 100, 10, 10, Color.yellow);
    	     
    	    fps.addRect(newb);
    	     
    	    fps.animate();
    	    
    	    frame.add(main);
    	    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	    fps.setPreferredSize(new Dimension(800, 600));
    	    frame.getContentPane().add(fps);
    	    frame.pack();
    	    frame.setLocationRelativeTo(null);
    	    frame.setVisible(true);
    	    main.start();
    	    
    	  }
    	  
    	public void run() {
    		
    	}
    	
    	public void start(){
    		while(isRunning = true);
    			
    		}
    	}
    Pretty much the only changes are the loop in Start and such. Now I at least know it's getting to the addMouseListener(listening) (Because of the MousInit println),but it doesn't seem to update the position still. The Listening code is exactly the same. Do I need to manually update the mouse point? Because apparently I'm missing something right in front of me. It adds the mouse listener,then nothing. It doesn't actually call the mouseMoved,ever. I add a checking println,and that doesn't pop up in the console. So it goes to addMouseListener,and addMouseMotionListener,but it never actually uses the Listener class. ( FYI as a refrence i'm using just a big yellow box for the location of the mouse. I know it's being updated since I can add seperate code to move it and it moves freely.)

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

    Default Re: Won't go to mouse listener

    First, let's clear up something. Do you really want to run this as an applet via a browser? Or do you want to run it as a regular Java program on a client PC?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  13. #13
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    92
    Rep Power
    0

    Default Re: Won't go to mouse listener

    Just a regular Java Program. The tutorial I took originally said to use both in the actual code,since it made it easier.

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

    Default Re: Won't go to mouse listener

    Ok, great! Now I do not know what the other classes you reference are for but I can get your mouse working for you. However, if they expect an applet then this will not be compatible. Nor can I tell you how to use them in any event. Forget implementing runnable for now. Have your class extend JPanel. Then add that to the JFrame you have created (do that in the constructor for Main).

    Also add the mouseListeners to the JPanel instance. Set your JPanel size and then do a frame.pack() and frame.setVisible(true).

    And I would remove the static modifier from JFrame.

    Regard,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  15. #15
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    92
    Rep Power
    0

    Default Re: Won't go to mouse listener

    Java Code:
    package Main;
    
    import java.awt.*;
    
    import javax.swing.*;
    
    class Main extends JPanel{
    	private static final long serialVersionUID = 1L;
    
    	public static Listener listener = new Listener();
    	  
    	static Main main = new Main();
    	  
    	boolean isRunning = true;
    	  
    	public static Point mse = new Point(0,0);
    	  
    	Main(){
    		
    		addMouseListener(listener);
    	 	addMouseMotionListener(listener);
    		System.out.println("MouseInit");
    		
    	}
    	
    	public static void main(String[] args) {
    		JFrame frame = new JFrame();
    		JPanel panel = new JPanel();
    		
    		DrawingComponent fps = new DrawingComponent();
    		Bee newb = new Bee(200, 100, 10, 10, Color.yellow);
    		   
    		fps.addRect(newb);
    	  
    		fps.animate();
    		 
    		frame.add(main);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    	 	panel.setPreferredSize(new Dimension(800,600));
    	 	
    	 	frame.getContentPane().add(fps);
    	 	frame.pack();
    	 	frame.setLocationRelativeTo(null);
    	 	frame.setVisible(true);
    	 	main.start();
    	 	
    	  }
    	  
    
    	public void start(){
    		
    	}
    }
    That's what I have now. First,the AddMouseListener won't go into the main(String arg0) because it's static,and it has to be. I just need to know how to "add that to the JFrame you have created (do that in the constructor for Main)."

    If you have anything else i should do,just say. I tested it like that,and still nothing.

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

    Default Re: Won't go to mouse listener

    You misunderstood what I said. Here is a typical structure for a JPanel in a JFrame. You do not need to create a new instance of JPanel. It is created when you create an instance of Main in the static main method.

    p
    Java Code:
    ublic MyClass extends JPanel {
         JFrame frame = new JFrame();
         public MyClass() { // constructor
          frame.add(this);
          setSize(500,500); // your choice for size
          addMouseListener(listener);
             // other listeners.
          frame.pack();
          frame.setVisible(true);
    
         }
    //  rest of code
    
          }
    Regards,
    Jim

  17. #17
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    92
    Rep Power
    0

    Default Re: Won't go to mouse listener

    frame.getContentPane().add(fps);

    Okay,new problem. Whenever I have that line,it will draw my on-screen bojects,but not move my mouse. Without it,it's the other way around.

    Here's the draw code and other class.

    Java Code:
    package Main;
    
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;
    
    import javax.swing.JComponent;
    import javax.swing.JPanel;
    import javax.swing.Timer;
    
    class Bee {
    	  int x;
    	  int y;
    	  int height;
    	  int width;
    	  Color color;
    	  Bee(int x, int y, int width, int height, Color color) {
    		  	this.x = x;
    	    	this.y = y;
    	    	this.width = width;
    	    	this.height = height;
    	    	this.color = color;
    	  }
    	 
    	  public void draw(Graphics g) {
    		  g.setColor(color);
    		  g.fillRect(x, y, width, height);
    		  g.fillRect(Main.mse.x,Main.mse.y, 50, 50);
    		  System.out.println("Beez2");
    	  }
    	 
    	  public int getY() {
    		  return y;
    	  }
    	  
    	  public int getX(){
    		  return x;
    	  }
    	   
    	  public int getHeight() {
    		  return height;
    	  }
    	   
    	  public void setY(int y) {
    		  this.y = y;
    	  }
    	  
    	  public void setX(int x) {
    		  this.x = x;
    	  }
    }
    
    class DrawingComponent extends JPanel {
    	private static final int ANIMATION_DELAY = 10;
    	private List<Bee> beeList = new ArrayList<Bee>();
    	private int dirY = 0;
    	private int dirX = 0;
    	   
    	  DrawingComponent() {
    		  
    	  }
    	   
    	  public void animate() {
    		    new Timer(ANIMATION_DELAY, new ActionListener() {
    			      public void actionPerformed(ActionEvent arg0) {
    				        for (Bee bee : beeList) {
    					          int y = bee.getY();
    					          int x = bee.getX();
    					          
    					          if(new Random().nextInt(5) == 1){
    					        	  dirY = 1;
    					          } else if(new Random().nextInt(5) == 2){
    					        	  dirY = -1;
    					          } else if(new Random().nextInt(5) == 3){
    					        	  dirX = 1;
    					          } else if(new Random().nextInt(5) == 4){
    					        	  dirX = -1;
    					          } else if(new Random().nextInt(5) == 5){
    					        	  dirX = 0;
    					        	  dirY = 0;
    					          } 
    					          
    					          try {
    								Thread.sleep(50);
    							} catch (InterruptedException e) {
    								e.printStackTrace();
    							}
    					           
    					          bee.setY(y + (dirY * 2));
    					          bee.setX(x + (dirX * 2));
    					        }
    				        repaint();
    			      }
    		    }).start();
    		    System.out.println("Beez");
    	  }
    	 
    	  public void addRect(Bee bee) {
    		  beeList.add(bee);
    	  }
    	 
    	  @Override
    	  public void paintComponent(Graphics g) {
    		    super.paintComponent(g);
    			    for (Bee bee : beeList) {
    				    System.out.println("Beez1");
    			    	bee.draw(g);
    			    }
    		  }
    	}
    It's all contained in one file. The drawing and everything. But it's kinda weird. Anything wrong with it?

  18. #18
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,532
    Blog Entries
    7
    Rep Power
    20

    Default Re: The Idiot's Help Guide For Mulberry bags Described

    Quote Originally Posted by RiokuTheSlayer View Post
    Can i report this guy,please?
    I banned the creature already (forever).

    kind regards,

    Jos
    RiokuTheSlayer likes this.
    cenosillicaphobia: the fear for an empty beer glass

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

    Default Re: Won't go to mouse listener

    First, to fix your current problem. I had you do some unncessary stuff because I did not have access to your other classes. Mea Culpa. So, first, don't create a new JPanel. But set the size of fps appropriately and add the mouse listener to fps. This latter situation is why the mouse isn't dragging. It would be best to do it like this:

    Java Code:
    // other code here
    DrawingComponent fps = new DrawingComponent();
        Main(){   
           fps.addMouseListener(listener);
           fps.addMouseMotionListener(listener);
           frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           fps.setPreferredSize(new Dimension(800,600));
           frame.add(fps);
           
           frame.pack();
           frame.setLocationRelativeTo(null);
           frame.setVisible(true);
           System.out.println("MouseInit");
        }
    // other code here.

    Other recommendations:

    1. If you aren't certain, you must become familiar with static and non-static contexts. You cannot reference an instance value in a static context.
    2. Don't declare any of your values static (except main of course). It isn't needed. I know you are doing it to reference a value across objects but there is a better way.
    You can pass an instance of the object to the class where it is needed via the constructor. For example, in Listener, just pass mse. Since the reference is shared across objects, changing its state will be available wherever you have the same reference. Another alternative is to use inner classes. Inner classes have access to instance fields of the enclosing class.
    3. Do your initialization in the Main constructor. Adding components to frame, repacking etc.
    4. Use the MouseAdapter. It will save some typing in future GUI's.
    5. Check out the Oracle Tutorials in my signature. They are a great resource.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default Re: Won't go to mouse listener

    Use the MouseAdapter.
    Be sure to use the @Override notation when you override the methods in an adapter. The compiler is happy to let you add new methods to the class that won't be called as listeners.
    If you don't understand my response, don't ignore it, ask a question.

Page 1 of 2 12 LastLast

Similar Threads

  1. Keyboard Listener to Mouse Listener
    By cupOJava in forum Advanced Java
    Replies: 0
    Last Post: 04-28-2013, 09:26 PM
  2. Help with mouse listener please.
    By fatabass in forum AWT / Swing
    Replies: 1
    Last Post: 02-23-2012, 04:39 PM
  3. Need some help with mouse listener
    By Serialcek in forum AWT / Swing
    Replies: 8
    Last Post: 01-07-2012, 10:20 AM
  4. Mouse listener
    By chetoos in forum Advanced Java
    Replies: 3
    Last Post: 06-23-2011, 09:23 PM
  5. Mouse Listener for mouse floating over object?
    By Krooger in forum AWT / Swing
    Replies: 1
    Last Post: 11-18-2009, 04:34 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
  •