Kinesia Online Course
Object Oriented Programming
Kinesia LLC, 2003
    1. A Little Taste of Java
    2. Abstraction and Modeling
    3. Objects and Classes
    4. Object Interactions
    5. UML and Relationships between Objects
    6. Collection of Objects
    7. Some Final Concepts
    8. Object Modeling and Use Cases
    9. Modeling of Dynamic Behavaior and Sequence Diagrams
    10. A Deeper Look at Java
    11. Java Layout
    12. Java Events
    
    The world would take people out of the slums.
    Christ takes the slums out of people, and then
    they take themselves out of the slums.
    				Ezra Taft Benson
    
    
    A Deeper Look at Java
    1. Components and Containers

    2. GUI component -- an object that represents a screen element that is used to display information or to allow the user to interact with the program in a certain way. e.g. labels, buttons, text fields, scroll bars, menus

    3. Abstract Windowing Toolkit ( AWT ) -- original Java GUI package, appearance is system dependent

    4. Swing -- contemporary Java GUI package, appearance system independent

    5. Container -- a special type of component that is used to hold and organize other components. e.g. frames and panels

    6. Frames and Panels
      • frame -- a container that is used to display GUI-based Java applications; displayed as a separate window with its own title bar
        defined by JFrame class

      • panel -- unlike a frame, it cannot be displayed on its own; it must be added to another container for it to be displayed

      • all visible elements of a Java interface are displayed in a frame's content pane

      //********************************************************************
      //  Authority.java       Author: Lewis/Loftus
      //
      //  Demonstrates the use of frames, panels, and labels.
      //********************************************************************
      
      import java.awt.*;
      import javax.swing.*;
      
      public class Authority
      {
         //-----------------------------------------------------------------
         //  Displays some words of wisdom.
         //-----------------------------------------------------------------
         public static void main (String[] args)
         {
            JFrame frame = new JFrame ("Authority");
      
            frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
      
            JPanel primary = new JPanel();
            primary.setBackground (Color.yellow);
            primary.setPreferredSize (new Dimension(250, 75));
      
            JLabel label1 = new JLabel ("Question authority,");
            JLabel label2 = new JLabel ("but raise your hand first.");
      
            primary.add (label1);
            primary.add (label2);
      
            frame.getContentPane().add(primary);
            frame.pack();
            frame.setVisible(true);
         }
      }
      

      • The pack() method of the frame sets its size appropriately based on its content.
      • setVisible() method causes the frame to be displayed on the monitor screen.

    7. Nested Panels

      //********************************************************************
      //  NestedPanels.java       Author: Lewis/Loftus
      //
      //  Demonstrates a basic componenet hierarchy.
      //********************************************************************
      
      import java.awt.*;
      import javax.swing.*;
      
      public class NestedPanels
      {
         //-----------------------------------------------------------------
         //  Presents two colored panels nested within a third.
         //-----------------------------------------------------------------
         public static void main (String[] args)
         {
            JFrame frame = new JFrame ("Nested Panels");
            frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
      
            // Set up first subpanel
            JPanel subPanel1 = new JPanel();
            subPanel1.setPreferredSize (new Dimension(150, 100));
            subPanel1.setBackground (Color.green);
            JLabel label1 = new JLabel ("One");
            subPanel1.add (label1);
      
            // Set up second subpanel
            JPanel subPanel2 = new JPanel();
            subPanel2.setPreferredSize (new Dimension(150, 100));
            subPanel2.setBackground (Color.red);
            JLabel label2 = new JLabel ("Two");
            subPanel2.add (label2);
      
            // Set up primary panel
            JPanel primary = new JPanel();
            primary.setBackground (Color.blue);
            primary.add (subPanel1);
            primary.add (subPanel2);
      
            frame.getContentPane().add(primary);
            frame.pack();
            frame.setVisible(true);
         }
      }
      

    8. Images

      //********************************************************************
      //  LabelDemo.java       Author: Lewis/Loftus
      //
      //  Demonstrates the use of image icons in labels.
      //********************************************************************
      
      import java.awt.*;
      import javax.swing.*;
      
      public class LabelDemo
      {
         //-----------------------------------------------------------------
         //  Creates and displays the primary application frame.
         //-----------------------------------------------------------------
         public static void main (String[] args)
         {
            JFrame frame = new JFrame ("Label Demo");
            frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
      
            ImageIcon icon = new ImageIcon ("devil.gif");
      
            JLabel label1, label2, label3;
      
            label1 = new JLabel ("Devil Left", icon, SwingConstants.CENTER);
      
            label2 = new JLabel ("Devil Right", icon, SwingConstants.CENTER);
            label2.setHorizontalTextPosition (SwingConstants.LEFT);
            label2.setVerticalTextPosition (SwingConstants.BOTTOM);
      
            label3 = new JLabel ("Devil Above", icon, SwingConstants.CENTER);
            label3.setHorizontalTextPosition (SwingConstants.CENTER);
            label3.setVerticalTextPosition (SwingConstants.BOTTOM);
      
            JPanel panel = new JPanel();
            panel.setBackground (Color.cyan);
            panel.setPreferredSize (new Dimension (200, 250));
            panel.add (label1);
            panel.add (label2);
            panel.add (label3);
      
            frame.getContentPane().add(panel);
            frame.pack();
            frame.setVisible(true);
         }
      }
      

    9. Graphical Objects

      //********************************************************************
      //  SmilingFace.java       Author: Lewis/Loftus
      //
      //  Demonstrates the use of a separate panel class.
      //********************************************************************
      
      import javax.swing.JFrame;
      
      public class SmilingFace
      {
         //-----------------------------------------------------------------
         //  Creates the main frame of the program.
         //-----------------------------------------------------------------
         public static void main (String[] args)
         {
            JFrame frame = new JFrame ("Smiling Face");
            frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
      
            SmilingFacePanel panel = new SmilingFacePanel();
      
            frame.getContentPane().add(panel);
      
            frame.pack();
            frame.setVisible(true);
         }
      }
      //********************************************************************
      //  SmilingFacePanel.java       Author: Lewis/Loftus
      //
      //  Demonstrates the use of a separate panel class.
      //********************************************************************
      
      import javax.swing.JPanel;
      import java.awt.*;
      
      public class SmilingFacePanel extends JPanel
      {
         private final int BASEX = 120, BASEY = 60; // base point for head
      
         //-----------------------------------------------------------------
         //  Constructor: Sets up the main characteristics of this panel.
         //-----------------------------------------------------------------
         public SmilingFacePanel ()
         {
            setBackground (Color.blue);
            setPreferredSize (new Dimension(320, 200));
            setFont (new Font("Arial", Font.BOLD, 16));
         }
      
         //-----------------------------------------------------------------
         //  Draws a face.
         //-----------------------------------------------------------------
         public void paintComponent (Graphics page)
         {
            super.paintComponent (page);
      
            page.setColor (Color.yellow);
            page.fillOval (BASEX, BASEY, 80, 80);  // head
            page.fillOval (BASEX-5, BASEY+20, 90, 40);  // ears
      
            page.setColor (Color.black);
            page.drawOval (BASEX+20, BASEY+30, 15, 7);  // eyes
            page.drawOval (BASEX+45, BASEY+30, 15, 7);
      
            page.fillOval (BASEX+25, BASEY+31, 5, 5);   // pupils
            page.fillOval (BASEX+50, BASEY+31, 5, 5);
      
            page.drawArc (BASEX+20, BASEY+25, 15, 7, 0, 180);  // eyebrows
            page.drawArc (BASEX+45, BASEY+25, 15, 7, 0, 180);
      
            page.drawArc (BASEX+35, BASEY+40, 15, 10, 180, 180);  // nose
            page.drawArc (BASEX+20, BASEY+50, 40, 15, 180, 180);  // mouth
      
            page.setColor (Color.white);
            page.drawString ("Always remember that you are unique!",
                             BASEX-105, BASEY-15);
            page.drawString ("Just like everyone else.", BASEX-45, BASEY+105);
         }
      }
      

    10. Graphical User Interfaces

      Buttons

      A push button is a component that allows a user to initiate an action with a press of the mouse; defined by JButton class.

      A JButton generates an action event when it is pushed. Thus the listener class we write will be an action event listener.

      //********************************************************************
      //  PushCounter.java       Authors: Lewis/Loftus
      //
      //  Demonstrates a graphical user interface and an event listener.
      //********************************************************************
      
      import javax.swing.JFrame;
      
      public class PushCounter
      {
         //-----------------------------------------------------------------
         //  Creates the main program frame.
         //-----------------------------------------------------------------
         public static void main (String[] args)
         {
            JFrame frame = new JFrame ("Push Counter");
            frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
      
            frame.getContentPane().add(new PushCounterPanel());
      
            frame.pack();
            frame.setVisible(true);
         }
      }
      
      //********************************************************************
      //  PushCounterPanel.java       Authors: Lewis/Loftus
      //
      //  Demonstrates a graphical user interface and an event listener.
      //********************************************************************
      
      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;
      
      public class PushCounterPanel extends JPanel
      {
         private int count;
         private JButton push;
         private JLabel label;
      
         //-----------------------------------------------------------------
         //  Constructor: Sets up the GUI.
         //-----------------------------------------------------------------
         public PushCounterPanel ()
         {
            count = 0;
      
            push = new JButton ("Push Me!");
            push.addActionListener (new ButtonListener());
      
            label = new JLabel ("Pushes: " + count);
      
            add (push);
            add (label);
      
            setPreferredSize (new Dimension(300, 40));
            setBackground (Color.cyan);
         }
      
         //*****************************************************************
         //  Represents a listener for button push (action) events.
         //*****************************************************************
         private class ButtonListener implements ActionListener
         {
            //--------------------------------------------------------------
            //  Updates the counter and label when the button is pushed.
            //--------------------------------------------------------------
            public void actionPerformed (ActionEvent event)
            {
               count++;
               label.setText("Pushes: " + count);
            }
         }
      }
      

      //********************************************************************
      //  LeftRight.java       Authors: Lewis/Loftus
      //
      //  Demonstrates the use of one listener for multiple buttons.
      //********************************************************************
      
      import javax.swing.JFrame;
      
      public class LeftRight
      {
         //-----------------------------------------------------------------
         //  Creates the main program frame.
         //-----------------------------------------------------------------
         public static void main (String[] args)
         {
            JFrame frame = new JFrame ("Left Right");
            frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
      
            frame.getContentPane().add(new LeftRightPanel());
      
            frame.pack();
            frame.setVisible(true);
         }
      }
      
      //********************************************************************
      //  LeftRightPanel.java       Authors: Lewis/Loftus
      //
      //  Demonstrates the use of one listener for multiple buttons.
      //********************************************************************
      
      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;
      
      public class LeftRightPanel extends JPanel
      {
         private JButton left, right;
         private JLabel label;
         private JPanel buttonPanel;
      
         //-----------------------------------------------------------------
         //  Constructor: Sets up the GUI.
         //-----------------------------------------------------------------
         public LeftRightPanel ()
         {
            left = new JButton ("Left");
            right = new JButton ("Right");
      
            ButtonListener listener = new ButtonListener();
            left.addActionListener (listener);
            right.addActionListener (listener);
      
            label = new JLabel ("Push a button");
      
            buttonPanel = new JPanel();
            buttonPanel.setPreferredSize (new Dimension(200, 40));
            buttonPanel.setBackground (Color.blue);
            buttonPanel.add (left);
            buttonPanel.add (right);
      
            setPreferredSize (new Dimension(200, 80));
            setBackground (Color.cyan);
            add (label);
            add (buttonPanel);
         }
      
         //*****************************************************************
         //  Represents a listener for both buttons.
         //*****************************************************************
         private class ButtonListener implements ActionListener
         {
            //--------------------------------------------------------------
            //  Determines which button was pressed and sets the label
            //  text accordingly.
            //--------------------------------------------------------------
            public void actionPerformed (ActionEvent event)
            {
               if (event.getSource() == left)
                  label.setText("Left");
               else
                  label.setText("Right");
            }
         }
      }
      

      Text Fields

      A text field is an object of the JTextField class.

      //********************************************************************
      //  Fahrenheit.java       Author: Lewis/Loftus
      //
      //  Demonstrates the use of text fields.
      //********************************************************************
      
      import javax.swing.JFrame;
      
      public class Fahrenheit
      {
         //-----------------------------------------------------------------
         //  Creates and displays the temperature converter GUI.
         //-----------------------------------------------------------------
         public static void main (String[] args)
         {
            JFrame frame = new JFrame ("Fahrenheit");
            frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
      
            FahrenheitPanel panel = new FahrenheitPanel();
      
            frame.getContentPane().add(panel);
            frame.pack();
            frame.setVisible(true);
         }
      }
      
      //********************************************************************
      //  FahrenheitPanel.java       Author: Lewis/Loftus
      //
      //  Demonstrates the use of text fields.
      //********************************************************************
      
      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;
      
      public class FahrenheitPanel extends JPanel
      {
         private JLabel inputLabel, outputLabel, resultLabel;
         private JTextField fahrenheit;
      
         //-----------------------------------------------------------------
         //  Constructor: Sets up the main GUI components.
         //-----------------------------------------------------------------
         public FahrenheitPanel()
         {
            inputLabel = new JLabel ("Enter Fahrenheit temperature:");
            outputLabel = new JLabel ("Temperature in Celsius: ");
            resultLabel = new JLabel ("---");
      
            fahrenheit = new JTextField (5);
            fahrenheit.addActionListener (new TempListener());
      
            add (inputLabel);
            add (fahrenheit);
            add (outputLabel);
            add (resultLabel);
      
            setPreferredSize (new Dimension(300, 75));
            setBackground (Color.yellow);
         }
      
         //*****************************************************************
         //  Represents an action listener for the temperature input field.
         //*****************************************************************
         private class TempListener implements ActionListener
         {
            //--------------------------------------------------------------
            //  Performs the conversion when the enter key is pressed in
            //  the text field.
            //--------------------------------------------------------------
            public void actionPerformed (ActionEvent event)
            {
               int fahrenheitTemp, celsiusTemp;
      
               String text = fahrenheit.getText();
      
               fahrenheitTemp = Integer.parseInt (text);
               celsiusTemp = (fahrenheitTemp-32) * 5/9;
      
               resultLabel.setText (Integer.toString (celsiusTemp));
            }
         }
      }
      

      Dialog Boxes

      A dialog box is a grapical window that pops up on any currently active windows so that the user can interact with it.

      //********************************************************************
      //  EvenOdd.java       Author: Lewis/Loftus
      //
      //  Demonstrates the use of the JOptionPane class.
      //********************************************************************
      
      import javax.swing.JOptionPane;
      
      public class EvenOdd
      {
         //-----------------------------------------------------------------
         //  Determines if the value input by the user is even or odd.
         //  Uses multiple dialog boxes for user interaction.
         //-----------------------------------------------------------------
         public static void main (String[] args)
         {
            String numStr, result;
            int num, again;
      
            do 
            {
               numStr = JOptionPane.showInputDialog ("Enter an integer: ");
      
               num = Integer.parseInt(numStr);
      
               result = "That number is " + ((num%2 == 0) ? "even" : "odd");
      
               JOptionPane.showMessageDialog (null, result);
      
               again = JOptionPane.showConfirmDialog (null, "Do Another?");
            }
            while (again == JOptionPane.YES_OPTION);
         }
      }
      

      Check Boxes

      A check box is a button that can be toggled on or off using the mouse, indicating that a particular boolean condition is set or unset.

      //********************************************************************
      //  StyleOptions.java       Author: Lewis/Loftus
      //
      //  Demonstrates the use of check boxes.
      //********************************************************************
      
      import javax.swing.JFrame;
      
      public class StyleOptions
      {
         //-----------------------------------------------------------------
         //  Creates and presents the program frame.
         //-----------------------------------------------------------------
         public static void main (String[] args)
         {
            JFrame frame = new JFrame ("Style Options");
            frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
      
            StyleOptionsPanel panel = new StyleOptionsPanel();
            frame.getContentPane().add (panel);
      
            frame.pack();
            frame.setVisible(true);
         }
      }
      //********************************************************************
      //  StyleOptionsPanel.java       Author: Lewis/Loftus
      //
      //  Demonstrates the use of check boxes.
      //********************************************************************
      
      import javax.swing.*;
      import java.awt.*;
      import java.awt.event.*;
      
      public class StyleOptionsPanel extends JPanel
      {
         private JLabel saying;
         private JCheckBox bold, italic;
      
         //-----------------------------------------------------------------
         //  Sets up a panel with a label and some check boxes that
         //  control the style of the label's font.
         //-----------------------------------------------------------------
         public StyleOptionsPanel()
         {
            saying = new JLabel ("Say it with style!");
            saying.setFont (new Font ("Helvetica", Font.PLAIN, 36));
      
            bold = new JCheckBox ("Bold");
            bold.setBackground (Color.cyan);
            italic = new JCheckBox ("Italic");
            italic.setBackground (Color.cyan);
      
            StyleListener listener = new StyleListener();
            bold.addItemListener (listener);
            italic.addItemListener (listener);
      
            add (saying);
            add (bold);
            add (italic);
      
            setBackground (Color.cyan);
            setPreferredSize (new Dimension(300, 100));
         }
      
         //*****************************************************************
         //  Represents the listener for both check boxes.
         //*****************************************************************
         private class StyleListener implements ItemListener
         {
            //--------------------------------------------------------------
            //  Updates the style of the label font style.
            //--------------------------------------------------------------
            public void itemStateChanged (ItemEvent event)
            {
               int style = Font.PLAIN;
      
               if (bold.isSelected())
                  style = Font.BOLD;
      
               if (italic.isSelected())
                  style += Font.ITALIC;
      
               saying.setFont (new Font ("Helvetica", style, 36));
            }
         }
      }
      

      Radio Buttons

      A radio button is used with other radio buttons to provide a set of mutually exclusive options.

      //********************************************************************
      //  QuoteOptions.java       Author: Lewis/Loftus
      //
      //  Demonstrates the use of radio buttons.
      //********************************************************************
      
      import javax.swing.JFrame;
      
      public class QuoteOptions
      {
         //-----------------------------------------------------------------
         //  Creates and presents the program frame.
         //-----------------------------------------------------------------
         public static void main (String[] args)
         {
            JFrame frame = new JFrame ("Quote Options");
            frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
      
            QuoteOptionsPanel panel = new QuoteOptionsPanel();
            frame.getContentPane().add (panel);
      
            frame.pack();
            frame.setVisible(true);
         }
      }
      
      //********************************************************************
      //  QuoteOptionsPanel.java       Author: Lewis/Loftus
      //
      //  Demonstrates the use of radio buttons.
      //********************************************************************
      
      import javax.swing.*;
      import java.awt.*;
      import java.awt.event.*;
      
      public class QuoteOptionsPanel extends JPanel
      {
         private JLabel quote;
         private JRadioButton comedy, philosophy, carpentry;
         private String comedyQuote, philosophyQuote, carpentryQuote;
      
         //-----------------------------------------------------------------
         //  Sets up a panel with a label and a set of radio buttons
         //  that control its text.
         //-----------------------------------------------------------------
         public QuoteOptionsPanel()
         {
            comedyQuote = "Take my wife, please.";
            philosophyQuote = "I think, therefore I am.";
            carpentryQuote = "Measure twice. Cut once.";
      
            quote = new JLabel (comedyQuote);
            quote.setFont (new Font ("Helvetica", Font.BOLD, 24));
      
            comedy = new JRadioButton ("Comedy", true);
            comedy.setBackground (Color.green);
            philosophy = new JRadioButton ("Philosophy");
            philosophy.setBackground (Color.green);
            carpentry = new JRadioButton ("Carpentry");
            carpentry.setBackground (Color.green);
      
            ButtonGroup group = new ButtonGroup();
            group.add (comedy);
            group.add (philosophy);
            group.add (carpentry);
      
            QuoteListener listener = new QuoteListener();
            comedy.addActionListener (listener);
            philosophy.addActionListener (listener);
            carpentry.addActionListener (listener);
      
            add (quote);
            add (comedy);
            add (philosophy);
            add (carpentry);
      
            setBackground (Color.green);
            setPreferredSize (new Dimension(300, 100));
         }
      
         //*****************************************************************
         //  Represents the listener for all radio buttons
         //*****************************************************************
         private class QuoteListener implements ActionListener
         {
            //--------------------------------------------------------------
            //  Sets the text of the label depending on which radio
            //  button was pressed.
            //--------------------------------------------------------------
            public void actionPerformed (ActionEvent event)
            {
               Object source = event.getSource();
      
               if (source == comedy)
                  quote.setText (comedyQuote);
               else
                  if (source == philosophy)
                     quote.setText (philosophyQuote);
                  else
                     quote.setText (carpentryQuote);
            }
         }
      }