Event Based Programming Check out EventBasedProgramming from SVN - - PowerPoint PPT Presentation

event based programming
SMART_READER_LITE
LIVE PREVIEW

Event Based Programming Check out EventBasedProgramming from SVN - - PowerPoint PPT Presentation

Event Based Programming Check out EventBasedProgramming from SVN Exam 2 is less than 2 weeks away! First day of 8 th week Layout in Java windows JFrames add(Component c) method Adds a new component to be drawn Throws out the old


slide-1
SLIDE 1

Event Based Programming

Check out EventBasedProgramming from SVN

slide-2
SLIDE 2

Exam 2 is less than 2 weeks away! First day of 8th week

slide-3
SLIDE 3

Layout in Java windows

slide-4
SLIDE 4

 JFrame’s add(Component c) method

  • Adds a new component to be drawn
  • Throws out the old one!

 JFrame also has method

add(Component c, Object constraint)

  • Typical constraints:

 BorderLayout.NORTH, BorderLayout.CENTER

  • Can add one thing to each “direction”, plus center

 JPanel is a container (a thing!) that can display

multiple components

Q1-2

slide-5
SLIDE 5

So, how do we do this?

slide-6
SLIDE 6

 To update graphics:

  • We tell Java library that we need to be redrawn:

 space.repaint()

  • Library calls paintComponent() when it’s ready

 Don

  • n’t ca

call ll paintComponent() yoursel elf! It’ t’s ju just t ther ere f for J Jav ava’s ca call b back. ck.

Q3

slide-7
SLIDE 7

public interface MouseListener { public void mouseClicked(MouseEvent e); public void mouseEntered(MouseEvent e); public void mouseExited(MouseEvent e); public void mousePressed(MouseEvent e); public void mouseReleased(MouseEvent e); }

Q4

slide-8
SLIDE 8

 Sometimes a new class is a

a speci ecial cas case e of the concept represented by another

 Can “borrow” from an

existing class, changing just what we need

 The new class inherits

its from the existing one:

  • all methods
  • all instance fields

Q5

slide-9
SLIDE 9

 class SavingsAccount extends BankAccount

  • adds interest earning, keeps other traits

 class Employee extends Person

  • adds pay information and methods, keeps other

traits

 class Manager extends Employee

  • adds information about employees managed,

changes the pay mechanism, keeps other traits

slide-10
SLIDE 10

 class SavingsAccount extends BankAccount {

// added fields // added methods }

 Say “SavingsAccount is a

s a BankAccount”

 Superc

rcla lass: BankAccount

 Su

Subc bcla lass: SavingsAccount

Q6

slide-11
SLIDE 11

The “superest” class in Java Still means “is a” Solid line shows inheritance

Q7

slide-12
SLIDE 12

 class ClickHandler implements MouseListener

  • ClickHandler promises

ses to implement all the methods of MouseListener

 class CheckingAccount extends BankAccount

  • CheckingAccount inherit

rits (or overrides) all the methods of BankAccount

For cl clien ent code reuse For imple leme mentation tion code reuse

slide-13
SLIDE 13
slide-14
SLIDE 14

 Inheri

rit methods unchanged

 Overrid

rride methods

  • Declare a new method with same signature to use

inste tead o d of superclass method

 Ad

Add entirely new methods not in superclass

Q8

slide-15
SLIDE 15

 ALWAYS i

YS inherit all fields unchanged

 Ca

Can a n add entirely new fields not in superclass

DANGER! Don’t use the same name as a superclass field!

Q9

slide-16
SLIDE 16

 Calling superclass me

meth thod:

  • super.methodName(args);

 Calling superclass constr

truct ctor

  • r:
  • super(args);

Must be the first line of the subclass constructor

Q10

slide-17
SLIDE 17

 A subclass instance is

is a superclass instance

  • Polymorphism still works!
  • BankAccount ba = new CheckingAccount();

ba.deposit(100);

 But not the other way around!

  • CheckingAccount ca = new BankAccount();

ca.deductFees();

 Why not?

BOOM!

For cl clien ent code reuse

Q11

slide-18
SLIDE 18

 Can use:

  • public void transfer(double amt, BankAccount o){

this.withdraw(amount);

  • .deposit(amount);

}

in BankAccount

 To transfer between different accounts:

  • SavingsAccount sa = …;
  • CheckingAccount ca = …;
  • sa.transfer(100, ca);
slide-19
SLIDE 19

 Hybrid of superclasses and interfaces

  • Like regular superclasses:

 Provide implementation of some methods

  • Like interfaces

 Just provide signatures and docs of other methods  Can’t be instantiated

 Example:

  • public abstract class BankAccount {

/** documentation here */ public abstract void deductFees(); … }

Elided methods as before

Also look at the code in the shapes package, especially ShapesDemo (during or after class)

slide-20
SLIDE 20

 Review

  • public—any code can see it
  • private—only the class itself can see it

 Others

  • default

lt (i.e., no modifier)—only code in the same packa kage can see it

 good choice for classes

  • protected—like default, but

subclasses also have access

 sometimes useful for helper methods

Bad for fields!

Q12

slide-21
SLIDE 21

Demo UML Design Questions

slide-22
SLIDE 22

Linear Lights Out It's a solo project, but feel free to talk with others as you do it. And to ask instructor/assistants for help

Q13-Q14

slide-23
SLIDE 23

BigRational from HW 10 BoardGames from HW 10