Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default Mouseover with continuous updating?

    I am currently working on a simple swing application which requires shifting the coordinates of an image. This shifting is supposed to occur whenever the mouse if over a rectangle placed near the edges of the screen. If you have played any RTS games such as Starcraft, C&C, or Warcraft 3, it is similar to their shifting movement. Currently, I can only think of writing a loop which constantly checks if the mouse is inside this rectangle, and if so, add some integer to the locations of the image. I feel this is not a proper way to use swing, so...

    What is the proper way of doing this with Swing? I can't seem to find any listeners which would constantly be called when the mouse is over something. mouseMoved is the only event I could find that gave me decent results, but the image stops moving when my mouse does; I need it to continue to shift.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    What about detecting mouse entered/exited events and using that to determine whether motion is currently required.

    Or, again, mouse moved could be used to turn motion on or off.

  3. #3
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    I am not sure I understand you. Lets say I use mouseEntered/mouseExited or mouseMoved. The cursor is over a rectangle in the right side of the screen; therefore, I should be shifting the image. In the case for mouseEntered, shouldn't it only be called once? This would not solve my problem. mouseMoved will also no solve my problem because I need this image to continue to shift even if the mouse is not moving.

    Is there some event I can set so that the swing panel will continue to call some function every update? Or trick Swing into calling mouseMoved, even though my mouse is not moving?

    I am not sure you understand my problem, but if you do, I must be lacking some knowledge on how to use swing events.

  4. #4
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    I think I found a solution. I just created a swing.Timer and call it every 100ms to shift the image if the mouse was inside the rectangle. Thank you for you help though.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I think you're right to use a timer. The following is what I had in mind:

    Java Code:
    import java.awt.Dimension;
    import java.awt.Point;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    import javax.swing.Timer;
    import javax.swing.UIManager;
    
    public class PanelEdgeEg extends JPanel {
    	
    	private Point target;
    	private JLabel thing; // something to move
    	private Timer timer;
    	
    	public PanelEdgeEg() {
    		setLayout(null);
    		addMouseListener(listener);
    		addMouseMotionListener(listener);
    		thing = new JLabel("*");
    		add(thing);
    		thing.setSize(20, 20);
    		thing.setLocation(50, 50);
    		timer = new Timer(100, moveAL);
    	}
    	
    		/*
    		 * Mouse event are used, not to move things, but to turn 
    		 * motion on or off.
    		 */
    	private MouseAdapter listener = new MouseAdapter() {
    		@Override
    		public void mouseEntered(MouseEvent e) {
    			setTarget(e.getPoint());
    		}
    
    		@Override
    		public void mouseExited(MouseEvent e) {
    			setTarget(null);
    		}
    
    		@Override
    		public void mouseMoved(MouseEvent e) {
    			setTarget(e.getPoint());
    		}
    	};
    	
    	private void setTarget(Point p) {
    		if(p == null) {
    			timer.stop();
    			return;
    		} else {
    			target = p.getLocation();
    			if(!timer.isRunning()) {
    				timer.start();
    			}
    		}
    	}
    
    		/*
    		 * Something to represent onscreen motion
    		 */
    	private ActionListener moveAL = new ActionListener() {
    		@Override
    		public void actionPerformed(ActionEvent e) {
    			Point loc = thing.getLocation();
    			double dist = loc.distance(target);
    			int deltaX = (int)(10 * (target.x - loc.x) / dist);
    			int deltaY = (int)(10 * (target.y - loc.y) / dist);
    			
    			thing.setLocation(loc.x + deltaX, loc.y + deltaY);
    		}
    	};
    	
    	public static void main(String[] args) {
    		try {
    			UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName());
    		} catch (Exception e) {}
    		final JFrame frame = new JFrame("PanelEdgeEg");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    		SwingUtilities.invokeLater(new Runnable() {
    			@Override
    			public void run() {
    				frame.add(new PanelEdgeEg());
    				frame.setSize(new Dimension(500, 500));
    				frame.setLocationRelativeTo(null);
    				frame.setVisible(true);
    			}
    		});
    	}
    }

Similar Threads

  1. mouseover event
    By pds8475 in forum New To Java
    Replies: 4
    Last Post: 01-31-2011, 03:58 AM
  2. continuous key press
    By afraidofdark in forum Java Applets
    Replies: 3
    Last Post: 03-25-2010, 08:23 PM
  3. Mouseover question
    By Psyclone in forum AWT / Swing
    Replies: 2
    Last Post: 02-24-2010, 04:16 AM
  4. Replies: 1
    Last Post: 04-03-2009, 07:17 AM
  5. continuous playback of chunks
    By arnab321 in forum CLDC and MIDP
    Replies: 0
    Last Post: 12-11-2008, 08:46 PM

Posting Permissions

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