appletviewer MyApplet.html
java MyClass
javac MyApplet.java
main()
method.extends
the Applet
class (in the
java.applet
package).public
.Graphics
object using the paint()
method.Graphics
object is displayed in the
browser or appletviewer window.Graphics
object is defined in the java.awt
package.import java.awt.Graphics;
import java.applet.Applet;
public class HelloWorldApplet extends Applet {
public void paint(Graphics g) {
g.drawString("Hello world!", 5, 25);
}
}
applet
tag appears within the body of the document
and needs a closing tag.code
attribute specifies the .class file
containing the byte code of the compiled applet.width
and height
attributes
specify the size of the applet within the browser window.codebase
attribute specifies the subdirectory
containing the .class file.name
attribute names the applets for use by
JavaScript and other Java programs.applet
tag is deprecated and the object
tag should be used instead.<html>
<head> <title>Applet: Hello World</title> </head>
<body>
<p>My Java applet says:
<applet code="HelloWorldApplet.class" width="150"
height="25"> </applet>
</body>
</html>
Return type, method, parameters | Description |
---|---|
void drawRect(int x, int y, int width, int height) |
paint a rectangle with upper left corner (x, y) and dimensions (width, height) |
void drawOval(int x, int y, int width, int height) |
paint an oval bounded by the rectangle (x, y, width, height) |
void drawArc(int x, int y, int width, int height, |
paint an arc along the oval bounded by the rectangle (x, y, width, height) |
void drawPolygon(Polygon p) |
paint a polygon p |
void fillRect(int x, int y, int width, int height) |
same as above but filled with the current foreground color |
void drawLine(int x1, int y1, int x2, int y2) |
paint a line from (x1, y1) to (x2, y2) |
void drawString(String str, int x, int y) |
paint the character string str starting at point (x, y) |
Return type, method, parameters | Description |
---|---|
Polygon(int[] xpoints, int[] ypoints, int n) |
construct a polygon p with n points. xpoints and ypoints are arrays of n integers. |
void translate(int dx, int dy) |
translate the polygon vertices by dx horizontally and dy vertically |
Rectangle getBounds() |
return the bounding rectangle of the polygon |
boolean contains(int x, int y) |
return true if the polygon contains |
Color
class (defined in java.awt
) defines
and manages colors.Color
object represents a color.Color.black
is 0,0,0 and Color.white
is 255,255,255.RGB Color | red | green | blue | cyan | magenta | yellow | black | darkGray | gray | lightGray | white |
---|---|---|---|---|---|---|---|---|---|---|---|
Int value | 255,0,0 | 0,255,0 | 0,0,255 | 0,255,255 | 255,0,255 | 255,255,0 | 0,0,0 | 64,64,64 | 128,128,128 | 192,192,192 | 255,255,255 |
Float value | 1.0,0.0,0.0 | 0.0,1.0,0.0 | 0.0,0.0,1.0 | 0.0,1.0,1.0 | 1.0,0.0,1.0 | 1.0,1.0,0.0 | 0.0,0.0,0.0 | .25,.25,.25 | 0.5,0.5,0.5 | .75,.75,.75 | 1.0,1.0,1.0 |
Return type, method, parameters | Description |
---|---|
void setColor(Color color) |
set the foreground color |
void setBackground(Color color) |
set the background color |
Color getColor() |
return the foreground color |
Color getBackground() |
return the background color |
Font f = new Font("TimesRoman", Font.BOLD, 36);
// creates a new
Font object
// sets the current font to f
g.setFont(f);
// displays a string in the
current font at the base point (50, 25)
g.drawString("Hello, World!", 50, 25);
Method | Description | When called | Browser event |
---|---|---|---|
void init() |
initializes the applet | just after the applet is loaded | the browser first views the applet |
void start() |
starts the applet | after the applet is made active | the user returns to the page |
void stop() |
stops the applet | after the applet is made inactive | the user goes to a different web page |
void destroy() |
destroys the applet | just before the applet is closed | the user exits the browser |
Return type, method, parameters | Description |
---|---|
URL getCodeBase() |
return the URL of the HTML file containing the applet tag |
URL getDocumentBase() |
return the URL of the directory containing the applet |
Image getImage(URL url, String name) |
retrieves an image from the specified URL and file name |
void drawImage(Image img, int x, int y, this) |
paints the image img at position (x, y) |
AudioClip getAudioClip(URL url, String name) |
retrieves an audio clip from the specified URL and file name |
void play(URL url, String name) |
plays an audio clip at the specified URL and file name |
Applets have no
command-line arguments because
they have no main()
method.
param
tags.<param name="myparameter" value="my value">
getParameter()
method.
String myvariable = getParameter("myparameter");
//
myvariable is set to "my value"