Copy/paste this code into a file TestApplet.java
:
import java.awt.*;
import java.applet.*;
// this is a simple Java applet
// all applets are extentions of the class Applet
public class TestApplet extends Applet {
// method paint displays the graphics of the applet
public void paint(Graphics g) {
// set the Color for drawing (default is black):
g.setColor(Color.green);
// draw a rectangle:
g.drawRect(100, 100, 200, 50);
}
}
Copy/paste this into a file TestApplet.html
:
<HTML>
<APPLET CODE="TestApplet.class" HEIGHT=300 WIDTH=400></APPLET>
</HTML>
To run (on Windows):
- Open Command Prompt window. Go to the folder where your programs
are:
cd C:\myprograms
(replace myprograms by the folder
where you copied the files).
- Compile the code:
javac TestApplet.java
in the
Command Prompt window.
- Run it using appletviewer:
appletviewer
TestApplet.html
.
This is an example from CSci 1211 course.