CIS279-BASFORD-06/06/01

Java Programming
CIS279
Tori Basford
tori@bly.net

Assignment(s):
CIS 279 Homework 1, Part a: ColorBar

Exercise(s):

  1. None

Review: Java Basics, Objects
Java is polymorphic. You can have several methods with the same name, but with different inputs.

There are three places methods and variables can be:

  1. In a class (static)
  2. In an object (object)
  3. Inside a method (local)
Methods can not exist in other methods.

To access a local variable or method, you simply use it's name. To access an object variable, you use the name of the object and the name of the variable (or method). To access a class variable, you use the name of the class and the name of the variable.

In Java (along with other languages), expressions are where the actual calculations get done by the computer. An expression is a combination of constants, variables, and operators. Expressions also include method calls (such as Math.sqrt()). ALL method calls are considered expressions, whether it returns a result or not.

Resolving Symbols:

  1. First Java looks in 'local' variables
  2. Next Java looks in object variables
  3. Finally Java looks in class variables
example:
class Z {
    static int m=14
    int r=12;
    static int s=15;
    public static void main(String[]args) {
        int r=25;
        int s=95;
        System.out.println(s); // 94
        System.out.println(Z.s); // 15
        System.out.println(r); // 25
        System.out.println(m); // 14
    }
}


To access something in the object you are in, you can use the this function.
examples:
System.out.println(r); // prints the number stored in r
System.out.println(this.r); // prints the reference number to r

Abstract Methods:
The class AdjustmentListener contains a partial, incomplete version of adjustmentValueCahgned called an abstract (or incomplete) method.

In order to use the AdjustmentListener (or even compile the program), you have to replace the abstract method with a new one.

AdjustmentListener al = new AdjustmentListener() {
    public void adjustmentValueChanged(AdjustmentEvent ae) {
        ...
    }
}


This creates an AdjustmentListener object but replaces some of it's methods.

Note: You can only replace existing methods. You can NOT add new ones.

The ColorBar class - an example of Extending Classes:
class ColorBar
    JScrollBar jsb;
    JPanel display;
    ColorBar() {
        JScrollBar jsb = new JScrollBar(JScrollBar.HORIZONTAL);
        jsb.setPreferredSize(new Dimension(120,20));
        JPanel display = new JPanel();
        display.setPreferredSize(new Dimension(50,50));
        display.setBackground(Color.yellow);
        this.add(jsb);
        this.add(diplay);
    }
}

As demonstrated in the Colorbar class, using the extend function puts all the parts of another class into a class, plus allows the programmer to append things.

The Layout Manager:
The Layout Manager decides where to put each component and how big to make it. It first manages when the setVisible() method is called. It also manages sizes and locations when the window is resized or when components are changed. It manages by calling the component's setSize() and setLocation() methods.

There are different layout managers which allow different levels of control.



[ return ]