using classes. one step instantiation, composition i jframe mywindow = new jframe( ); two step...

16
Using classes

Upload: clement-burke

Post on 19-Jan-2016

218 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: Using classes. One step instantiation, Composition I JFrame myWindow = new JFrame( ); Two step Instantiation, Composition II private JFrame myWindow;

Using classes

Page 2: Using classes. One step instantiation, Composition I JFrame myWindow = new JFrame( ); Two step Instantiation, Composition II private JFrame myWindow;

• One step instantiation, Composition IJFrame myWindow = new JFrame( );

• Two step Instantiation, Composition II private JFrame myWindow; // this is called DEPENDENCY

public static void method( int a) { myWindow = new JFrame( ); // CREATES = … // COMPOSITION … }

Page 3: Using classes. One step instantiation, Composition I JFrame myWindow = new JFrame( ); Two step Instantiation, Composition II private JFrame myWindow;

JFrame

• Puts graphics-capable windows on screen.

• JOptionPane uses JFrame

• Colors

• Fonts

• Drawings

• A frame for intuitive GUI

• Adds a paint method

Page 4: Using classes. One step instantiation, Composition I JFrame myWindow = new JFrame( ); Two step Instantiation, Composition II private JFrame myWindow;

JFrame

import javax.swing.JFrame;

Methods:

setLocation( x, y );

setSize( height, width );

setTitle(“string”);

setDefaultCloseOperation( EXIT_ON_CLOSE );

add( anotherObject );

Page 5: Using classes. One step instantiation, Composition I JFrame myWindow = new JFrame( ); Two step Instantiation, Composition II private JFrame myWindow;

A proper programimport javax.swing.*;

public class WindowDemo{

private JFrame myWindow; // DEPENDENCY

public WindowDemo() // all the work done in constructor { JFrame myWindow = new JFrame( ); // COMPOSITION myWindow.setLocation( 400, 200 ); myWindow.setSize(200, 100); myWindow.setTitle("HELLO"); myWindow.setDefaultCloseOperation( myWindow.EXIT_ON_CLOSE ); myWindow.setVisible( true ); }

public static void main(String args [ ]) // not considered part of the class { WindowDemo app = new WindowDemo(); } // end main

} //end class

Page 6: Using classes. One step instantiation, Composition I JFrame myWindow = new JFrame( ); Two step Instantiation, Composition II private JFrame myWindow;

JButton

import javax.swing.JButton;

can be “added” to JFrame

Page 7: Using classes. One step instantiation, Composition I JFrame myWindow = new JFrame( ); Two step Instantiation, Composition II private JFrame myWindow;

Stringyes, it’s not just a variable, it’s a class with

methods.

String testStr = new String( “ ” );testStr = JOptionPane.showInputDialog("enter a string");

System.out.println( testStr.concat("ing") );System.out.println( testStr.toLowerCase() );System.out.println( testStr.toUpperCase() );System.out.println( testStr.indexOf( "s" ) );System.out.println( testStr.replace('s', 'q'));

if ( testStr.equalsIgnoreCase("exit") == true )

{ System.out.println("exiting"); System.exit(0); }

Page 8: Using classes. One step instantiation, Composition I JFrame myWindow = new JFrame( ); Two step Instantiation, Composition II private JFrame myWindow;

Math class

• look it up

Page 9: Using classes. One step instantiation, Composition I JFrame myWindow = new JFrame( ); Two step Instantiation, Composition II private JFrame myWindow;

Random class

• look it up

Page 10: Using classes. One step instantiation, Composition I JFrame myWindow = new JFrame( ); Two step Instantiation, Composition II private JFrame myWindow;

Date class

• look it up

Date today = new Date();

System.out.println( today );

Page 11: Using classes. One step instantiation, Composition I JFrame myWindow = new JFrame( ); Two step Instantiation, Composition II private JFrame myWindow;

Objects As Containers of Information

Page 12: Using classes. One step instantiation, Composition I JFrame myWindow = new JFrame( ); Two step Instantiation, Composition II private JFrame myWindow;

Objects don’t always DO things

• Sometimes they just HOLD things (information)

• The Color class can hold color information

• The Point class can hold cartesian coordinate information

• The Container class contains 10,000 variables that describe your computer

Page 13: Using classes. One step instantiation, Composition I JFrame myWindow = new JFrame( ); Two step Instantiation, Composition II private JFrame myWindow;

Color class

• holds a color value (nothing more)

e.g Color boxColor; // DEPENDENCY boxColor = new Color( ); // COMPOSITION boxColor = Color.blue; // ASSIGNMENT

then boxColor can be used to set System properties in Classes/Objects that need color (more later).

Page 14: Using classes. One step instantiation, Composition I JFrame myWindow = new JFrame( ); Two step Instantiation, Composition II private JFrame myWindow;

JColorChooser – returns a Color object to the caller

Page 15: Using classes. One step instantiation, Composition I JFrame myWindow = new JFrame( ); Two step Instantiation, Composition II private JFrame myWindow;

Returns an Object?

• JColorChooser fills in all of the information in a blank object of the Color class, and copies it to the Color object in the calling statement:

boxColor = JColorChooser.showDialog(

null, Greeting, default color );

Page 16: Using classes. One step instantiation, Composition I JFrame myWindow = new JFrame( ); Two step Instantiation, Composition II private JFrame myWindow;

How is boxColor given the value “Color.blue” contained in the Jframe library?

private Color boxColor;

boxColor = new Color( );

boxColor = Color.blue;

Color.pink

Color.red

Color.green

Color.blue

COMPOSITION: creates an Object… ready for Value

ASSIGNMENT: Color.blue copied from Jframe library and assigned to boxColor

Computer Memory SpaceJFrame library

DEPENDENCY: reserves the name, sets public of private domain