DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Andrew Osborne Guest

    Delaying processing using Timer


    I'm a student taking a Java course where I had to build a fake screen saver
    that draws line and refreshes itself after 1 second. I accomplished this
    task. But I don't like how fast the line are drawn on the screen. I was
    able to slow how quickly the line are being drawn by inserting a for loop
    that does nothing. There has to be a better way. Any help would be appreciated.
    Please note, this was not part of the assignment. I'm just curious. I
    say this because I see people post her looking for someone to complete their
    assignments for them.

    Code
    import javax.swing.*;
    import javax.swing.Timer;
    import java.awt.*;
    import java.awt.event.*;

    public class ElevenTwentyOne extends JFrame implements ActionListener{
    private Timer timer = new Timer(1000,this);
    private int counter = 0;

    public ElevenTwentyOne()
    {
    super("Screen Saver");

    setBackground(Color.black);
    setSize(640, 480); //Standard windows full screen
    show();

    timer.start();

    }//end ElevenTwentyOne constructor


    public void actionPerformed(ActionEvent e)
    {
    repaint();

    }//end actionPerformed

    public void paint(Graphics g)
    {
    int X1,
    Y1,
    X2,
    Y2;

    for(int i = 0; i < 100; i++)
    {
    X1 = 1 + (int) (Math.random() * 100);
    Y1 = 1 + (int) (Math.random() * 100);
    X2 = 1 + (int) (Math.random() * 800);
    Y2 = 1 + (int) (Math.random() * 800);

    g.setColor(Color.blue);
    g.drawLine(X1 ,Y1,X2,Y2);
    for(int b = 0; b < 10000000; b++)
    {
    //slow things down... find better way
    }

    }
    g.clearRect(0,0,640,480);

    }//end paint


    public static void main(String arg[])
    {
    ElevenTwentyOne app = new ElevenTwentyOne();

    app.addWindowListener(
    new WindowAdapter(){
    public void windowClosing(WindowEvent e){
    System.exit(0);
    }//end windowClosing
    }//end WindowAdapter
    );
    }//end main
    }//end class ElvenTwentyOne

  2. #2
    Paul Clapham Guest

    Re: Delaying processing using Timer

    If you want your program to delay for 1 second (1000 milliseconds), just
    write

    Thread.currentThread().sleep(1000);

    But in your case that may not work. As you may or may not have learned in
    your course, drawing of Swing controls runs in a separate thread, and you
    really want that thread to be the one that executes its "sleep" method. To
    do that, I think you'd have to use the following code:

    SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    Thread.currentThread().sleep(1000);
    }
    });

    which puts the "sleep" command into Swing's event-dispatching thread.

    PC2

    "Andrew Osborne" <aosborne@home.com> wrote in message
    news:3abfb98e$1@news.devx.com...
    >
    > I'm a student taking a Java course where I had to build a fake screen

    saver
    > that draws line and refreshes itself after 1 second. I accomplished this
    > task. But I don't like how fast the line are drawn on the screen. I was
    > able to slow how quickly the line are being drawn by inserting a for loop
    > that does nothing. There has to be a better way. Any help would be

    appreciated.




Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links