Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introducing, the JFrame Gives us a work area beside System.out.println.

Similar presentations


Presentation on theme: "Introducing, the JFrame Gives us a work area beside System.out.println."— Presentation transcript:

1 Introducing, the JFrame Gives us a work area beside System.out.println

2 Many ways to make use of classes Dependency new JFrame( ); Realization public class FishClass implements IFishBehavior Association - passed into a method public A ( B b ) { _b = b; }

3 One step instantiation, Composition I JFrame myWindow = new JFrame( ); Two step Instantiation, Composition II private JFrame myWindow; public static void method( int a) { myWindow = new JFrame( ); … }

4 third way: public class myClass extends JFrame { // your class inherits all of JFrame’s // methods }

5 Extension, Hierarchy, and Inheritance

6 Extends – in the class statement, tells which larger class serves as the “parent”. Inheritance – the child class inherits methods and variables from the parent class Hierarchy - parent to child relationship

7 Extend keyword public class myClass extends someOtherClass { // I can use all the methods and // variables from someOtherClass in myClass. }

8 Other terms Parent / Child Base class / Sub class Super Class / Sub class Any class (not just an imported library class) can be a parent or base class. Write classes – then write new inherited classes for targeted capabilities.

9 Java common inherited classes public class myClass extends JFrame - myClass gets a whole bunch of methods for displaying windows on-screen including the ability to launch a paint( ) method. public class myClass extends Applet - applet and browser graphics public class myClass extends Object - the parent of ALL classes, usually omitted

10 JFrame Puts graphics-capable windows on screen. JOptionPane uses JFrame Colors Fonts Drawings A frame for intuitive GUI Adds a paint method

11 Use these helper libraries import javax.swing.JFrame ; // for JFrame import java.awt.* ; // for painting and animation import java.awt.event.* ; // for “event” handling, more later import java.util.Random ; // always useful

12 some JFrame methods setLocation(100,200); setSize(250,250); setVisible(true); setDefaultCloseOperation ( EXIT_ON_CLOSE) paint (Graphics g)

13 public class WindowClass extends JFrame { public WindowClass ( ) { setLocation(100,200); setSize(250,250); setVisible(true); } // end MainClass constructor } // end class

14 public class WindowClass extends JFrame { public WindowClass ( int x, int y ) { setLocation( x, y ); setSize(250,250); setVisible(true); } // end MainClass constructor } // end class

15 public static void main (String[] args) { new WindowClass( 200, 200 ); } // end main method

16 Demo

17 If you don’t extend JFrame…. public class WindowClass { public WindowClass ( ) { JFrame myFrame = new JFrame( ); myFrame.setLocation(100,200); myFrame.setSize(250,250); myFrame.setVisible(true); } // end MainClass constructor } // end class

18 Objects As Containers of Information

19 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

20 Color class Like the String class, and Integer class, does not use the new keyword. holds a color value (nothing more) e.g Color boxColor = new Color( ); boxColor = Color.blue; or Color boxColor = Color.blue then boxColor can be used to set System properties in Classes/Objects that need color (more later).

21 JColorChooser – returns a Color object to the caller

22 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 );

23 a clarification Color boxColor = Color.blue or Color boxColor = new Color( ); boxColor = Color.blue;object t to box Color.

24 Color boxColor = Color.blue Color.pink Color.red Color.green Color.blue JFrame library Computer Memory Space

25 Color boxColor = new Color( ); boxColor = Color.blue; Color.pink Color.red Color.green Color.blue ready for Color Color.blue Computer Memory Space

26 the this qualifier Means “this object, the one that we’re in”. Used when a call to a method, especially in a child, needs to specify which object the method should act upon. this always refers to an object of some type.

27 the super qualifier refers to the parent class the command super calls the parent’s constructor super.MethodName can call any method in the parent.

28 graphics on “your computer” Since there are many computers (types, instances).... and since the programs that you write should run anywhere.... then the programs that you write, should retrieve and use information native to the computer that it is running on. This is called “context awareness”

29 The paint Method Computer runs it repeatedly (you don’t), but then you can run it by calling repaint();. The computer passes it an object of the Graphics Class, of it’s own making.

30 public void paint (Graphics g) { // the computer calls the paint program // automatically whenever an event requires // the screen to be redrawn }

31 An “event” The user does anything: moves the mouse, moves the window, hits a key, etc.

32 Graphics! g.setColor g.fillRect g.drawString g.drawPolygon hundreds of methods that use your computer’s graphics capability

33 demo A Frank Lloyd Wright style generator


Download ppt "Introducing, the JFrame Gives us a work area beside System.out.println."

Similar presentations


Ads by Google