CS4812 Java for Jockstm - PowerPoint PPT Presentation

1 / 43
About This Presentation
Title:

CS4812 Java for Jockstm

Description:

CS4812 -- Java for Jocks(tm) Lecture 3: being a discussion of ... You can change your topic up to 5 minutes before your presentation. ... – PowerPoint PPT presentation

Number of Views:75
Avg rating:3.0/5.0
Slides: 44
Provided by: davidd1
Category:
Tags: cs4812 | java | jocks | jockstm

less

Transcript and Presenter's Notes

Title: CS4812 Java for Jockstm


1
CS4812 -- Java for Jocks(tm)
  • Lecture 3
  • being a discussion of
  • Components, Containers and Layout Managers
  • A simplified look at, um, those buttons

2
Administrative Announcements
  • Today is the DUE DATE for p1. Need some help?
    Need some more time? Talk to me soon.
  • Today is the DEADLINE for topics/ presentation
    dates. Plan or get stuck. You can change your
    topic up to 5 minutes before your presentation.
    Presentation dates are going fast.
  • In both cases, an e-mail will do.
  • What is the difference between due date and
    deadline? Dont find out the hard way.

3
Todays Lecture Topics
  • Layout managers, components, containers (all
    heavy weight). Review?
  • Insets and simple graphics. Huh?

4
Layout Managers
  • Defined LayoutManager is an interface specifying
    the following behavior
  • void addLayoutComponet (String n, Component c)
  • void removeLayoutComponent (String n, Comonent
    c)
  • Dimension preferredLayoutSize (Container parent)
  • Dimension minimumLayoutSize (Container parent)
  • void layoutContainer (Container parent)

5
LayoutManager think position and size
  • Layout managers are used to calculate preferred
    and minimum sized for a container
  • They also arrange component within the container
  • Each container has associated with it exactly one
    layout manager
  • But a single layout manager--a proper object--may
    do double duty, performing the layout for
    multiple containers

6
A simple example . . .
  • public class MyPanel extends Container
  • static LayoutManager fLayout
  • new FlowLayout()
  • . . .
  • Public MyPanel()
  • setLayout(fLayout)
  • . . .

7
LayoutManagers Two General Flavors
  • One can conceptually divide layout managers into
    two types
  • those that attach constraints to their
    components, and . . .
  • . . . those that do not
  • What does this mean? If a manager attaches
    constraints to a component, information about a
    components location (e.g., compass points) is
    generated with the object.

8
LayoutManagers Constraints
  • BorderLayout specifies constraints corresponding
    to compass regions of a container

9
LayoutManagers Constraints
  • BorderLayout then appends constraint information
    on all components
  • E.g.
  • this.setLayout (new BorderLayout())
  • Button e new Button (East)
  • Button w new Button (West)
  • Button n new Button (North)
  • add(e, East) // deprecated
  • add(West, w) // works deprecated
  • //add(n, BorderLayout.NORTH) // better

10
LayoutManagers Constraints
11
LayoutManagers Another Example
  • import java.awt.
  • import java.applet.
  • public class test extends Applet
  • String Compass "North", "South", "East",
    "West", "Center"
  • public void init()
  • / ALWAYS call super init! /
  • super.init()
  • / set layout /
  • setLayout(new BorderLayout())
  • for (int iCompass.length i--gt0)
  • add (new Button (Compassi),
    Compassi)

12
LayoutManager Example
  • Thus

13
LayoutManager No Constraints
  • The second type of LayoutManager does not specify
    constraints for the objects it holds.
  • Examples
  • GridLayout()
  • FlowLayout()
  • Without constraints, you cannot accurately
    predict layout behavior across platforms

14
LayoutManager No Constraints
  • import java.awt.
  • import java.applet.
  • public class test extends Applet
  • public void init()
  • super.init()
  • String Labels "Short", "Short",
    "Long Label", "Really Long Label",
    "Really, really long"
  • setLayout(new FlowLayout())
  • for (int iLabels.length i--gt0)
  • add (new Button (Labelsi))
  • //init
  • //class test

15
LayoutManager No Constraints
  • Thus

16
LayoutManager No Constraints
  • And also

17
LayoutManager No Constraints
  • Note
  • Since pixels, fonts and insets vary with each
    platform, layout without constraints will vary
    greatly.
  • Lesson
  • Use layout managers without constraints only when
    you have one or few components

18
LayoutManager No Constraints
  • Do not suppose that layout managers without
    constraints are not useful!
  • One of the most useful constraint-free layout
    manager is GridLayout.
  • public GridLayout()
  • public GridLayout(int rows, int cols)
  • public GridLayout(int rows, int cols, int hgap,
    int vgap)

19
GridLayout Example
  • import java.awt.
  • import java.applet.
  • public class test extends Applet
  • public void init()
  • super.init()
  • setLayout(new GridLayout(4,3,5,5))
  • int off-2,2,0
  • for (int i10 i--gt1)
  • add (new Button (""(ioffi3)))
  • add (new Button ("."))
  • add (new Button ("0"))
  • add (new Button ("/-"))
  • add (new MyPanel(null))
  • // init
  • //test

20
GridLayout Example
  • Thus

21
Invoking Layout Managers
  • invalidate()-- a method found in Component and
    Container. Causes an invalidation of the object,
    and all its parents marks these as needing
    layout
  • doLayout() -- JDK 1.1 version of layout().
    Forces the layout manager to layout this
    component
  • When do these get called? Youd be surprised . .
    .

22
import java.awt. import java.awt.event. public
class LayoutWatcher extends Frame public
Button b1 public Label label1, label2 public
Button b2 public int count10,
count20 public LayoutWatcher()
super("Simple Frame to Watch
Layout") this.setSize(400,400) this.setLayou
t(new FlowLayout()) this.addWindowListener(new
WindowAdapter() public void
windowClosing(WindowEvent e) Window w
(Window) e.getSource() w.setVisible(false)
w.dispose() System.exit(0)) b1 new
Button ("Button 1") b2 new Button ("Button
2") label1 new Label("invalidate() count "
count1) label2 new Label("doLayout() count "
count2) label2.setBackground(Color.yellow) a
dd(b1) add(b2) add(label1) add(label2)
23
public void invalidate() System.out.println
("Old Invalidate count " count1) label1.setTe
xt("Invalidate count " count1) super.invali
date() //label1.invalidate() public void
layout() System.out.println ("Hey, this is
deprecated!") super.layout() public void
doLayout() System.out.println ("Old doLayout
count " count2) label2.setText("doLayout
count " count2) super.doLayout() publ
ic static void main(String arg) new
LayoutWatcher().show()
24
Insets and LayoutManagers
  • Layout management can be complemented by the use
    of Insets
  • All containers have Insets--margins within which
    you may paint, but not position components.
  • unless a null layout is set!

25
Insets--Did you know?
  • When components are arranged in a container, Java
    calls the containers getInsets() accessor to
    determine margins.
  • Insets vary with both the peer and the
    component--theres no way to know the default
    inset value!
  • Insets for Frame objects are unusual in that they
    count the menubar (if any).
  • Example
  • public Insets getInsets()
  • return new Insets(10,10,10,10)

26
Using Insets
  • Since components are not placed inside the
    insets, but you can nonetheless draw in the inset
    area, Insets are useful for adding graphic depth
    to layouts
  • They can be used to add border, edging, and the
    like.
  • Suppose we subclass panel to hold components, and
    specify an inset with colored edges . . .

27
  • import java.awt.
  • class MyPanel extends Panel
  • int offset Component c
  • public MyPanel ()
  • this (5, null)
  • public MyPanel (Component c)
  • this (5, c)
  • public MyPanel(int offset, Component c)
  • this.offset offset this.cc
  • setLayout (new BorderLayout())
  • if (c!null) add(c, "Center")
  • public Insets getInsets()
  • return new Insets
  • (offset, offset, offset, offset)

28
public void paint (Graphics g) Dimension D
getSize() Insets I getInsets() if (c!null)
g.setColor(Color.gray)
g.fillRect(0,0,D.width, I.top)
g.fillRect(0,0,I.left, D.height)
g.fillRect(D.width-I.right,
0, I.right, D.height)
g.fillRect(0,D.height-I.bottom,
D.width,D.height) else /WATCH THIS
SPACE!/ // if/else // paint MyPanel
29
Example (contd)
  • Using MyPanel to hold the buttons from the
    previous example, we find

30
Components
  • The widgets of Java--Buttons, SlideBars, Labels,
    etc.
  • Containers are components themselves! (I.e.,
    they can be added to other containers).
  • Hierarchy

Checkbox
Choice
Object
Component
Button
Container
Label
TextComponent
Panel
List
Applet
31
AWT Components HeavyWeights--How Buttons Work
  • Quiz How long did it take Sun to code the AWT?

Answer 6 Months, start to finish
Made possible by use of existing heavyweight
components--libraries specific to each platform
32
Component Architecture Peers
  • To better understand how AWT components work, one
    has to appreciate what is involved in the
    creation of heavyweight objects.

33
Component Creation Last-Minute Peer Creation
  • When AWT components are created, but not yet made
    visible, the associated peer resources are not
    allocated. When the component object is made
    visible, the peers are created just-in-time.
    (As a result, a components size is not valid
    until it is displayed.)
  • The same is found when a component is added to a
    non-visible container. When the contain is made
    visible, all its interior components get peer
    resources.

34
Component Creation Tips
  • When adding a component to a visible container,
    one needs to explicitly tell the AWT to create
    peer resources.
  • The method validate() method can be invoked on
    the object, but is usually called on the
    container. The method addNotify() is also
    useful, but does not layout the object.
  • Invoking validate() has a ripple effect, whereby
    every component in the container gets validated
    as well.
  • Example when adding components to an Applet
    object, you call validate() on the Applet, which
    creates peers for all the components in the
    Applet.

35
Introduction to Graphics Objects . . .
  • Every component/container has associated with it
    a graphics object
  • This graphics object can be use to draw on the
    component/container
  • The graphics object is best accessed through the
    paint() method, which is automatically called by
    Java for updates
  • You can also call paint() by requesting
    repaint() at key moments.

36
Overriding paint()
On can change the look of an object by simply
overriding paint(), thus
import java.awt. public class MyButton extends
Button public void paint (Graphics
g) g.setColor(Color.red) g.fillOval(0,0,g
etSize().width/2, getSize().height/2)
37
Graphics and Insets
  • While more specifics about graphics will come
    with the next lecture, we can use some basic
    graphics techniques to improve our containers.
  • Suppose we find a pattern we wish to use as a
    background.
  • Loading the pattern as an image can be costly in
    terms of time and memory.
  • We could instead draw it ourselves . . .

38
Graphics and Insets
  • Find a pattern you like, and consider how you
    might duplicate it in the paint() method

39
Graphics and Insets
  • Our foregoing example breaks down to a few simple
    drawLine calls
  • for (h0 hlt D.height h7)
  • for (w0 wltD.width w7)
    g.drawLine(w,h, w4,h)
    g.drawLine(w4,h, w7,h3)
    g.drawLine(w7,h3,
    w7,h3)
    g.drawLine(w7,h3,w7,h5)
    g.drawLine(w7,h5,w6, h3)
    g.drawLine(w6,h3,
    w6,h6)
    g.drawLine(w6,h6,w6,h6)
    g.drawLine(w6,h6,w5,h3)
    g.drawLine(w5,h3,w1,h7)

    g.drawLine(w1,h7,w2,h7)
    g.drawLine(w,h4, w3,h1)
    g.drawLine(w3,h1,w1,h1)

40
Graphics and insets . . .
  • What would happen if we incorporated this pattern
    into our panel, and used null as a sort of
    switch to draw the pattern or the inset borders ?
    . . .

41
  • import java.awt.
  • class MyPanel extends Panel
  • int offset Component c
  • public MyPanel ()
  • this (25, null)
  • public MyPanel (Component c)
  • this (5, c)
  • public MyPanel(int offset, Component c)
  • this.offset offset this.cc
  • setLayout (new BorderLayout())
  • if (c!null) add(c, "Center")
  • public Insets getInsets()
  • return new Insets(offset, offset,
  • offset, offset)
  • public void paint (Graphics g)
  • Dimension D getSize()
  • Insets I getInsets()


42
if (c!null) g.setColor(Color.gray) g.fillRect
(0,0,D.width, I.top) g.fillRect(0,0,I.left,
D.height) g.fillRect(D.width-I.right, 0,
I.right,D.height) g.fillRect(0,D.height-I.bot
tom, D.width, D.height) else for (int
h0 hlt D.height h7)
for (int w0 wltD.width w7)
g.drawLine(w,h, w4,h)
g.drawLine(w4,h, w7,h3)
g.drawLine(w7,h3,w7,h3)
g.drawLine(w7,h3,w7,h5)
g.drawLine(w7,h5,w6, h3)
g.drawLine(w6,h3,w6,h6)
g.drawLine(w6,h6,w6,h6)
g.drawLine(w6,h6,w5,h3)
g.drawLine(w5,h3,w1,h7)
g.drawLine(w1,h7,w2,h7)

g.drawLine(w,h4, w3,h1)
g.drawLine(w3,h1,w1,h1)
//class
43
  • import java.awt.
  • import java.applet.
  • public class test extends Applet
  • public void init()
  • super.init()
  • MyPanel m new MyPanel()
  • m.setLayout(new GridLayout(4,3,5,5))
  • int off-2,2,0 for (int i10 i--gt1) m.add
    (new MyPanel( (new Button (""(ioffi3)))))
  • m.add (new MyPanel ( new Button ("."))) m.add
    (new MyPanel (new Button ("0")))
  • m.add (new MyPanel (new Button ("/-")))
    add(m)
  • public Insets getInsets()
  • return new
  • Insets(5,5,5,5)
Write a Comment
User Comments (0)
About PowerShow.com