1 / 22

Java Swing Toolkit Graphics

Java Swing Toolkit Graphics. 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. Some patterns used.

Download Presentation

Java Swing Toolkit Graphics

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. Java Swing Toolkit Graphics • 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. Some patterns used • OBSERVER – event handling • STRATEGY – layout management • COMPOSITE – containers are also components • DECORATOR – scrollbars, streams

  4. java.awt.Component • All displayable objects in the graphics hierarchy extend this base class. You will never create an instance of this class directly (since it is abstract), but instead use the pre-defined GUI elements that Java provides.

  5. Containers • A containers is the basic enclosing element of a graphical application. You can’t have a graphical application without container to hold the rest of your graphical components. • The top level container for an application is usually a javax.swing.JFrame

  6. javax.swing.JFrame • A JFrame is a top-level container (meaning it does not need to be contained within any other container). • A JFrame is a window with a title and a border. • JFrames also support menu bars.

  7. An example using a JFrame public class JFrameExample { public JFrameExample() { JFrame f = new JFrame(); f.pack(); f.setVisible(true); } public static void main(String[] args) { new JFrameExample(); } }

  8. Composite Pattern

  9. Composite Pattern • The whole graphical framework is a hierarchy of components. This is an almost textbook example of the composite pattern. A frame contains many children, which all comply to the same interface (Component), and a call to, for example, repaint() on the frame will also call repaint() on all its children.

  10. Input Elements • The whole point of a GUI is to accept user input and do something with it. To this end, there are a large number of input components in Swing. • Let’s first examine the JButton.

  11. Button Example JButton button = new JButton("Push Me"); f.getContentPane().add(button); button.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { label.setText("Clicked"); } });

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

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

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

  15. Multiple Observers We can add more than one observer to our button. All observers are notified when a button event occurs. Lets add an observer that will also change the color of the label. button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { label.setForeground(Color.RED); } });

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

  17. STRATEGY • A layout manager has responsibility for laying out components within a container. • Unlike in NGP, JFC containers do not have fixed layout managers. • Layout managers are treated as strategies. • Strategies can be swapped.

  18. Layout Managers • Swing has several different layout managers. Some of the most common are FlowLayout, BorderLayout, GridLayout, and GridBag Layout. Each has advantages and disadvantages.

  19. BorderLayout • The BorderLayout manager creates an object that resembles a picture with a four sided frame, or border.

  20. FlowLayout • FlowLayout is arguably the simplest layout manager. It just stacks up components in a row, right to left. If it runs out of space, it wraps to a new line. This is the default layout manager for JPanel. You can see the behavior in how the button, text area, and combo box are laid out.

  21. GridLayout • GridLayout arranges its components in an equally spaced grid of cells. Will take as much space as is available to it.

  22. GridBag Layout • GridBag is both the most flexible of the layout managers, and the most difficult to use. It allows you customize the size and growth of all the components separately, but setting it up is a pain.

More Related