Programming Java: Applets
Programming Java: Applets
Applets
Incheon Paik
1 Applets
Java
Contents
Overview of Applets
First Java Applet
The Life Cycle of an Applet
The Graphics Class
Using Colors
Displaying Text
Using Applet Dimensions
Using Applets in a Web Page
The Applet Class
The AppletContext Class
Using Images
Using Threads
Double Buffering
2 Applets
Java
An Overview of Applets
Applets
3 Applets
Java
Extends Applet
Graphics by Abstract Window Toolkit (AWT)
Run: after compile,
appletviewer FirstApplet.html
or
appletviewer FirstApplet.java
4 Applets
Java
The Life Cycle of an Applet
import java.applet.Applet;
import java.awt.Graphics;
/*
<applet code="AppletLifecycle" width=300
height=50>
</applet>
*/
http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Graphics.html
6 Applets
Java
Using Colors
import java.applet.Applet;
Color Constructors import java.awt.Color;
Color(int red, int green, int blue) import java.awt.Graphics;
/*
Color(int rgb) <applet code="BlueString" width=300 height=100>
Color(float r, float g, float b) </applet>
*/
http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Color.html
7 Applets
Java
Displaying Text
import java.applet.Applet;
Font Constructor import java.awt.*;
/*
Font(String name, int style, int ps) <applet code="FontDemo" width=200 height=200>
</applet>
*/
// Draw baseline
int baseline = 100;
g.setColor(Color.lightGray);
FontMetrics Constructor g.drawLine(0, baseline, 200, baseline);
8 Applets
Java
Using Applet Dimensions
import java.applet.*;
getSize() Method import java.awt.*;
/*
Dimension getSize() <applet code="Circle" width=200 height=200>
</applet>
*/
http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Dimension.html
9 Applets
Java
10 Applets
Java
The Applet Class
import java.applet.*;
import java.awt.*;
java.lang.Object /*
<applet code="AppletParameters" width=300
height=300>
<param name="background" value="0xffffff">
java.awt.Component <param name="foreground" value="0x000000">
<param name="message" value="Testing Applet
Parameters">
</applet>
*/
java.awt.Container
public class AppletParameters extends Applet {
11 Applets
Java
12 Applets
Java
Using Images
import java.applet.*;
import java.awt.*;
getImage() Methods /*
<applet code="DrawImage" width=280 height=280>
Image getImage(URL url) <param name="file" value="kids2.jpg">
Image getImage(URL base, String fileName) </applet>
*/
13 Applets
Java
Using Threads
public void run() {
try {
update() and repaint() Methods while(true) {
// Request a repaint
repaint() : request an update of the applet repaint();
display //Sleep before displaying next count
update() : clears the applet display with the Thread.sleep(1000);
background color and then invokes the paint() //Increment counter
method ++counter;
}
}
catch(Exception e) { }
public class Counter extends Applet }
implements Runnable {
int counter; public void paint(Graphics g) {
Thread t;
// Set Font
public void init() { g.setFont(new Font("Serif", Font.BOLD, 36));
15 Applets
Java
16 Applets
Java