graphics without ngp
Download
Skip this Video
Download Presentation
Graphics without NGP

Loading in 2 Seconds...

play fullscreen
1 / 16

Graphics without NGP - PowerPoint PPT Presentation


  • 74 Views
  • Uploaded on

Graphics without NGP. The key to effectively using graphics in Java is understanding: the basic components of the graphics library the patterns that are used to combine components. Some patterns used. O BSERVER S TRATEGY C OMPOSITE D ECORATOR. Remember NGP?.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Graphics without NGP' - flavio


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
graphics without ngp
Graphics without NGP
  • The key to effectively using graphics in Java is understanding:
    • the basic components of the graphics library
    • the patterns that are used to combine components
some patterns used
Some patterns used
  • OBSERVER
  • STRATEGY
  • COMPOSITE
  • DECORATOR
remember ngp
Remember NGP?
  • NGP classes are designed to hide complexity of JFC classes.
  • Using NGP taught you the basics of event-driven programming.
  • Now we will learn how raw JFC classes are wired together.
  • Let’s start with the NGP.Components.PushButton
ngp components pushbutton
NGP.Components.PushButton
  • How did we create a PushButton?

new PushButton(NGP.Container, String)

  • What does the constructor do with the container?

To answer this, we need to look at the PushButton class

ngp components pushbutton1
NGP.Components.PushButton
  • How did we specify behavior?

We subclassed and overrode “public void release()”

  • How did this work?

To answer this, we need to look at the PushButton class

ngp components pushbutton2
NGP.Components.PushButton

In the constructor the button adds itself to the container (which keeps track of a collection of components). The string is passed to the superclass constructor.

public PushButton(NGP.Container container, String name) {

super(name);

container.add(this);

}

o bserver pattern
OBSERVER PATTERN

Idea: decouple event from event handling

Abstract Observable

Abstract Observable

Abstract Observable

Abstract Observer

attach(Observer)

detach(Observer)

notifyObservers()

update()

0..*

Concrete Observable

Concrete Observer

jfc use of o bserver
JFC use of OBSERVER
  • The observer pattern is used for event notification.
  • Observables (classes like JButton) generate events.
  • An observable can have many observers.
o bserver pattern1
OBSERVER PATTERN

Terminology differs slightly in JFC classes:

Abstract Observable

Abstract Observable

JButton

ActionListener

addActionListener(ActionListener)

actionPerformed(ActionEvent)

0..*

event handling
Event handling

The observer pattern is used to associate an event handler with the button. How is this association set up?

public PushButton(NGP.Container container, String name) {

super(name);

this.addActionListener(new ButtonListener());

container.add(this);

}

the event handler
The event handler

What does the event handler do? (This is an inner class of the PushButton class):

private class ButtonListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

release();

}

}

It calls the button’s release method!

our own simple button handler
Our own simple button handler

The button handler counts the number of times a button has been pressed…

private int _clickCount = 0;

actionPerformed(ActionEvent e) {

_clickCount++;

}

our own simple button handler1
Our own simple button handler

…and changes the text of a label:

private int _clickCount = 0;

actionPerformed(ActionEvent e) {

_clickCount++;

_label.setText(“Button clicked ”+_clickCount+“ times.”);

}

s trategy
STRATEGY
  • A layout manager has responsibility for laying out components within a container.
  • In NGP different containers had fixed layout strategies (e.g. NGP.Containers.Row and NGP.Containers.Column).
  • Unlike in NGP, JFC containers do not have fixed layout managers.
  • Layout managers are treated as strategies.
  • Strategies can be swapped.
d ecorator
DECORATOR
  • A decorator adds functionality while maintaining an interface.
  • One example:
    • InputStreamReader wraps InputStream
    • BufferedReader wraps InputStreamReader
  • Another example:
    • JScrollPane wraps JTextArea
ad