Title: OOP
1OOPM el cuarto día
the ordinary laws of motion do not apply to
hyperspace and it would take us a week of
computation to calculate a new set of parameters
- Isaac Asimov -
2OOPM the GUI Abstract Window Toolkit
- The java.awt package is the Abstract Window
Toolkit. The classes into it can be roughly
divided into three categories - Graphics classes that define colors, fonts,
images, polygons - Components these classes are what we commonly
identify as the GUI components such as buttons,
menus, lists and dialog boxes - Layout Managers classes that control the layout
of components within their container objects
3OOPM the GUI Abstract Window Toolkit
- The java.awt package, a picture
Layout
Graphics
Components
4OOPM AWT Graphics
The class Graphics is probably the most important
It defines methods for doing line and text
drawing and image painting
It relies on other classes such as Color, Font,
Image and Polygon
Image is itself an important class, used in many
places in java.awt and throughout the related
package java.awt.image
5OOPM AWT Component
The classes Component and Menucomponent are in
the second category of java.awt classes
Their subclasses are GUI components that can
appear in interfaces and menus
It relies on other classes such as Button, List ,
Choice, Canvas, TextArea, PopupMenu, Container
Components are contained in Containers, which are
responsible of of arrange them visually. add()
adds components into containers
6OOPM AWT Container
The subclass Container is a part of the Component
class
Their subclasses are quite general structures
from a graphical point of view
It relies on other classes such as Window,
ScrollPane and Panel
Containers are related to different layout
managers by the use of the setLayout() method
7OOPM AWT Container
Panel ScrollPane Window
Container
Dialog Frame
File Dialog
Frame is a toplevel window that can contain a
menu bar and have a custom cursor and icon
Dialog is a dialog window
Panel is a container that does not have its own
window it is contained within some other
container
8OOPM the button an example
Java AWT provides a predefined class for modeling
buttons, called Button
the constructor for Button accepts the text in it
(a String reference) as its argument
new Button (string)
An example of this would be
Button b b new Button(touch Bjorn)
9OOPM the button an example
- Now we have created the object button, but we
must also accomplish the following - Buttons should appear in the applet
- The applet should be able to respond to clicks in
the buttons
Java AWT provides a predefined method that
handles both problems add()
The complete code would be
Button b b new Button(touch Bjorn) add(b)
NOW THE BUTTON IS CONTAINED INTO A CONTAINER!!
10OOPM AWT Layout Manager
The third category of java.awt is the layout
managers class
Its subclasses are responsible for arranging the
Component objects contained within a specified
Container
Subclasses such as GridBaglayout, BorderLayout
and GridLayout
11OOPM AWT a general picture
Checkbox Choice List Button Canvas Container Label
Scrollbar TextComponent
java.awt
java.lang
CheckboxGroup Component MenuComponent MenuShort
cut FlowLayout GridLayout GridBagConstraints B
orderLayout CardLayout GridBagLayout
Panel ScrollPane Window
Dialog Frame
FileDialog
MenuBar MenuItem
Menu CheckboxMenuItem
PopupMenu
Object
12OOPM the button an example
If we implement the whole program that shows the
button on the screen, it would look like
import java.awt. import java.applet. public
class Press extends Applet public void init()
Button b b new Button(touch
Bjorn) add(b)
BIG DUDE Where are the containers and the layout
manager in here?!?!
13OOPM the button an example
Applet is an example of a container class in Java
this means, a class whose objects can
graphically contain controls or other containers
Container objects such as Applets rely on
LayoutManager objects to guide the placement of
their contents
Programmers can control the way applets or other
container objects place their contents by
creating a LayoutManager object
First we are going to study two different ways of
showing the information on the screen FlowLayout
and BorderLayout
14OOPM LayoutManager FlowLayout
FlowLayouts manager causes the objects to be
placed one after the other in the order in which
they are added to the container. It goes in rows
starting at the top and from left to right.
Depending on the container the objects would fit
or not in the expected positions
Imagine that we want to implement a calculator,
that should look like the following
0.00
textarea (label)
C
/
7
8
9
-
4
5
6
buttons
1
2
3
0
.
15OOPM LayoutManager FlowLayout
import java.awt. import java.applet. public
class Calc extends Applet public void init()
Button b1, b2, b3, b4, b5, b6, b7, b8, b9,
b0 Button bC, beq, badd, bsub, bdiv, bmult,
bdot Label result b1 new Button(1)
b2 new Button(2) b3 new Button(3)
b4 new Button(4) b5 new Button(5)
b6 new Button(6) b7 new Button(7)
b8 new Button(8) b9 new Button(9)
b0 new Button(0) bC new Button(C)
beq new Button() badd new
Button() bsub new Button(-) bdiv
new Button(/) bmult new Button() bdot
new Button(.) setLayout(new
FlowLayout()) result new Label(0.00,
Label.RIGHT) add(result) add(bC) add(beq)
add(bdiv) add(bmult) add(b7) add(b8)
add(b9) add(bsub) add(b4) add(b5) add(b6)
add(badd) add(b1) add(b2) add(b3) add(beq)
add(b0) add(bdot)
The code would look like this
16OOPM LayoutManager FlowLayout
A result on the applet to the code would look
like the following
C
/
7
8
9
-
4
5
0.00
6
1
2
3
0
.
Or maybe
C
/
0.00
WHAT CAN WE DO?
7
8
9
-
4
5
6
1
2
3
0
.
17OOPM LayoutManager BorderLayout
BorderLayouts manager divides the container into
five regions north, south, east, west and
center. It allows the programmer to assign
objects to these regions
Controls assigned to north and south are
stretched horizontally in order to cover top and
bottom borders of the container. Controls
assigned to east and west are stretched
vertically. Controls in the center take whats
left.
north
center
west
east
south
18OOPM LayoutManager
BorderLayouts manager is not perfect at all,
imagine that you want to implement the calculator
with it you only have 5 regions for including
18 buttons and a text field!!
FlowLayouts manager was not able of doing this
work, either
The combination of both classes is going to be
the solution to our problem, Java provides a
tool, the Panel class, that combined with these
two layout managers allows us to accomplish this
design
Panel is a container
19OOPM LayoutManager
Panel is the simplest container class, the only
thing you can do with it is to assign it a layout
manager and add objects
The trick in here is that we can group the
objects in panels, this makes easier to layout
them. A first strategy could be
0.00
panels within Applet
panels within panels
3
0
.
20OOPM LayoutManager
0.00
FlowLayout object
FlowLayout objects
BorderLayout objects
21OOPM AWT FlowLayout/BorderLayout
Java AWT provides some predefined Layout classes,
on is the so called FlowLayout
the constructor for FlowLayout accepts until 3
arguments
new FlowLayout ()
An example of this would be
FlowLayout fl fl new FlowLayout()
22OOPM AWT Panel
Java AWT provides a predefined class container,
called Panel
the constructor for Panel accepts not to have any
arguments
new Panel ()
An example of this would be
Panel p p new Panel()