Results 1 to 4 of 4
Thread: Time
- 09-06-2008, 07:33 PM #1
Member
- Join Date
- Jul 2008
- Posts
- 14
- Rep Power
- 0
Time
hey, im trying to get a method with a for loop to run and then wait a second before it runs again for '(int i = 0; i <= 50; i++)' the loop draws a square 1 pixel below its previous location every loop.
I tried using Thread.sleep(100) but all that happened was that the loop ran as normal and then the square jumped into the position it would end up in.
Can anyone suggest to me another way of doing it that i can look into?
Thx
Fireking
- 09-06-2008, 09:18 PM #2
its Thread.sleep(1000) and this link might help you.
Java Applet Tutorial - Double Buffering Example
Google for term "double buffering"dont worry newbie, we got you covered.
-
If you are using Swing, likely you are putting the EDT, the Event Dispatch Thread, to sleep. This is the single thread that Swing uses to do its drawing and to interact with the user.
One solution is to use a background thread within which you can call Thread.sleep(...), but if you do this, you have to take care that most all Swing calls get called back on Event Dispatch Thread.
A safer and easier solution is to use a Swing Timer which will perform a block of code repeatedly without blocking Swing events, and all code called in this Timer gets called correctly on the EDT. Sun has a decent tutorial on this as well as of course the API both of which I suggest you read.
For instance:
Java Code:import java.awt.Dimension; import java.awt.Graphics; import java.awt.Point; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.image.BufferedImage; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import javax.imageio.ImageIO; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.Timer; class SimpleAnimationPanel extends JPanel { private static final int DELAY = 20; public static final int X_TRANSLATION = 2; public static final int Y_TRANSLATION = 2; private Point point = new Point(5, 32); private BufferedImage duke = null; private Timer timer = new Timer(DELAY, new TimerAction()); public SimpleAnimationPanel() { try { // borrow an image from sun.com duke = ImageIO.read(new URL( "http://java.sun.com/products/plugin/images/duke.wave.med.gif")); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } setPreferredSize(new Dimension(600, 400)); timer.start(); } // do our drawing here in the paintComponent override @Override protected void paintComponent(Graphics g) { super.paintComponent(g); if (duke != null) { g.drawImage(duke, point.x, point.y, this); } } private class TimerAction implements ActionListener { public void actionPerformed(ActionEvent e) { int x = point.x; int y = point.y; Dimension size = SimpleAnimationPanel.this.getSize(); if (x > size.width) { x = 0; } else { x += X_TRANSLATION; } if (y > size.height) { y = 0; } else { y += Y_TRANSLATION; } point.setLocation(new Point(x, y)); // update the point SimpleAnimationPanel.this.repaint(); } } private static void createAndShowUI() { JFrame frame = new JFrame("SimpleAnimationPanel"); frame.getContentPane().add(new SimpleAnimationPanel()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); } }
Last edited by Fubarable; 09-06-2008 at 10:23 PM.
- 09-07-2008, 02:30 AM #4
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 12
if you are using threads:
Java Code:import java.applet.*; import java.awt.*; public class TimerTest extends Applet implements Runnable { final Thread timer; final int cycletime = 1000; public void init() { setSize(500,500); timer = new Thread(this); timer.start(); } public void run() { while (true) { // Do things here try { timer.sleep(cycletime); } catch (InterruptedException ie) { System.out.println("ERROR sleeping"); } } } }
to stop the above Thread: timer.stop() (deprecated) or timer = nullI die a little on the inside...
Every time I get shot.
Similar Threads
-
Quiz Time
By rjuyal in forum Advanced JavaReplies: 1092Last Post: 06-23-2012, 09:55 AM -
i click on it,then first time error page comes,second time click then product page co
By 82rathi.angara in forum New To JavaReplies: 21Last Post: 08-01-2008, 12:13 PM -
Hello, first time here.
By ludragon in forum IntroductionsReplies: 2Last Post: 01-03-2008, 06:03 AM -
DataObject with the time given by me
By garinapavan in forum New To JavaReplies: 2Last Post: 08-07-2007, 07:33 PM
Bookmarks