1 / 39

Inheriting the JFrame

Inheriting the JFrame. Three ways to make use of classes. 1. Using a static class (no need to create an object): myStr = String.toLowerCase( “ABCDE ); myInt = Integer.parseInt( “14” ); JOptionPane.showMessageDialog( null, “Hello” ); 1a Using a non-static class (new, but no oject)

Download Presentation

Inheriting the JFrame

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Inheriting the JFrame

  2. Three ways to make use of classes • 1. Using a static class (no need to create an object): myStr = String.toLowerCase( “ABCDE ); myInt = Integer.parseInt( “14” ); JOptionPane.showMessageDialog( null, “Hello” ); • 1a Using a non-static class (new, but no oject) new AnyClass( ); // essentially, just to run the // constructor

  3. Second way: Create objects • 2. One step instantiation: JFrame myWindow = new JFrame( ); • 2.a. Two step Instantiation: public 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. Extension, Hierarchy, and Inheritance • 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. import javax.swing.*; public class MainClass extends JFrame { // stuff here public static void main (String [ ] args) { MainClass application = new MainClass( ); } }

  11. JFrame Sept 18: • Puts graphics-capable windows on screen. • JOptionPane uses JFrame • Colors • Fonts • Drawings • A frame for intuitive GUI • Adds a paint method

  12. 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

  13. some JFrame methods • setLocation(100,200); • setSize(250,250); • setVisible(true); • setDefaultCloseOperation( EXIT_ON_CLOSE)

  14. public class MainClass extends JFrame { public MainClass ( ) { setLocation(100,200); setSize(250,250); setVisible(true); }// end MainClass constructor public static void main (String[] args) { MainClass application = new MainClass( ); } // end main method } // end class

  15. public class MainClass extends JFrame { public MainClass ( int x, int y ) { setLocation( x, y ); setSize(250,250); setVisible(true); }// end MainClass constructor public static void main (String[] args) { MainClass application = new MainClass(100, 200); } // end main method } // end class

  16. public class MainClass extends JFrame { public MainClass ( int x, int y ) { setLocation( x, y ); setSize(250,250); setVisible(true); }// end MainClass constructor public static void main (String[] args) { MainClass appl1 = new MainClass(100, 200); MainClass appl2 = new MainClass(200, 300); } // end main method } // end class

  17. Demo

  18. If you don’t extend JFrame…. public class MainClass { public MainClass ( ) { JFrame myFrame = new JFrame( ); myFrame.setLocation(100,200); myFrame.setSize(250,250); myFrame.setVisible(true); }// end MainClass constructor public static void main (String[] args) { MainClass application = new MainClass( ); } // end main method } // end class

  19. Objects As Containers of Information

  20. 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

  21. Color class Holds a color value (nothing more) e.g Color boxColor = new Color( redInt, greenInt,blueInt); boxColor = Color.blue; or Color boxColor = Color.blue then boxColor can be used to set System properties in Classes/Objects that need color.

  22. How do you define a color?

  23. JColorChooser – returns a Color object to the caller

  24. 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 );

  25. the Point class • holds an x,y value (nothing more) Point screenLocation = new Point( ); screenLocation.x = 100; screenLocation.y = 200;

  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 or class 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 once (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

  34. imports import javax.swing.*;import java.awt.*;import java.util.Random;public class FLWPainting extends JFrame { public int x=0, y=0, width=0, height=0, red=0, green=0, blue =0; public Random myGen = new Random(); public int calls = 0; public int size = 800;

  35. Constructor public FLWPainting(){setSize( size, size );setLocation( 10, 10 );setVisible( true );setDefaultCloseOperation( EXIT_ON_CLOSE); }// end constructor

  36. paint // "paint" is a method already in JFrame// it's called automajically by the computer// it uses the Graphics helper class// I'm rewriting it// That's called over-ridingpublic void paint( Graphics g ) {g.setColor( Color.white ); g.fillRect(0, 0, size, size);

  37. for (int i=0; i<1000; i++) { x = myGen.nextInt( size ); // 0 thru size-1 y = myGen.nextInt( size ); width = myGen.nextInt( 101 ); // 0 thru 100 height = myGen.nextInt( 101 ); red = myGen.nextInt( 256 ); // 0 thru 255 green = myGen.nextInt( 256 ); blue = myGen.nextInt( 256 ); g.setColor( new Color( red, green, blue ) ); g.fillOval( x, y, width, height );

  38. x = myGen.nextInt( size ); // 0 thru size-1 y = myGen.nextInt( size ); width = myGen.nextInt( 101 ); // 0 thru 100 height = myGen.nextInt( 101 ); red = myGen.nextInt( 256 ); // 0 thru 255 green = myGen.nextInt( 256 ); blue = myGen.nextInt( 256 ); g.setColor( new Color( red, green, blue ) ); g.fillRect( x, y, width, height );

  39. x = myGen.nextInt( size ); // 0 thru size-1 y = myGen.nextInt( size ); height = myGen.nextInt( 101 ); g.setColor( Color.black ); g.fillRect( x, y, 4, height ); x = myGen.nextInt( size ); // 0 thru size-1 y = myGen.nextInt( size ); width = myGen.nextInt( 101 ); g.setColor( Color.black ); g.fillRect( x, y, width, 4); } // end for } // end paint

More Related