Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The basic concept would look something like this...</p> <pre><code>public class MoveMe01 { public static void main(String[] args) { new MoveMe01(); } public MoveMe01() { EventQueue.invokeLater( new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception ex) { } final JFrame frame = new JFrame("Test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new JLabel("Use the Force Luke")); frame.pack(); frame.setLocation(0, 0); frame.setVisible(true); Timer timer = new Timer(40, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { Point location = frame.getLocation(); Point to = new Point(location); if (to.x &lt; 100) { to.x += 4; if (to.x &gt; 100) { to.x = 100; } } if (to.y &lt; 100) { to.y += 4; if (to.y &gt; 100) { to.y = 100; } } frame.setLocation(to); if (to.equals(location)) { ((Timer)e.getSource()).stop(); } } }); timer.setRepeats(true); timer.setCoalesce(true); timer.start(); } }); } } </code></pre> <p>This is a pretty straight, linear animation. You'd be better of investigating one of the many animation engines available for Swing, this will provide you with the ability to change the speed of the animation based on the current frame (doing things like slow-in and slow-out for example)</p> <p>I would take a look at</p> <ul> <li><a href="http://java.net/projects/timingframework/pages/Home" rel="nofollow">Timing Framework</a></li> <li><a href="http://kenai.com/projects/trident/pages/Home" rel="nofollow">Trident</a></li> <li><a href="http://code.google.com/p/java-universal-tween-engine/" rel="nofollow">Universla Tween Engine</a></li> </ul> <p><strong>Updated with a "variable time" solution</strong></p> <p>This is basically an example of how you might do a variable time animation. That is, rather then having a fixed movement, you can adjust the time and allow the animation to calculate the movement requirements based on the run time of the animation...</p> <pre><code>public class MoveMe01 { public static void main(String[] args) { new MoveMe01(); } // How long the animation should run for in milliseconds private int runTime = 500; // The start time of the animation... private long startTime = -1; public MoveMe01() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception ex) { } final JFrame frame = new JFrame("Test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new JLabel("Use the Force Luke")); frame.pack(); frame.setLocation(0, 0); frame.setVisible(true); Timer timer = new Timer(40, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (startTime &lt; 0) { // Start time of the animation... startTime = System.currentTimeMillis(); } // The current time long now = System.currentTimeMillis(); // The difference in time long dif = now - startTime; // If we've moved beyond the run time, stop the animation if (dif &gt; runTime) { dif = runTime; ((Timer)e.getSource()).stop(); } // The percentage of time we've been playing... double progress = (double)dif / (double)runTime; Point location = frame.getLocation(); Point to = new Point(location); // Calculate the position as perctange over time... to.x = (int)Math.round(100 * progress); to.y = (int)Math.round(100 * progress); // nb - if the start position wasn't 0x0, then you would need to // add these to the x/y position above... System.out.println(to); frame.setLocation(to); } }); timer.setRepeats(true); timer.setCoalesce(true); timer.start(); } }); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload