review - PowerPoint PPT Presentation

1 / 46
About This Presentation
Title:

review

Description:

Imagine a clock 'ticking' at every tick, the computer generates an event. ... tickTimer.stop(); How many milliseconds between events. what code runs every tick ... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 47
Provided by: mik97
Category:
Tags: review | tick

less

Transcript and Presenter's Notes

Title: review


1
review
  • Midterm on Wed.

2
A Java Method basic program unit
  • Methods are often called routines, subroutines,
    functions or procedures
  • A single function from start to finish
  • Can be used many times (called) from all over
  • Many methods and variables are collected into a
    class
  • A Class is a Java program that does one job, and
    draws on many methods and variables to accomplish
    it. Classes eventually become objects

3
// public
static int ExampleMethod( ) int x, y,
Sum x 2 y 2 Sum x y
return( Sum ) //a service to users
//of this
method //
4
How to call a method from elsewhere in your
program
  • Calling a void method myMethod( )
  • Calling a method with a return int variable -
  • i.e. public static int myMethod( )
  • return(6)
  • Calling from main
  • int LocalVariable
  • LocalVariable myMethod( )

5
Our First Complete Java Class (i.e. collection of
Methods and variables).
class
public class Lab1MainClass String
GreetingStr boolean AreVariablesSet
public static void main( String args ) /
ignore String args/
AreVariablesSet false AreVariablesSet
Set_Greeting( ) if (AreVariablesSet
true) then
System.out.println(GreetingStr)
public boolean Set_Greeting( )
GreetingStr Hello" return(
true ) // end class
variables
method1main
call
return
method2
6
Instantiation
  • You or someone else writes a class
  • public class someClass
  • ..
  • You use the class in your program
  • You must make a local copy
  • someClass myObject new someClass( )
  • Or, two-step
  • public someClass myObject
  • myObject new someClass()

7
What a Class does
  • A collection of methods and variables.
  • Every Object created from a class, operates as an
    independent, freely running program.
  • Only one main allowed
  • main spawns useable objects, sometimes many
    objects of the same class.

8
Using a library class
  • import javax.swing.JOptionPane
  • import javax.swing.JButton
  • public class test
  • public static void main (String args )
  • JOptionPane.showMessageDialog(null, Hello)
  • JButton myButton new JButton( )

9
Another example of using a Class What if we need
a Date
  • Look for a Date (or something like it) class.
  • Check which library to import
  • See if it has appropriate methods.

10
Objects
  • They are instantiated from Classes
  • An object, is a Class that has been put to
    work.
  • Since Classes can be used many times in the same
    program, each instance of a Class is a
    separate, usable copy.

11
Copies of Classes
  • Copy is an oversimplification, but the concept
    has run-time value
  • A copy is created using the new statement.
  • A copy of a Class is an Object.
  • non-Static Classes can never be used directly.
  • They must be copied to Objects
  • The copy step is called instantiation.
  • Your program owns and controls the copy.

12
Instantiation
  • Once a class is defined, making it your own for
    use, using the new keyword
  • myClass myObject new myClass( )
  • Creates an instance of the Class.
  • the Class solves a Class of problems.

13
Class constructor
  • If a class contains a method that has the same
    name as the class, it gets run automatically at
    instantiation.
  • e.g. MyClass MyObject new MyClass( )
  • will run a method in MyClass, if its name
    MyClass( )
  • Called the Class Constructor.
  • It's the reason for the parenthesis in the
    instantiation statement

14
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( )

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

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

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

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

19
JColorChooser returns a Color object to the
caller
20
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 )

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

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

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

24
what does this do?
  • FlowLayout layout new FlowLayout( )
  • setLayout( layout )

25
Setting up a JFrame
  • setSize( w, h )
  • setLocation( x, y )
  • setDefaultCloseOperation( EXIT_ON_CLOSE )
  • FlowLayout layout new FlowLayout()
  • setLayout( layout )
  • JButton helloButton new JButton( "Hello" )
  • add( helloButton )
  • setVisible( true )

26
grab a color
  • public Color palette
  • new Color ( 25, 120, 200 )
  • // palette is a Color object
  • // Color (red, green, blue )

note 25, 120, 200 is ocean blue
27
graphics on a computer
X direction
( 0, 0 )
( 1024, 0 )
( ?, ? )
y direction
( 0, 768 )
( 1024, 768 )
28
what is paint?
  • a method in JFrame
  • its a void method (returns nothing to the
    caller )
  • called by the computer automatically
  • that we re-write (override)
  • that someone else wrote
  • that uses a Graphics helper class
  • that puts colors and shapes in a JFrame

29
Graphics helper class?
  • look up the Graphics class on the Sun Java
    website
  • It must be used by a paint method in a JFrame
    class
  • it contains variables and methods of its own, to
    color and draw shapes

30
structure of a paint method
  • public void paint( Graphics g )
  • // g is the Graphics copy (object)
  • // set a color
  • g. setColor( pallette )
  • // do something with the color
  • // g.fillRect( x, y, width, height )
  • g.fillRect( 50, 100, 200, 200 )

31
Using Arrays to draw shapes
  • int xValues 245, 245, 260, 260, 230, 230
  • int yValues 250, 300, 300, 310, 310, 250
  • g.setColor( Color.yellow )
  • g.fillPolygon ( xValues, yValues, 6 )
  • // connects the dots

32
The Font class
  • Constructor
  • Font ( Name, Style, Size )
  • Name Monospace, Arial, Helvetica
  • Style uses FIELDS
  • Font.BOLD, Font.PLAIN, Font.ITALIC
  • Font myFont new Font(Arial, Font.PLAIN,12 )

33
Writing text in Graphics
  • Font is an object like Color
  • Font myFont new Font ("Monospaced",
  • Font.BOLD , 12)
  • g.setFont( myFont )
  • g.setColor( Color.red)
  • g.drawString( hello" ,160, 220)

34
3 new classes
  • Use these two
  • ActionListener a private inner class
  • ActionEvent a helper class used by
    ActionListener
  • To write your own
  • Private inner class with actionPerformed( )
    method

35
Add this to any code
  • private class eventClass implements
    ActionListener
  • public void actionPerformed ( ActionEvent e )
  • System.out.println(Something just
    happened)
  • System.out.println( e.getActionCommand( )
    )

36
Add this to any code
  • private class eventClass implements
    ActionListener
  • public void actionPerformed ( ActionEvent e )
  • System.out.println(Something just
    happened)
  • System.out.println( e.getActionCommand( )
    )

37
Objects of the private inner class
  • Useful when this becomes an object somewhere in
    the immediate code (for instance, a class
    constructor)
  • eventClass handler new eventClass( )

38
Tying a component to an ActionListener
  • Components JButtons, JComboBoxes, JTextFields,
    and other GUI controls.
  • JButton helloButton new JButton( Hello)
  • eventClass handler new eventClass( )
  • helloButton.addActionListener( handler )

39
  • import java.awt.event.
  • public class ButtonTest extends JFrame
  • public ButtonTest ( )
  • public static void main (String args)
  • private class eventClass implements
    ActionListener
  • public void actionPerformed ( ActionEvent
    e )

JFrame class
Constructor
Inner class
main method
40
e.getSource( )
  • Pull JButtons, JTextFields out of constructor
    brackets.
  • Make them public and known to every one.
  • Can be tested in if statements

41
Instantiation
  • public class ButtonTest extends JFrame
  • JButton exitButton new JButton(I want to
    leave)
  • public ButtonTest()
  • // other stuff

42
test in actionPerformed
  • private class ButtonHandler implements
    ActionListener
  • public void actionPerformed( ActionEvent e
    )
  • System.out.println(e.getActionCommand())
  • if (e.getSource( ) exitButton)
  • System.out.println("Exiting")
  • System.exit(0)

43
  • public class TextFieldTest extends JFrame
  • JTextField writeBox
  • public TextFieldTest()
  • setLayout( new FlowLayout( ) )
  • writeBox new JTextField( "Hello" )
  • add( writeBox )
  • eventClass handler new eventClass( )
  • writeBox.addActionListener( handler )
  • setVisible(true)

44
A Timer Event
  • Imagine a clock ticking at every tick, the
    computer generates an event.
  • Computers have many internal clocks.
  • Some with nano-second timing.
  • 10-9
  • A nanosecond is to a second as a second is to 30
    years
  • The standard measure of computer time is the
    millisecond - 10-3

45
Time
  • second
  • millisecond - 1/1000 - 10-3
  • microsecond - 1/1,000,000 - 10-6
  • nanosecond - 1/1,000,000,000 - 10-9

46
The Timer Component
  • Timer tickTimer new Timer( 1000, null )
  • SomeHandler myHandler new SomeHandler( )
  • tickTimer.addActionListener( myHandler )
  • tickTimer.start()
  • tickTimer.stop()

How many milliseconds between events
what code runs every tick
Write a Comment
User Comments (0)
About PowerShow.com