Screen Saver


  1. Use the template below and fill in the required detail to write a program that acts as a screen saver.
    import java.awt.*;
    import java.applet.Applet;
    
    public class ScreenSaver extends Applet {
        
        // These are instance variables, so they can be used
        // everywere inside this class.
        int x1, x2, y1, y2;  // the coordinates of the line
        int colour;          // the colour of the line
        
        public void paint(Graphics g) {
            
            // repeat 100 times
            for (int i= 0; i<100; i++) {
                
                // write code here that calls methods random
                // and chooseColour
                // and then draws a line using x1, x2, y1, and y2
                
                
            }
        }
        
        // Sets the instance variables.
        public void random() {
            
            // assigns to x an int between 0 and 599
            x1= (int)Math.floor(600*Math.random());
            
            // write code here to assign a different random value 
            // between 0 and 599 to each of x2, y1, and y2
            
            
            // write code here to assign a random value (between
            // two values of your choice) to colour
            
            
        }
        
        // Sets the drawing colour.
        public void chooseColour(Graphics g) {
            
            // write code here which uses a switch statement on
            // colour to set the drawing colour (e.g., for the
            // case where colour is 0, you can choose to set
            // the drawing colour to blue with the code:
            // g.setColor(Color.blue);)
            
            
        }
    }
  2. Modify the above program so that it draws something other than lines. For example: circles, ovals, houses, people, or whatever you choose.