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

Post on 19-Dec-2015

220 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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

• OBSERVER

• STRATEGY

• COMPOSITE

• DECORATOR

Some patterns used

• OBSERVER – event handling

• STRATEGY – layout management

• COMPOSITE – containers are also components

• DECORATOR – scrollbars, streams

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.

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

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.

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

}

}

Composite Pattern

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.

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.

Button Example

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

label.setText("Clicked");}

});

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

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

0..*

JButton

addActionListener(ActionListener)

Abstract ObservableActionListener

actionPerformed(ActionEvent)

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

}

});

DECORATOR

• A decorator adds functionality while maintaining an interface.

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

• Another example:– JScrollPane wraps Jlist

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.

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.

BorderLayout

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

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.

GridLayout

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

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.

top related