Link to home
Start Free TrialLog in
Avatar of ocha033
ocha033

asked on

ScreenSaver example

I am creating a program (in Java) that is supposed to draw 100 lines over and over.  I have my timer in place to do this, but I am having problems creating the 100 lines.  I know there must be a way to use the for loop to do this.  Please provide suggestions if any.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Please post your code so far
Avatar of ocha033
ocha033

ASKER

// API's to be used in this program
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ScreenSaver extends JFrame implements ActionListener
{
    // Sets up the title bar
    public ScreenSaver()
    {        
        super("Orlando's ScreenSaver");
       
        setSize(433, 330);
        setVisible(true);
    }
   
    public void ReDrawLines()
    {
        Timer timer = new Timer(1000, this);
        timer.start();
    }
   
    public void paint(Graphics g)
    {
        super.paint(g);
       
       ReDrawLines();  
       
       //for (int i = 1; i <= 1000; i++)
      g.setColor(Color.black);
      g.drawLine(23, 5, -13, 70);
     
     
    }
   
    public void actionPerformed(ActionEvent e)
    {      
      repaint();
    }
   
    public static void main(String args[])
    {
       
        ScreenSaver app = new ScreenSaver();
       
        app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      
       
    }
}
are you wanting to draw the 100 lines on the screen at the same time? or one at a time. Also are you wanting to make them appear in different positions?
You wouldn't call ReDrawLines (method names should start with lower case letter btw) in paint. Simply start the timer at the end of the constructor. Aren't you meant to be drawing lines progressively down the canvas?
Avatar of ocha033

ASKER

I want the lines to print diagonally from the left corner one at a time, then after the last line is created, the screen is cleared and the process occurs again.  The user will stop the program simply by closing this application.
Usually the best way to go about something like this is to use Double Buffering, its fairly simple and reduces screen flicker, Also implementing the Runnable interface is also used for most type of games written in Java.

Try this:
// API's to be used in this program
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ScreenSaver extends JFrame implements Runnable
{
     int i = 0;
     Image offscreenImage;
     Graphics offscr;
     Thread t;

   public ScreenSaver()
   {        
       super("Orlando's ScreenSaver");
       
       setSize(433, 330);
       setVisible(true);
       
       //Creates double buffering to reduce flickering
       offscreenImage = this.createImage(433, 330);
       offscr = offscreenImage.getGraphics();
       
       t = new Thread(this);
       t.start();
   }

   public void run()
   {
       
     while(true)
     {
        i = i+10;
       
        try
        {
          //higher number longer it pauses 1000 = 1 sec
          t.sleep(250);    
        }
        catch(InterruptedException ie)
        {}
        repaint();
    }
   }
   
   public void paint(Graphics g)
   {
     
     offscr.setColor(Color.black);
     offscr.drawLine(0, 10 + i, 10 + i, 0);    
     
     //when the lines are greater than the window size - reset
     if(i > this.size().width)//the greater value between width and height
          {
               i = 0;
               offscreenImage.flush();    
               offscreenImage = this.createImage(this.size().width,this.size().height);
               offscr.dispose();
               offscr = offscreenImage.getGraphics();
          }
 
     g.drawImage(offscreenImage, 0, 0, this);
   }

   public void update(Graphics g)
   {
         paint(g);
   }
   
   public static void main(String args[])
   {
       
       ScreenSaver app = new ScreenSaver();
       
       app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      
       
   }
}
Even though a screen saver isnt a game it still uses some of the same principles.
ASKER CERTIFIED SOLUTION
Avatar of Zeroshade
Zeroshade

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ocha033

ASKER

Are there any books that you can recommend for Java?  At this time I am referring to the "Java How To Program" 4th Edition by Deitel & Deitel.  The book is okay but doesn't reinforce its concepts enough.  
Theres a textbook called Big Java that i hear is very good