Transcript View File


Applets

An applet is a Java program that runs on a web
page
◦ Applets can be run from:
 Internet Explorer
 Netscape Navigator (sometimes)
 appletviewer

An application is a Java program that runs all by
itself





Java supplies a huge library of pre-written “code,”
ready for you to use in your programs
Code is organized into classes
Classes are grouped into packages
One way to use this code is to import it
You can import a single class, or all the classes in
a package

To create an applet, you must import
the Applet class
◦ This class is in the java.applet package


The Applet class contains code that
works with a browser to create a
display window
Capitalization matters!
◦ applet and Applet are different names

Applet class derives from the
Abstract Window Toolkit (AWT)
hierarchy.

Here is the directive that you need:
import java.applet.Applet;





import is a keyword
java.applet is the name of the package
A dot ( . ) separates the package from the class
Applet is the name of the class
There is a semicolon ( ; ) at the end


“awt” stands for “Abstract Window Toolkit”
The java.awt package includes classes for:
◦
◦
◦
◦

Drawing lines and shapes
Drawing letters
Setting colors
Choosing fonts
If it’s drawn on the screen, then java.awt is probably
involved!

AWT supplies the following UI components:
◦
◦
◦
◦
◦
◦
Buttons (java.awt.Button)
Checkboxes (java.awt.Checkbox)
Single-line text fields (java.awt.TextField)
Menus (java.awt.MenuItem)
Containers (java.awt.Panel)
Lists (java.awt.List)

Since you may want to use many classes from the
java.awt package, simply import them all:
import java.awt.*;


The asterisk, or star (*), means “all classes”
The import directives can go in any order, but must be
the first lines in your program

To try it
◦ Compile: javac Hello.java
◦ Test: appletviewer hello.html
Now Press minimize
button, stop( ) method
will be called
init( ) method called
start( ) method called
after init( )
Maximizing Screen will again
call start( ) method
Closing the
window will
call destroy( )
method





init(): browser calls it when applet first loaded
start(): Called immediately after init( ) and again revisited after
browser left page containing applet
stop(): stop execution (eg. after switching to different page),
Called when the browser leaves the page
destroy(): Called when applet is killed (rarely used)
paint(): browser tells it it’s time to redraw
◦ Called by the browser after init and start, and again whenever
the browser redraws the screen
◦ This method is where user-level drawing is placed
◦ Inherited from java.awt.Container
public void paint(Graphics g) { … }
 A Graphics is something that holds information about a
painting
◦
◦
◦
◦
◦
It remembers what color you are using
It remembers what font you are using
You can “paint” on it, and it remembers what you have painted
Graphics g says g is an object of type Graphics
We can paint on g


The java.awt package defines a class named Color
There are 13 predefined colors--here are their fullyqualified names:
Color.BLACK
Color.DARK_GRAY
Color.GRAY
Color.LIGHT_GRAY
Color.WHITE
Color.PINK
Color.RED
Color.ORANGE
Color.YELLOW
Color.MAGENTA
Color.GREEN
Color.CYAN
Color.BLUE






Every color is a mix of red, green, and blue
You can make your own colors:
new Color( red , green , blue )
Amounts range from 0 to 255
Black is (0, 0, 0), white is (255, 255, 255)
We are mixing lights, not pigments
Yellow is red + green, or (255, 255, 0)


To use a color, we tell our Graphics g what color we
want:
g.setColor(Color.RED);
g will remember this color and use it for everything
until we tell it some different color
(0, 0)
(50, 0)
(0, 20)
(50, 20)
(w-1, h-1)





Java uses an (x, y) coordinate system
(0, 0) is the top left corner
(50, 0) is 50 pixels to the right of (0, 0)
(0, 20) is 20 pixels down from (0, 0)
(w - 1, h - 1) is just inside the bottom right corner, where
w is the width of the window and h is its height

There are two ways to draw rectangles:
◦ g.drawRect( left , top , width , height );
◦ g.fillRect(left , top , width , height );
◦
import java.applet.Applet;
import java.awt.*;
public class Drawing extends Applet {
public void paint(Graphics g) {
g.setColor(Color.BLUE);
g.fillRect(20, 20, 50, 30);
g.setColor(Color.RED);
g.fillRect(50, 30, 50, 30);
}
}






g.drawLine( x1 , y1 , x2 , y2 );
g.drawOval( left , top , width , height );
g.fillOval( left , top , width , height );
g.drawRoundRect( left , top , width , height );
g.fillRoundRect( left , top , width , height );
g.drawArc( left , top , width , height ,
startAngle , arcAngle );

setFont, getFont
◦ Specifies the font to be used for drawing text
◦ Setting the font for the Graphics object does not persist to
subsequent invocations of paint
◦ Set the font of the window (I.e., call the applet’s setFont
method) for permanent changes to the Graphics object
◦ In JDK 1.1, only 5 fonts are available: Serif (aka
TimesRoman), SansSerif (aka Helvetica),
Monospaced (aka Courier), Dialog, and
DialogInput

A String is a sequence of characters enclosed in double
quote marks
◦ "Hello, World!"

A double quote mark in a String must be preceded by a
backslash ( \ )
◦ "He said, \"Please don't go.\""

g.drawString( string , x , y );

Browser calls repaint method to request redrawing of
applet
◦ Called when applet first drawn or applet is hidden by another window
and then re-exposed


Search awt Method for loading an Image. Also find
Graphics Method for drawing that image in Applet
window and write instructions for loading and
drawing an Image using Applet viewer
Also explore how to play an audio file