Welcome to Dream.In.Code
Become a Java Expert!

Join 149,759 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,515 people online right now. Registration is fast and FREE... Join Now!




Simple Java Screen Saver

 
Reply to this topicStart new topic

Simple Java Screen Saver, Need help adding a timer so the program is not so fast

baseballstud154
14 Feb, 2007 - 01:20 PM
Post #1

New D.I.C Head
*

Joined: 1 Nov, 2006
Posts: 18


My Contributions
All I need to do is add a timer to this code that I already have working so that the program will not be so fast. Will someone please help me with this, I have been trying for a long while now and have had no success.
Thanks,

CODE

import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Saver1JPanel extends JPanel
{
   private Random generator = new Random();
  
   // draw lines
   public void paintComponent( Graphics g )
   {
         
         
      super.paintComponent( g ); // call superclass's paintComponent
      
      int x1, y1, x2, y2;
      
      // draw 100 random lines
      for ( int i = 0; i < 100; i++ )
      {
         x1 = generator.nextInt( 700 );
         y1 = generator.nextInt( 700 );
         x2 = generator.nextInt( 700 );
         y2 = generator.nextInt( 700 );

         g.setColor( new Color( generator.nextInt( 256 ),
            generator.nextInt( 256 ), generator.nextInt( 256 ) ) );
         g.drawLine( x1, y1, x2, y2 );
        
        
  
      } // end outer for

      repaint(); // repaint component
   } // end method paintComponent
} // end class Saver1JPanel


CODE

import javax.swing.JFrame;

public class Saver
{
   // execute application
   public static void main( String args[] )
   {
      // create frame for Saver1JPanel
      JFrame frame = new JFrame( " Line ScreenSaver " );
      frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

      // create Saver1JPanel
      Saver1JPanel saver1JPanel = new Saver1JPanel();
      frame.add( saver1JPanel ); // add saver1JPanel to frame
      frame.setSize( 700, 700 ); // set frame size
      frame.setVisible( true ); // display frame
   } // end main
} // end class Saver


User is offlineProfile CardPM
+Quote Post

horace
RE: Simple Java Screen Saver
14 Feb, 2007 - 01:52 PM
Post #2

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 5 times
Dream Kudos: 50
My Contributions
in paintComponent( Graphics g ) you could sleep() for a number of milliseconds after calling repaint, e.g.
CODE

      repaint(); // repaint component
      try { Thread.sleep(100); } catch( InterruptedException e) {}


User is offlineProfile CardPM
+Quote Post

WolfCoder
RE: Simple Java Screen Saver
14 Feb, 2007 - 01:53 PM
Post #3

ギュウ~
Group Icon

Joined: 5 May, 2005
Posts: 3,715



Thanked: 8 times
Dream Kudos: 1450
My Contributions
Before the main loop, have

CODE

// Get current time
timeStep = System.currentTimeMillis();


And then do your stuff.

Then before the loop goes back to start, have

CODE

// Hold to lock at FPS
while(System.currentTimeMillis()-timeStep < 1000/60);


Where 60 is the frames per second to run.

This method also allows you to get the difference in times after the while loop in order to find out how much time it took to render one frame. Using this, you could have a variable that you can multiply all your increments by to get frame-independent speed.

For example, if you divide the number of milliseconds it took to handle the frame by 16.666, the number will equal 1 when the program runs at about 60 FPS. When the frame rate is lower, it takes more milliseconds to render the frame and that factor gets bigger.

Be sure to put it BEFORE repaint however. This is the way video games are timed also. An example is shown in my video game base applet code snippet under JAVA.

This post has been edited by WolfCoder: 14 Feb, 2007 - 02:12 PM
User is online!Profile CardPM
+Quote Post

baseballstud154
RE: Simple Java Screen Saver
14 Feb, 2007 - 02:08 PM
Post #4

New D.I.C Head
*

Joined: 1 Nov, 2006
Posts: 18


My Contributions
Thanks,

That worked great... thank you very much!

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 06:10AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month