Results 1 to 5 of 5
- 04-23-2011, 03:46 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
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.
- 04-23-2011, 04:08 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,560
- Rep Power
- 11
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.
- 04-23-2011, 04:16 AM #3
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
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.
- 04-23-2011, 04:29 AM #4
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
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.
- 04-23-2011, 04:52 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,560
- Rep Power
- 11
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
-
mouseover event
By pds8475 in forum New To JavaReplies: 4Last Post: 01-31-2011, 03:58 AM -
continuous key press
By afraidofdark in forum Java AppletsReplies: 3Last Post: 03-25-2010, 08:23 PM -
Mouseover question
By Psyclone in forum AWT / SwingReplies: 2Last Post: 02-24-2010, 04:16 AM -
Displaying (scrolling To) A Specific Record In A Continuous Form
By REVANSIDDHA in forum Advanced JavaReplies: 1Last Post: 04-03-2009, 07:17 AM -
continuous playback of chunks
By arnab321 in forum CLDC and MIDPReplies: 0Last Post: 12-11-2008, 08:46 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks