Java programs that run from web pages are called applets, and they have a fundamentally different layout than standard Java programs. You'll recall that typical Java programs consist of a main() block that is run when the program is run. In applets, there is no main() block, but rather there are several different sections that are handled depending on what is happenning in the applet.
Two of these sections are init() and paint().
init()
init() is short for initialization, and is used to take care of anything that needs to be set up when the applet first runs. This can be initialization of variables, setting up connections, etc.
paint()
The paint() block is for anything that will be displayed when the web-browser actually renders (paints) the page.
Example
import java.awt.*;
public class TwentyApplet extends javax.swing.JApplet {
int number;
public void init() {
number = 20;
}
public void paint(Graphics screen) {
Graphics2D screen2D = (Graphics2D) screen;
screen2D.drawString("The number is " + number);
}
}
This example writes a number onto the screen. In order to use it, you must first compile the applet:
C:\> javac TwentyApplet.java
and then you need to have HTML that refers to the resulting class: