java swing toolkit graphics the key to effectively using graphics in java is understanding: –the...

22
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

Post on 19-Dec-2015

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Java Swing Toolkit Graphics The key to effectively using graphics in Java is understanding: –the basic components of the graphics library –the patterns

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

Page 2: Java Swing Toolkit Graphics The key to effectively using graphics in Java is understanding: –the basic components of the graphics library –the patterns

Some patterns used

• OBSERVER

• STRATEGY

• COMPOSITE

• DECORATOR

Page 3: Java Swing Toolkit Graphics The key to effectively using graphics in Java is understanding: –the basic components of the graphics library –the patterns

Some patterns used

• OBSERVER – event handling

• STRATEGY – layout management

• COMPOSITE – containers are also components

• DECORATOR – scrollbars, streams

Page 4: Java Swing Toolkit Graphics The key to effectively using graphics in Java is understanding: –the basic components of the graphics library –the patterns

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.

Page 5: Java Swing Toolkit Graphics The key to effectively using graphics in Java is understanding: –the basic components of the graphics library –the patterns

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

Page 6: Java Swing Toolkit Graphics The key to effectively using graphics in Java is understanding: –the basic components of the graphics library –the patterns

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.

Page 7: Java Swing Toolkit Graphics The key to effectively using graphics in Java is understanding: –the basic components of the graphics library –the patterns

An example using a JFramepublic class JFrameExample {

public JFrameExample() {JFrame f = new JFrame();f.pack();f.setVisible(true);

}

public static void main(String[] args) {new JFrameExample();

}

}

Page 8: Java Swing Toolkit Graphics The key to effectively using graphics in Java is understanding: –the basic components of the graphics library –the patterns

Composite Pattern

Page 9: Java Swing Toolkit Graphics The key to effectively using graphics in Java is understanding: –the basic components of the graphics library –the patterns

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.

Page 10: Java Swing Toolkit Graphics The key to effectively using graphics in Java is understanding: –the basic components of the graphics library –the patterns

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.

Page 11: Java Swing Toolkit Graphics The key to effectively using graphics in Java is understanding: –the basic components of the graphics library –the patterns

Button Example

JButton button = new JButton("Push Me");f.getContentPane().add(button);button.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e) {

label.setText("Clicked");}

});

Page 12: Java Swing Toolkit Graphics The key to effectively using graphics in Java is understanding: –the basic components of the graphics library –the patterns

OBSERVER PATTERN

Idea: decouple event from event handling

Concrete Observable

Abstract Observable

Concrete Observer

0..*

Abstract Observable

attach(Observer)

detach(Observer)

notifyObservers()

Abstract ObservableAbstract Observer

update()

Page 13: Java Swing Toolkit Graphics The key to effectively using graphics in Java is understanding: –the basic components of the graphics library –the patterns

JFC use of OBSERVER

• The observer pattern is used for event notification.

• Observables (classes like JButton) generate events.

• An observable can have many observers.

Page 14: Java Swing Toolkit Graphics The key to effectively using graphics in Java is understanding: –the basic components of the graphics library –the patterns

OBSERVER PATTERN

Terminology differs slightly in JFC classes:

Abstract Observable

0..*

JButton

addActionListener(ActionListener)

Abstract ObservableActionListener

actionPerformed(ActionEvent)

Page 15: Java Swing Toolkit Graphics The key to effectively using graphics in Java is understanding: –the basic components of the graphics library –the patterns

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);

}

});

Page 16: Java Swing Toolkit Graphics The key to effectively using graphics in Java is understanding: –the basic components of the graphics library –the patterns

DECORATOR

• A decorator adds functionality while maintaining an interface.

• One example:– InputStreamReader wraps InputStream– BufferedReader wraps InputStreamReader

• Another example:– JScrollPane wraps Jlist

Page 17: Java Swing Toolkit Graphics The key to effectively using graphics in Java is understanding: –the basic components of the graphics library –the patterns

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.

Page 18: Java Swing Toolkit Graphics The key to effectively using graphics in Java is understanding: –the basic components of the graphics library –the patterns

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.

Page 19: Java Swing Toolkit Graphics The key to effectively using graphics in Java is understanding: –the basic components of the graphics library –the patterns

BorderLayout

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

Page 20: Java Swing Toolkit Graphics The key to effectively using graphics in Java is understanding: –the basic components of the graphics library –the patterns

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.

Page 21: Java Swing Toolkit Graphics The key to effectively using graphics in Java is understanding: –the basic components of the graphics library –the patterns

GridLayout

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

Page 22: Java Swing Toolkit Graphics The key to effectively using graphics in Java is understanding: –the basic components of the graphics library –the patterns

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.