Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 897
  • Last Modified:

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.
0
ocha033
Asked:
ocha033
  • 5
  • 3
  • 2
1 Solution
 
CEHJCommented:
Please post your code so far
0
 
ocha033Author Commented:
// 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
 
ZeroshadeCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
CEHJCommented:
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
 
ocha033Author Commented:
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
 
ZeroshadeCommented:
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
 
ZeroshadeCommented:
Even though a screen saver isnt a game it still uses some of the same principles.
0
 
ZeroshadeCommented:
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
 
ocha033Author Commented:
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
 
ZeroshadeCommented:
Theres a textbook called Big Java that i hear is very good
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now