There are two kinds of Java programs, applications (also called stand-alone programs) and applets. Applets run inside a web browser .There are four methods in the Applet class that give you the knowledge about Applets and their behavior. Some of them we won't implement and we will just take the default ones provided by the Applet class.
init
This method is used for whatever initializations are needed for your applet. Applets can have a default constructor, but it is better to perform all initializations in the init method instead of the default constructor.
start
This method is automatically called after Java calls the init method. If this method is overwritten, code that needs to be executed every time that the user visits the browser page that contains this applet.
stop
This method is automatically called when the user moves off the page where the applet sits. If your applet doesn't perform animation, play audio files, or perform calculations in a thread, you don't usually need to use this method.
destroy
Java calls this method when the browser shuts down.
To write applets we need to import the following:
import java.applet.*;
import java.awt.*;
import java.io.*;
The paint function
Takes as argument an object of class Graphics. With the paint method you can use the Graphics object to display graphics on the screen.
Fonts and Text Font(String name, int style, int size);
public void paint(Graphics g){
Font f = new Font("Helvetica", Font.BOLD, 14);
// creating a new font Helvetica, bold of size 14 points.
g.setFont(f);
g.drawString("Something in the web", 80, 40);
}
Some standard Java Fonts:
Helvetica, TimesRoman, Courier, Dialog, Symbol.
Colors Color is a class.
Color (int r, int g, int b) //creates a Color object
setColor method selects a color that will be used for all the subsequent drawing operations.
void setColor(Color c) //changes the current color
Java Standard Colors
black, blue, cyan, darkGray, gray, green, lightGray, magenta, orange, pink, red, white, yellow
Drawing Graphical Shapes need to import java.awt.Graphics