Results 1 to 5 of 5
Thread: Simple animation won't work
- 07-08-2009, 09:30 PM #1
Member
- Join Date
- Jul 2009
- Posts
- 18
- Rep Power
- 0
Simple animation won't work
I'm following a tutorial. The tutorial is using AWT but I chose to use swing instead so that may have something to do with it. Its very simple code so I'll post it here.
The idea is to just move a 2D ball from left to right across the screen. However, instead of repainting the screen, it drags the ball across the screen and leaves a thick red line where the ball was moving.
Java Code:package org.me.hello; /** * * @author Nick */ import java.awt.Color; import javax.swing.*; import java.awt.Graphics; public class MyApplet extends JApplet implements Runnable{ private int x_pos = 10; private int y_pos = 100; private int radius = 20; @Override public void init(){ setBackground(Color.blue); } @Override public void start(){ //Thread.start runs the run() method of the target class //in this case, "this" Thread th = new Thread(this); th.start(); } @Override public void stop(){ } @Override public void destroy(){ } @Override public void paint(Graphics g){ g.setColor(Color.red); g.fillOval(x_pos - radius, y_pos - radius, 2 * radius, 2 * radius); } public void run() { //set currentThread to lowest priority Thread.currentThread().setPriority(Thread.MIN_PRIORITY); while(true){ x_pos++; repaint(); //make this thread sleep for 20 milliseconds try{ Thread.sleep(20); }catch(InterruptedException ex){ //do nothing } Thread.currentThread().setPriority(Thread.MAX_PRIORITY); } } }Last edited by nolsen01; 07-08-2009 at 09:45 PM.
-
To fully repaint the screen you need to have the super paint method called as the first method of the paint method. In other words, instead of this:
use this:Java Code:public void paint(Graphics g){ g.setColor(Color.red); g.fillOval(x_pos - radius, y_pos - radius, 2 * radius, 2 * radius); }
Java Code:public void paint(Graphics g){ super.paint(g); // repaints the background of the component g.setColor(Color.red); g.fillOval(x_pos - radius, y_pos - radius, 2 * radius, 2 * radius); }
-
Having said this, if you really want to do Swing animation, you'll make other changes:
1) Draw in a JPanel, not directly in the JApplet itself.
2) Override the JPanel's paintComponent method, not the paint method.
3) Use a Swing Timer instead of a while (true) loop.
4) Call the Swing code using SwingUtilities invoke and wait for thread safety.
For instance:
Java Code:import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class SwingAnimation2 extends JApplet { @Override public void init() { try { // start Swing in a thread-safe way SwingUtilities.invokeAndWait(new Runnable() { public void run() { // add an animation panel object to the applet's contentPane getContentPane().add(new AnimationPanel()); } }); } // to the purist's out there, sorry for catching a naked Exception... catch (Exception e) { System.err.println("Applet failed to initialize"); } } } class AnimationPanel extends JPanel { private static final int RADIUS = 20; private static final int TIMER_DELAY = 20; private int xPos = 10; private int yPos = 100; public AnimationPanel() { setBackground(Color.blue); javax.swing.Timer timer = new javax.swing.Timer(TIMER_DELAY, new ActionListener() { public void actionPerformed(ActionEvent arg0) { xPos++; AnimationPanel.this.repaint(); } }); timer.start(); } @Override // override a Swing JComponent's paintComponent, not the paint method protected void paintComponent(Graphics g) { super.paintComponent(g); // again to repaint the screen // set antialiasing rendering hints to smooth out the circle // this is not necessary but makes for prettier animation Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setColor(Color.red); g.fillOval(xPos - RADIUS, yPos - RADIUS, 2 * RADIUS, 2 * RADIUS); } }Last edited by Fubarable; 07-08-2009 at 11:59 PM.
- 07-08-2009, 11:32 PM #4
Member
- Join Date
- Jul 2009
- Posts
- 18
- Rep Power
- 0
oh i see. I guess because JApplet overloads the paint method from the original Applet class?
Thank you.
-
Similar Threads
-
Animation with Animated GIF
By JavaBean in forum Java 2DReplies: 3Last Post: 06-04-2011, 04:26 PM -
How can I get this to work properly -animation issue with an applet.
By AlejandroPe in forum New To JavaReplies: 3Last Post: 05-06-2009, 12:11 AM -
JTextArea Animation
By hoglu in forum New To JavaReplies: 1Last Post: 12-18-2008, 11:44 AM -
Text animation
By Java Tip in forum java.awtReplies: 0Last Post: 06-21-2008, 08:44 PM -
GUI Animation
By serfster in forum New To JavaReplies: 2Last Post: 06-11-2008, 03:37 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks