Sample Code
The following code is a portion of a simple applet that I will use to demonstrate the 4 major types of objects in Java and their methods. Click on each link to reveal a popup description of what that portion of the code is about. I have seperated the sections by color. Some colors have an brief comment enclosed in the official commented out symbols of /* comments */ and then a clickable explanation that goes into greater detail. On other colors, the code itself is clickable.

/* This portion of the code in green imports the needed packages and classes to be used in the applet. See explanation*/
import java.awt.Graphics;
import java.awt.Font;
import java.awt.Color;
import java.awt.Image;
import java.applet.*;

public class Example1 extends java.applet.Applet implements Cloneable {

/*This portion of the code in red names the variables for class Example1. See explanation.*/

     Font f1 = new Font("Courier", Font.BOLD, 20);
     Font f2 = new Font("TimesRoman", Font.PLAIN, 20);
     Font f3 = new Font("Arial", Font.ITALIC, 24);
     String text1 = "This is a simple Applet!";
     String text2 = "But it serves our needs!";
     String text3 = "Rholl";
     Image glad;

public void paint(Graphics screen) {

/*This portion of the code in purple names the submethods for the method paint() The first 9 lines are for drawing 3 strings of text. See explanation.*/

     screen.setFont(f1);
     screen.setColor(Color.white);
     screen.drawString(text1, 5, 50);
     screen.setFont(f2);
     screen.setColor(Color.yellow);
     screen.drawString(text2, 5, 100);
     screen.setFont(f3);
     screen.setColor(Color.green);
     screen.drawString(text3, 50, 200);
/*This portion of the code in gray is a continuation of the paint() method and draws a simple smiley face graphic and for placing a jpg image on the screen. See explanation.*/
     screen.setColor(Color.red);
     screen.fillOval(50,220,50,50);
     screen.setColor(Color.yellow);
     screen.drawArc(56,232,15,15,130,-100);
     screen.drawArc(76,232,15,15,50,100);
     screen.setColor(Color.blue);
     screen.fillOval(62,235,7,7);
     screen.fillOval(78,235,7,7);
     screen.setColor(Color.black);
     screen.drawLine(73,240,65,248);
     screen.drawLine(65,248,70,248);
     screen.setColor(Color.pink);
     screen.fillOval(61,253,25,5);
     screen.setColor(Color.black);
     screen.drawArc(66,254,14,2,170,240);
     screen.drawImage(glad, 150, 150, 90, 119, this);

}

/*This portion of the code in maroon overides the automatic method init() to add new qualities to the init method. See explanation.*/

public void init() {
     Color back = new Color(64,127,127);
     setBackground(back);
     glad = getImage(getCodeBase(),"jr.jpg");
}
}

So, this is the source code, the .java file that you write in your text editor. When you compile the above code it looks like this in a text editor(sample portion only):

While most of the code may be unrecognizable to a human, this language is loved by the machine, the JVM. It is this language that you actually have to load onto your webserver and link to in the html document. This file could be named Example1.class.

You also need to write some html code to call this applet much like you would write html code to call an image. Here is the html code for this applet:

An applet tag is very much like an image tag except you have a closing tag. You can put applet tags in tables, new windows, etc. I recommend a line of text like that in blue for those old browsers that can't see Java or have it turned off. Otherwise that user will get nothing. You can also add attributes and pass parameters into an applet tag. We will work on that later.

And finally here is the output of the simple applet I wrote:



Index Grading Schedule Syntax JavaDocs Video Projects Links