1 / 16

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. O BSERVER S TRATEGY C OMPOSITE D ECORATOR. Remember NGP?.

flavio
Download Presentation

Graphics without NGP

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Some patterns used • OBSERVER • STRATEGY • COMPOSITE • DECORATOR

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

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

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

  6. 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); … }

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

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

  9. OBSERVER PATTERN Terminology differs slightly in JFC classes: Abstract Observable Abstract Observable JButton ActionListener addActionListener(ActionListener) actionPerformed(ActionEvent) 0..*

  10. 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); … }

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

  12. 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++; … }

  13. 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.”); }

  14. Live code example(see lecture code repository)

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

  16. DECORATOR • A decorator adds functionality while maintaining an interface. • One example: • InputStreamReader wraps InputStream • BufferedReader wraps InputStreamReader • Another example: • JScrollPane wraps JTextArea

More Related