Using classes
One step instantiation, Composition I JFrame 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 … }
JFrame Puts graphics-capable windows on screen. JOptionPane uses JFrame Colors Fonts Drawings A frame for intuitive GUI Adds a paint method
JFrame import javax.swing.JFrame; Methods: setLocation( x, y ); setSize( height, width ); setTitle(“string”); setDefaultCloseOperation( EXIT_ON_CLOSE ); add( anotherObject );
A proper program import 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
JButton import javax.swing.JButton; can be “added” to JFrame
String yes, 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); }
Math class look it up
Random class look it up
Date class look it up Date today = new Date(); System.out.println( today );
Objects As Containers of Information
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
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).
JColorChooser – returns a Color object to the caller
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 );
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 Space JFrame library DEPENDENCY: reserves the name, sets public of private domain