?
Solved

ScreenSaver example

Posted on 2003-03-26
10
Medium Priority
?
783 Views
Last Modified: 2012-06-21
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.
0
Comment
Question by:ocha033
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
10 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 8211898
Please post your code so far
0
 

Author Comment

by:ocha033
ID: 8212375
// 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);      
       
    }
}
0
 

Expert Comment

by:Zeroshade
ID: 8212662
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?
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 86

Expert Comment

by:CEHJ
ID: 8212668
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?
0
 

Author Comment

by:ocha033
ID: 8221224
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.
0
 

Expert Comment

by:Zeroshade
ID: 8226954
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);      
       
   }
}
0
 

Expert Comment

by:Zeroshade
ID: 8226961
Even though a screen saver isnt a game it still uses some of the same principles.
0
 

Accepted Solution

by:
Zeroshade earned 84 total points
ID: 8227462
oops in paint change this:
if(i > this.size().width)//the greater value between width and height
         {

to this:
if(i > this.size().width * 2)//the greater value between width and height
         {
0
 

Author Comment

by:ocha033
ID: 8228749
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.  
0
 

Expert Comment

by:Zeroshade
ID: 8230109
Theres a textbook called Big Java that i hear is very good
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month13 days, 21 hours left to enroll

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question