CSC 161 Lab 2-1 and Project 1: Inheritance and Polymorphism

In this lab you will work Inheritance and polymorphism. You will create an array of 4 JFrame objects, and set the array entries to point to objects of different classes that derive, or subclass from, JFrame. These classes will all override the setVisible method to do different things. When you call the setVisible methods on the different subclasses, you will see that even though you are using a JFrame reference to call the setVisible method, different implementations of the method are being called. The version of a method called is determined by the type of object, not the type of the reference.

1. Create a Java Project called YourLastName_Lab2_1. Add a definition for a class called FrameDisplayButtonListener, and have this class implement the action listener method:

package lab2_1;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Main 
{
    public static void main(String[] args) 
    {
      
        
    }
}

class FrameDisplayButtonListener implements ActionListener
{
    public void actionPerformed(ActionEvent e) 
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }    
}

2. Modify the FrameDisplayButtonListener class to add a constructor that takes a single parameter of type JFrame []. The constructor will store this parameter in an instance field of the class so that it will be available to the actionPerformed method. The idea is that this listener will cycle through the JFrame objects each time it is called, making the ``current" JFrame visible while hiding the others.

3. Add an integer field currentFramePosto the class to keep track of the current frame that should be shown. You can initialize this field to -1.

4. Modify the actionPerformed method so it checks the currentFramePos. If this value is -1, it is set to 0 to make the current frame be the one at position 0. If the currentFramePos is not -1, then there is already a frame showing, so that frame is hidden. Then, currentFrame is circularly incremented by 1, and the frame at that new position is made visible.

5. Add a new class called SizedFrame that extends JFrame with a default constructor that sets the size of the frame to 300 x 100.

class SizedFrame extends JFrame
{
    public SizedFrame()
    { 
       super("SizedFrame");
       SetSize(300, 100); 
    }
}

6. Add a new class BlueSizedFrame that extends SizedFrame. Override the setVisible method so that it sets background color of the content pane of its frame to blue, and then calls the superclass version of the setVisible method. Finally, the method should call setTitle to set the title to BlueSizedFrame.

Remember that to override a method, you need to redefine it in your subclass with the same return type, same name, and parameter type list.

7. Add a new class BlueExpandingFrame that extends BlueSizedFrame. Override its setVisible method so that increases both the width and height of its frame by 20 pixels, and then calls the superclass version of the setVisible method. Finally, the method should call setTitle to set the title to BlueExpandingFrame.

To increase the width and height of a frame, use the getWidth() and getHeight() methos of the frame to obtain its current dimensions. You can then add 20 to each of the two dimensions and use them in a call to setSize() to set the new size.

8. Now modify the main method according to the following outline. By following the steps outlined in the comments, you will create a frame with a button on it. Furthermore, the button will have a listener so that each time the button is pressed, one of the three frames in the array is set visible.

 public static void main(String[] args)
    {
       // create the program frame
       JFrame mainFrame = new JFrame("Polymorphim Illustrator");
       // create an array of frames to display
       JFrame [] frames = {new SizedFrame(), new BlueSizedFrame(),
                           new BlueExpandingFrame()
                          };

       // create button to cycle through the frames
       

       // create a FrameDisplayListener and add it to the button
       

       // set flow layout in program frame and add the button, pack the frame
      

       // set the defaultCloseOperation and make the frame visiblesetVisible
      
    }

9. Run this program and think about what is happening. The frames are being shown in the actionPerformed method in the listener by calling setVisible through a JFrame reference. Java is ignoring the type of the reference, and instead, is calling the setVisible method in the class of the object being pointed to.

10. Start a new project in Netbeans, and call it Project1, prefixed with your last name. Copy the contents of your the file from Lab2_1 into your new program using Cut and Paste. Next, modify your program so that when the button is pressed, it starts a timer that causes the program to cycle through the frames, displaying each frame for three seconds before moving on to the next frame.

Turn in both the lab and the project. The lab is due Tonight at Midnight, the project is due next Wednesday at Midnight. The project must be commented according to the specified guidelines. The labs do not need a lot of comments.