Java swing toolkit graphics
This presentation is the property of its rightful owner.
Sponsored Links
1 / 22

Java Swing Toolkit Graphics PowerPoint PPT Presentation


  • 84 Views
  • Uploaded on
  • Presentation posted in: General

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


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

Some patterns used

  • OBSERVER

  • STRATEGY

  • COMPOSITE

  • DECORATOR


Some patterns used1

Some patterns used

  • OBSERVER – event handling

  • STRATEGY – layout management

  • COMPOSITE – containers are also components

  • DECORATOR – scrollbars, streams


Java awt component

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

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

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 jframe

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

}

}


Composite pattern

Composite Pattern


Composite pattern1

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

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

Button Example

JButton button = new JButton("Push Me");

f.getContentPane().add(button);

button.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

label.setText("Clicked");

}

});


O bserver pattern

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 o bserver

JFC use of OBSERVER

  • The observer pattern is used for event notification.

  • Observables (classes like JButton) generate events.

  • An observable can have many observers.


O bserver pattern1

OBSERVER PATTERN

Terminology differs slightly in JFC classes:

Abstract Observable

Abstract Observable

JButton

ActionListener

addActionListener(ActionListener)

actionPerformed(ActionEvent)

0..*


Multiple observers

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

}

});


D ecorator

DECORATOR

  • A decorator adds functionality while maintaining an interface.

  • One example:

    • InputStreamReader wraps InputStream

    • BufferedReader wraps InputStreamReader

  • Another example:

    • JScrollPane wraps Jlist


S trategy

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

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

BorderLayout

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


Flowlayout

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

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


Gridbag layout

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.


  • Login