Results 1 to 4 of 4

Thread: Time

  1. #1
    Fireking is offline Member
    Join Date
    Jul 2008
    Posts
    14
    Rep Power
    0

    Default 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

  2. #2
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    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.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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 09:23 PM.

  4. #4
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    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");
                }
            }
        }
    }
    I prefer Threads, they're easier. :)

    to stop the above Thread: timer.stop() (deprecated) or timer = null
    I die a little on the inside...
    Every time I get shot.

Similar Threads

  1. Quiz Time
    By rjuyal in forum Advanced Java
    Replies: 1092
    Last Post: 06-23-2012, 08:55 AM
  2. Replies: 21
    Last Post: 08-01-2008, 11:13 AM
  3. Hello, first time here.
    By ludragon in forum Introductions
    Replies: 2
    Last Post: 01-03-2008, 05:03 AM
  4. DataObject with the time given by me
    By garinapavan in forum New To Java
    Replies: 2
    Last Post: 08-07-2007, 06:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •