160 likes | 268 Views
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?.
E N D
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 • OBSERVER • STRATEGY • COMPOSITE • DECORATOR
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 • 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.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.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); … }
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 OBSERVER • The observer pattern is used for event notification. • Observables (classes like JButton) generate events. • An observable can have many observers.
OBSERVER PATTERN Terminology differs slightly in JFC classes: Abstract Observable Abstract Observable JButton ActionListener addActionListener(ActionListener) actionPerformed(ActionEvent) 0..*
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 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 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 handler …and changes the text of a label: private int _clickCount = 0; actionPerformed(ActionEvent e) { _clickCount++; _label.setText(“Button clicked ”+_clickCount+“ times.”); }
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.
DECORATOR • A decorator adds functionality while maintaining an interface. • One example: • InputStreamReader wraps InputStream • BufferedReader wraps InputStreamReader • Another example: • JScrollPane wraps JTextArea