Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 09-06-2008, 06:33 PM
Member
 
Join Date: Jul 2008
Posts: 14
Rep Power: 0
Fireking is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 09-06-2008, 08:18 PM
roots's Avatar
Moderator
 
Join Date: Jan 2008
Location: Dallas
Posts: 289
Rep Power: 2
roots is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 09-06-2008, 09:20 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 3,189
Rep Power: 5
Fubarable is on a distinguished road
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:
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 09-07-2008, 01:30 AM
Senior Member
 
Join Date: Aug 2008
Posts: 302
Rep Power: 1
Supamagier is on a distinguished road
Default
if you are using threads:
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
__________________
check out
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
, 100% made by me.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Quiz Time rjuyal Advanced Java 817 07-01-2009 06:29 PM
i click on it,then first time error page comes,second time click then product page co 82rathi.angara New To Java 21 08-01-2008 11:13 AM
Startup time JavaForums Java Blogs 0 02-04-2008 11:40 AM
Hello, first time here. ludragon Introductions 2 01-03-2008 05:03 AM
DataObject with the time given by me garinapavan New To Java 2 08-07-2007 06:33 PM


All times are GMT +2. The time now is 06:09 AM.



VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org