These classnotes are depreciated. As of 2005, I no longer teach the classes. Notes will remain online for legacy purposes

JAVA01/Introduction To Applets

Classnotes | JAVA01 | RecentChanges | Preferences

As was mentionned before, the primary use for Java programs is in web design.

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:

 <applet code="TwentyApplet.class" height=100 width=300>
 </applet>



Classnotes | JAVA01 | RecentChanges | Preferences
This page is read-only | View other revisions
Last edited May 27, 2003 9:40 pm (diff)
Search:
(C) Copyright 2003 Samuel Hart
Creative Commons License
This work is licensed under a Creative Commons License.