AWT vs Swing - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

AWT vs Swing

Description:

In Swing programs, you override the component's paintComponent method. ... { super.paintComponent (g); sets the color of g to. the component's background color ... – PowerPoint PPT presentation

Number of Views:70
Avg rating:3.0/5.0
Slides: 20
Provided by: wall57
Category:
Tags: awt | sets | swing

less

Transcript and Presenter's Notes

Title: AWT vs Swing


1
AWT vs Swing
  • AWT
  • Depend on the
  • native (peer) OS
  • High overhead.
  • Components look different on diff. sys.
  • Heavyweight components.
  • Swing
  • Pure Java (with 4 exceptions)
  • More efficient.
  • Components look the same on all systems.
  • Lightweight components.

2
Events and Listeners
  • When buttons are clicked they need to tell
  • someone but who?
  • public void addActionListener(ActionListener al)
  • is a method that JButton inherited from the
  • AbstractButton class.
  • add1.addActionListener(this)

This line of code is in the UserInterface class.
So, this refers to a UserInterface object
The ActionListener that will do something when
add1 is clicked
objects name
method in objects class
3
Events and Listeners (cont.)
  • add1.addActionListener(this)
  • this refers to this object that is running this
    code
  • Since a UserInterface object is running that line
    of code, the UserInterface class must implement
    the ActionListener interface.
  • Look up the ActionListener interface and you
  • will see that it has just one method to
  • implement, actionPerformed(ActionEvent e).

4
Summary of the add1 Button
  • add1 knows who to tell about clicks. (tell
    means send an ActionEvent)
  • add1 is the source of the ActionEvent.
  • The receiver of the ActionEvent has an
    actionPerformed method.
  • The actionPerformed method is called
    automatically when add1 is clicked.

5
JButtons Can Send Other Types of Events
  • Check the hierarchy of JButton on the Sun site. A
    JButton is-a Component.
  • Components have methods
  • addFocusListener (handles FocusEvents)
  • addKeyListener (handles KeyEvents)
  • addMouseListener (handles MouseEvents)
  • addMouseMotionListener

6
Choosing a Top Level Window
  • Java Applets use JApplet
  • Java Applications typically use JFrame
  • Display a JFrame by giving it a size and by
    making it visible. Look at JFrames methods for
    how.
  • Give your JFrame a way to exit the application
    when the user closes the frame.
  • Add components to the JFrames content pane.
  • Study the code that CodeWarrior generates!

7
How Does the Container Know Where to Put the
Components?
  • Layout managers control size and position.
  • FlowLayout left to right with wrapping.
  • GridLayout 2D grid of equally sized cells.
  • BorderLayout 5 areas N,S,E,W, center.
  • CardLayout In a stack, 1 comp is visible.
  • BoxLayout single row or col. (diff. sizes).

8
setLayout(null)
  • No Layout manager.
  • You control the size with method setSize.
  • Argument(s) are 2 ints or a Dimension object.
  • You control the position with setLocation.
  • Argument(s) are 2 ints or a Point object.

9
JPanels
  • JPanels are generic container used to contain and
    organize components.
  • You can have nested JPanels to better control the
    layout.
  • JPanels do not have content panes. You simply add
    components directly to them. Their default layout
    manager is FlowLayout.

10
Graphics and Drawing
  • Drawing Surfaces
  • Create a class that extends JPanel.
  • JPanels are transparent by default but can be
  • made to be opaque. (Do this by putting this line
    at
  • the top of your paintComponent method
  • super.paintComponent(g) ? where g is the
    Graphics object. )
  • Create a class that extends JComponent.
  • JComponents are always transparent.

11
Graphics Context
  • Every Java component automatically comes with a
    graphics context, i.e. an object of the Graphics
    class.
  • The graphics context object is the one with all
    the drawing methods.
  • In Swing programs, you override the components
    paintComponent method. (In AWT or in JApple,
    JFrame, JDialog, or JWindow, override the paint
    method.)
  • The paintComponent method is called automatically
    whenever the component needs to be repainted.

12
  • public class MyPaper extends JPanel
  • public MyPaper ( )
  • setBackground(Color.white)
  • public void paintComponent (Graphics g)
  • super.paintComponent (g)? sets the color of g
    to
  • the components background color
  • as it makes the component opaque.
  • g.setColor (Color.black)
  • g.drawLine(0, 60, 330, 60)
  • g.drawLine(0, 159, 330, 159)

13
Colors
  • A component has 2 colors assoc. with it.
  • a foreground color. ( getForeground() )
  • a background color. ( getBackground() )
  • A component also has a Graphic object assoc. with
    it (remember the graphic context?). It has 1
    color assoc. with it. (g.getColor() )

14
Coordinate Systems Example
(600, 0)
(0, 0)
(25, 20) in the JFrames coord. sys. (0, 0) in
the JPanels coord. sys.

(300, 200) in the JPanels coord. sys. (0, 0) in
the Houses coord. sys.
400 pixels
a JPanel object
(60, 340) in the JFrames coord. sys.
myButton
(600, 400)
a JFrame object
600 pixels
15
Colors
  • There are built in colors in the Color class such
    as Color.red, Color.yellow, Color.gray.
  • You can create Color objects with any of 3 Color
    constructors.
  • Color(int r, int g, int b) lt-- r, g, b range
    0-255
  • Color(int rgb) lt-- rgb65536r 256g b
  • Color(float r, float g, float b) lt-- r, g, b
    range 0-1
  • White is (255, 255, 255) and Black is (0, 0, 0)

16
Points and Dimension Objects
  • Points are useful objects for representing 2
    quantities, x and y locations.
  • Dimensions are also useful for representing 2
    quantities, width and height.
  • In the JComponent class,
  • getSize(Dimension d) returns the Dimension object
    d.
  • getLocation(Point p) returns the Point object p.

17
Lines Shapes
  • Look in the Graphics class for drawing and
  • painting methods remember, the
  • components graphics context object is
  • responsible for all the drawing and painting.
  • drawLine, drawOval, drawRect,
  • fillArc, fillOval, fillPolygon,
  • example g.fillOval(x, y, w, h)

(x, y)
h
w
18
Drawing Text
  • Use the drawString (String s, int x, int y)
    method in the Graphics class.
  • String s (drawn in this graphic contexts current
    font and color) appears with the baseline of the
    leftmost character at position (x, y) in this
    graphics context's coordinate system.

19
Fonts and Font Metrics
  • Each graphic context has an associated Font
    object and a FontMetrics object.
  • A FontMetrics object contains important data
    about the font like the maximum descent, maximum
    ascent, width of specific char., width of a
    specific string,
  • Font f new Font (Arial, Font.BOLD, 14)
  • g.setFont(f) ? assuming g is the graphic context.
Write a Comment
User Comments (0)
About PowerShow.com