4.8 Week 8 - JButtons, JFrames, ...  4 Operator Precedence and Associativity4.6 Week 6 - OOP -- Java Object Terminology 4.7 Week 7 - Java Object Terminology 3 - awt & swing

4.7 Week 7 - Java Object Terminology 3 - awt & swing

4.7.1 Reading and Web Sites

  1. Reading van der Linden Chapter 8, Interfaces
  2. Look over van der Linden Chapter 19, GUI Basics and Event Handling

4.7.2 Notes

  1. method subsumption
    What happens when a type is "promoted" prior to the call to the method. ie: The call uses an int but the method requires a double.
    /* Louis Taber   Pima Community College  */
    /* Subsumption example      Feb 24, 2003 */
    
    class Subsumption
    {
    public Subsumption(int a, double b) 
     { 
     System.out.println( " int a, double b ");
     }
    public Subsumption(double a, int b)
     {
     System.out.println( " double a, int b ");
     }
    public Subsumption(double a, double b, double c)
     {
     System.out.println( " double a, double b, double c ");
     }
      
    
    public static void main(String [] args)
    {
    Subsumption a = new Subsumption(1,2.0);
    Subsumption b = new Subsumption(2.0,1);
    
    Subsumption c = new Subsumption((double)1,2);
    // Subsumption c = new Subsumption(1,2);
    // Subsumption.java:21: reference to Subsumption is ambiguous, 
    //   both method 
    //    Subsumption(int,double) in Subsumption 
    //   and method 
    //    Subsumption(double,int) in Subsumption   match
    
    Subsumption d = new Subsumption(1,2,3);
    }
    
    }
    
    

    Note: The compiler complained when it could not find a specific method to call. I have commented out the call and edited in the error message from the compiler.

    And the output of the above program:

     int a, double b 
     double a, int b 
     double a, int b 
     double a, double b, double c 
    
    
  2. field
    The name of data in an object. It is stored on the heap. The memory is allocated when the object is instantiated with new.
  3. interface
    1. An interface describes a public set of methods that must exist in a class that implements the interface.
    2. A class that implements an interface must implement all of the variables and method signatures that are contained in an interface.
    3. Allows a class to implement/be a certain "type" of class without inheritance.
    4. A new class can only extend one class.
  4. abstract class
    1. An incomplete class that requires further specialization.
    2. Must be extended to be useful.
    3. Can contain actual code.
  5. Swing
    1. swing is in javax.swing
    2. Most swing components use the awt (Advanced Windowing Toolkit) parts and pieces.
    3. Most swing components are prefixed with a "J".
    4. There are OVER 200 method calls.

4.7.3 Lab Assignment - Interfaces

In this lab set up the following classes and methods related to illumination. (If you have other plans, please let me know. There is nothing special about this example.)
  1. Implement a parent class: Illuiminator. Implement the methods getName(), getPower(), and getIllumination()
  2. Define an interface, UsesElectricity with the method getPower. implement the UsesElectricity interface in the LED, and Incandescent classes.
  3. Your main() program will need to define an array or linked list with of type Illuminator. Place the following objects and data in the array or on the linked list.
  4. Extend class Illuminator in classes LED, Incandescent, and Tritium.
  5. Note: for UsesElectricity, power = Voltage * Current.
  6. Note: illumination = power * efficiency.
    Class Name Efficiency Voltage Current Power
    LED Red .17 3.2v .01A
    LED Green .21 3.6v .01A
    Incandescent Standard .03 120v .80A
    Incandescent Halogen .04 120v .80A
    Tritium Tritium .05 .05W
  7. Sequence through the array or linked list printing out the name, illumination, and power for each type of "Illuminator".
  8. Turn in a copy of your JavaTMcode and the resulting output.

4.7.4 Lab Assignment - Getting started with javax.swing

Compile run and modify a small Java/Swing program.
  1. First compile and run the following program:
    /* Louis Taber, PCC 2/25/2003 */
    
    import javax.swing.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.event.WindowEvent;
    import java.awt.event.WindowListener;
    import java.awt.*;
    
    public class Window
    {
    static JFrame aWindow = new JFrame( " Title - One");
    
    public static void main(String[] args)
      {
      aWindow.setBounds( 50, 100, 400, 150);
      aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      aWindow.getContentPane().setBackground(Color.red);
    
      aWindow.setVisible(true);
      
      WindowClose close = new WindowClose(aWindow);
      aWindow.addWindowListener( close );
      }
    }
    
    
    
    

    and

    /* Louis Taber,  PCC  2/25/2003 */
    
    import javax.swing.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.event.WindowEvent;
    import java.awt.event.WindowListener;
    import java.awt.Component;
    import java.awt.geom.Point2D;
    import java.awt.*;
    
    
    public class WindowClose implements WindowListener 
    {
    
    JFrame aWindow;
    Point pt = null;
    
    WindowClose(JFrame aWindow)
      {
      this.aWindow = aWindow;
      }
    
    public void windowClosing(WindowEvent e) 
      {System.out.println("windowClosing");}
    public void windowClosed(WindowEvent e) 
      {System.out.println("windowClosed"); }
    public void windowOpened(WindowEvent e) 
      {System.out.println("windowOpened"); }
    public void windowIconified(WindowEvent e) 
      {System.out.println("windowIconified");}
    public void windowDeiconified(WindowEvent e) 
      {System.out.println("windowDeiconified");}
    public void windowActivated(WindowEvent e) 
      { 
      System.out.println("windowActivated");
      if( pt == null ) pt = new Point();
      pt =  aWindow.getLocation();
      Point pt = new Point( aWindow.getLocation());
      System.out.println( pt );
      aWindow.setVisible(true);  
      }
    public void windowDeactivated(WindowEvent e) 
      { System.out.println("windowDeactivated");}
    }
    
    
    
  2. Then modify the program to print out the x and y coordinates that the mouse/pointer are on when the left mouse button is pushed within the window.
Please show me your program running.
Instructor: ltaber@pima.edu ** My new Home at GeoApps in Tucson ** The Pima College Site

4.8 Week 8 - JButtons, JFrames, ...  4 Operator Precedence and Associativity4.6 Week 6 - OOP -- Java Object Terminology 4.7 Week 7 - Java Object Terminology 3 - awt & swing