SE 450 Fall 2001/2002
Week 4 Lecture Notes
Passing Parameters to Applets
-
Use <param> tag in HTML page. Example: <param name=color value=blue>
-
Use getParameter(nameOfParameter) in Java code.
-
Example: Enhanced digital clock
java.awt.Graphics Class
-
Represent Graphics Context
-
A graphics context is an abstraction of various drawing surfaces:
- screen
- printer
- off-screen image (an image stored in memory)
- Provide a rich set of graphics methods.
- drawString(), drawLine()
- drawArc(), fillArc()
- drawOval(), fillOval()
- drawPolygon(), fillPolygon()
- drawRect(), fillRect()
- drawRoundRect(), fillRoundRect()
- setColor(color)-set the current color
- setFont(font)-set the current font
- setPaintMode()-set the paint, or overwrite mode
- setXORMode(color)-set the XOR mode
- getColor()-get the current color
- getFont()-get the current font
- getFontMetrics()-get the font metrics of the current font
- getFontMetrics(font)-get the font metrics for the specified font
java.awt.FontMetrics Class
-
Retrieved from Graphics class becuase metrics not only depend on the font
but also the resolution.
Animation Applets as a Framework
-
Abstact out the commonalities into a base class AnimationApplet
-
animationThread field
-
delay field
-
start() method
-
stop() method
-
run() method
-
You just implement what is different
-
paint() method
-
init() method
-
destroy() method
-
Any other class specific stuff that you need for your functionality.
-
java.applet.Applet ----> AnimationApplet ----> Your applet class
-
What does AnimationApplet look like??
-
How do we use it??
Double Buffering
-
A simple scrolling banner applet
-
Notice that sometimes the screen flickers
-
Caused by the mechanics of how applets redraw themselves
repaint() -----------> update() ----------------> paint()
-
The defualt implementation of update() (which comes from Container class)
does the following:
-
clears the background by filling it in with the background color
-
sets the pen color to the foreground color
-
calls the paint() method
-
Sometimes this happens slow enough for the human eye to catch the flicker
-
How do we fix this problem??? Use an off screen image (double buffering)!!!
-
Drawing on an off screen image is just like drawing on an on screen image.
-
Once the off screen image is done, you display it like any other image
(ie .gif, .jpeg, etc)
-
The improved Scrolling Banner applet
Bouncing Ball Applet Example
-
Create an applet that displays a bouncing ball inside of a rectangular
box. The ball will reverse direction whenever it touches any of the "walls"
of the box.
-
Here it is...
GUI Programming with AWT and Swing
Abstract Windows Toolkit (AWT) - java.awt.*
-
Provides basic support for graphical user interfaces
-
Abstract becuase it is platform independent.
-
Widgets that are the building blocks of GUI's
-
Primitives - Button, Label, CheckBoxe, etc
-
Containers - locations to put and group primitives (ie Frame, Panel, etc)
-
Layout managers to handle the arrangement of widgets
-
Event handlers to capture user inputs
-
Utility Classes to handle graphics and imaging
-
AWT Component Hierarchy (figure 6.2, p 259 in Jia)
-
Some examples:
a wrapping applet lifecycle applet ,
a text copy applet
Our first design pattern - The Composite Pattern (p. 260 in Jia)
-
Used to represent a part or whole hierarchy.
-
Clients can ignore the differences between compositions of object and
individual objects.
-
Composite - Defines interface for objects in composition
-
Leaf - AWT Primitives (Button, Label, etc)
-
Composite - AWT Containers (Frame, Panel, etc)
Swing - javax.swing.*
-
Provides more sophisticated components for UI design that AWT
-
Lightweight components vs the heavyweight components of AWT
-
Heavyweight components have peer components in the underlying operating system.
-
Look is determined by OS and can be different between platforms.
-
Lightweight components have no peers.
-
Look is independent of OS (pluggable look and feel).
-
Hundreds of classes but most useful are the AWT counterparts.
-
Begin with Jxxxx. For example JButton, JPanel, etc...
-
Swing Component Hierarchy (figure 6.2, p 259 in Jia)
Using Swing Applets with current browsers
-
Swing applets will always work with the appletviewer.exe present in 1.2.x and above.
-
The Java Plugin
is used to run Swing applets in current browsers. Automatically
installed with JDK 1.3 (and I believe JDK 1.2.x).
-
Netscape 6 is special. It uses a plugable JVM model.
-
The HTML converter is used to tell the browser to use the plugin. The HTML converter
can be found on the Java site
here.
Layout Mangers
-
A layout manager handles the layout of the elements in a container.
-
Relative position of elements are specified, not the absolute position.
-
Allows the position and size of elements to change automatically when the
size of the container is changed.
-
Certian container classes have default layout managers.
-
Layout Hierarchy (fig 6.4, p. 263 in Jia)
Layout Manger examples
-
FlowLayout
-
Default layout for an applet
-
Arrange components in a left to right flow.
-
GridLayout
-
Arrange components in a grid pattern.
-
BorderLayout
-
Arrange components into five regions.
-
Multiple Nested Layout Managers
-
Other layout managers in AWT and Swing we're not going to discuss (but you
can find info on them in the API reference)
-
GridBag layout - very flexible layout manager with keeps track of certain
constraints (GridBagConstraints).
-
CardLayout - used to implement tabbed dialogs and applets.
-
BoxLayout (Swing only) - similiar to GridBagLayout.
Event Handling (aka Where all the action is!!!)
-
Two pieces to the puzzle
-
Event source - component that generates the even (ie button, check box, choices)
-
Event listener - class that deals with the generated event(s)
-
In order to handle events, listeners must:
-
Implement the proper interface for the events it is interested in.
-
Tell the component that it is interested in handling the events.
-
One handler can handle multiple sources or multiple events.
-
Handler can handle full blown class or an inner class.
Events and their listeners (java.awt.event.*)
-
Listeners can either implement correct interface or extend correct adapter.
-
Adapter classes are simply classes that implement the interface with empty methods.
If you want it to do anything you have to override the methods in question.
-
Not all interfaces have adapters. Only those with more than one method have adapters.
-
XYZListener is an interface.
-
XYZAdapter is a class that implements the correct interface.
Event |
Listener Interface |
Listener Adapter |
ActionEvent |
ActionListener |
NONE |
AdjustmentEvent |
AdjustmentListener |
NONE |
ComponentEvent |
ComponentListener |
ComponentAdapter |
ContainerEvent |
ContainerListener |
ContainerAdapter |
FocusEvent |
FocusListener |
FocusAdapter |
KeyEvent |
KeyListener |
KeyAdapter |
MouseEvent |
MouseListener |
MouseAdapter |
MouseEvent |
MouseMotionListener |
MouseMotionAdapter |
WindowEvent |
WindowListener |
WindowAdapter |
ItemEvent |
ItemListener |
NONE |
TextEvent |
TextListener |
NONE |
see Jia, p273.
Nested Panels,Nested Panels with Inner Classes and Nested Classes with
anonymous classes
-
Nested panels example
uses the actual applet class to handle the events
generated by the "widgets".
-
Nested panels with inner
classes example uses the "inner" classes to handle the events
generated by the "widgets".
-
Inner classes are classes that are defined within the class declaration of another
class. They can not be public classes since this would violate the "only one public
class per file" rule.
-
Inner classes are meant to be small and act as helpers to the enclosing class.
-
Since they are enclosed within another class, only the enclosing class can see
the inner classes and the inner classes can see anything in the enclosing class.
-
Nested panels with
anonymous classes example uses anonymous classes to handle the events
generated by the "widgets".
-
Anonymous classes do not have a class declaration like we are used to seeing.
-
Anonymous classes have all of their methods declared immediately following the call
to the new operator.
-
Since they are define "inline" they should be small classes, one or two methods at most.
-
Some uses of anonymous classes: event handling, run methods for strings
Panels, Frames and Dialogs
-
Panels are used as containers for related widgets and really doesn't give you
anything above what you get from Container.
-
Each panel can have different layout managers which allows you to better control
the layout of your widgets by using the setLayout() method of the panel.
-
Frames are the main window for applications.
-
Typically yur application will extend Frame to create the main window of the
application.
-
Frames have a border, a title bar, and control buttons.
-
You have to handle the window closing event to actually kill an application.
-
Dialogs are pop-up windows that usually require immediate action from the user.
-
Dialogs can be used to display messages or get input from the user.