Inheriting the JFrame - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

Inheriting the JFrame

Description:

Inheriting the JFrame. Three ways to make use of classes. One step instantiation: ... Instantiating the Main class causes the constructor (if there is one) to be run. ... – PowerPoint PPT presentation

Number of Views:53
Avg rating:3.0/5.0
Slides: 29
Provided by: appliedsci3
Category:

less

Transcript and Presenter's Notes

Title: Inheriting the JFrame


1
Inheriting the JFrame
2
Three ways to make use of classes
  • One step instantiation
  • JFrame myWindow new JFrame( )
  • Two step Instantiation
  • public JFrame myWindow
  • public static void method( int a)
  • myWindow new JFrame( )

3
third way
  • public class myClass extends JFrame
  • // your class inherits all of JFrames
  • // methods

4
Extension, Hierarchy, and Inheritance
5
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

6
Extend keyword
  • public class myClass extends someOtherClass
  • // I can use all the methods and
  • // variables from someOtherClass in myClass.

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

8
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

9
  • import javax.swing.
  • public class MainClass extends JFrame
  • // stuff here
  • public static void main (String args)
  • MainClass application new MainClass( )

10
What is this?
  • Main method instantiates the class that its in?
  • Because main is not considered a member of the
    class that contains it.
  • The Main class serves a place-holding function
    for the computer, by keeping main in a usable
    place.
  • Instantiating the Main class causes the
    constructor (if there is one) to be run.
  • What other reasons?

11
JFrame
  • 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 dont 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 dont 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
the this qualifier
  • Means this object, the one that were 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.

22
the super qualifier
  • refers to the parent class
  • the command super calls the parents constructor
  • super.MethodName can call any method in the
    parent.

23
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

24
The paint Method
  • Computer runs it once (you dont), but then you
    can run it by calling repaint().
  • The computer passes it an object of the Graphics
    Class, of its own making.

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

26
An event
  • The user does anything moves the mouse, moves
    the window, hits a key, etc.

27
Graphics!
  • g.setColor
  • g.fillRect
  • g.drawString
  • g.drawPolygon
  • hundreds of methods that use your computers
    graphics capability

28
demo
  • A Frank Lloyd Wright style generator
Write a Comment
User Comments (0)
About PowerShow.com