File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
JavaRanch
»
Java Forums
»
Java
»
Swing / AWT / SWT
Author
animation using swings
Anandh Ramesh
Ranch Hand
Joined: Dec 15, 2004
Posts: 61
posted
Oct 24, 2006 13:23:00
0
hi,
i need to develop an animation that shows a box moving from one location to another. how do i go about it?
cheers,<br />Anandh
Michael Dunn
Ranch Hand
Joined: Jun 09, 2003
Posts: 4632
posted
Oct 24, 2006 15:39:00
0
here's a couple of ways
1) Thread
import java.awt.*; import java.awt.event.*; import javax.swing.*; class Testing extends JFrame { int x=0, y=0; int counter = 0; public Testing() { final JPanel p = new JPanel(){ public void paintComponent(Graphics g){ super.paintComponent(g); g.setColor(Color.BLUE); g.fillRect(x,y,50,50); } }; getContentPane().add(p); setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(800,600); setLocationRelativeTo(null); setVisible(true); Thread t = new Thread(){ public void run(){ while(true) { x += 5; y += 5; counter++; if(counter > 100) break; try{Thread.sleep(25);}catch(InterruptedException ie){} SwingUtilities.invokeLater(new Runnable(){ public void run(){ p.repaint(); } }); } } }; t.start(); } public static void main(String[] args){new Testing();} }
2) Timer
import java.awt.*; import java.awt.event.*; import javax.swing.*; class Testing extends JFrame { int x=0, y=0; javax.swing.Timer timer; int counter = 0; public Testing() { final JPanel p = new JPanel(){ public void paintComponent(Graphics g){ super.paintComponent(g); g.setColor(Color.BLUE); g.fillRect(x,y,50,50); } }; getContentPane().add(p); setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(800,600); setLocationRelativeTo(null); setVisible(true); ActionListener al = new ActionListener(){ public void actionPerformed(ActionEvent e){ x += 5; y += 5; p.repaint(); counter++; if(counter > 100) timer.stop(); } }; timer = new javax.swing.Timer(25,al); timer.start(); } public static void main(String[] args){new Testing();} }
I agree. Here's the link:
http://aspose.com/file-tools
subject: animation using swings
Similar Threads
Star Office presentation stuff
a fantasy action movie
WA #1.....word association
web based gaming code
Shivaji - To be released
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter