Applet: program that runs inside a web browser
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import javax.swing.JApplet;
/*
An applete that draws two rectangles.
*/
public class RectangleApplet extends JApplet //You extend JApplet, not JComponent
{
public void paint(Graphics g) //It is not paintComponent
{
//RecoverGraphics2D
Graphics2D g2 = (Graphics2D) g;
//Construct a rectangle and draw it.
Rectangle box = new Rectangle(5, 10, 20, 30);
g2.draw(box);
//Move rectangle 15 units to the right and 25 units down
box.translate(15, 25);
//Draw moved rectangle
g2.draw(box);
}
}
<html> <head> <title>Two rectangles</title> </head> <body> <p> Here is my <i>first applet</i>: </p> <applet code ="RectangleApplet.class" width="300" height="400"> </applet> </body> </html>
1. Compile the RectangleApplet.java file
2. (a) Open html file by a browerser, ex. Firfox
(b) Run appletviewer like below
appletviewer RectangleApplet.html
