Table of Contents

[ Intro | Begin | if/else | Loops | Arrays | Graphics | Animation | Mouse | Game | Real | Methods | Class | Class 2 | Applet | MouseClick | Thread | Button ]


Java Applets Tutorial

Java Applets are run inside Web pages and are used on the internet. They dont have a main method so they are not applications, they are web applets. Different.

Well let's start by doing an Applet and then explaining what goes on. Type this into your text editor:

    import java.applet.Applet;
    import java.awt.Graphics;
    public class HelloWorld extends Applet
    {
	// init method is used to initialize any variables, objects
	public void init()
	{
	    // empty for now
	}
 
	// paint does what it says
	// needed
	public void paint(Graphics g)
	{
	    g.drawString("Hello World", 20, 20);
	}
    }

Save as HelloWorld.java and then compile. BUT, Java applets must be compiled to Java 1.1, so we need to compile like this:

javac -target 1.1 HelloWorld.java

“-target 1.1” tells the compiler to compile as a Java 1.1 format, file.

Note: If you are using Java 1.5 then you dont need to use the target flag. Do it like this:

javac HelloWorld.java

How are we going to look at this? We need to make a html file to load our applet. soooo Create a new text file, save it as hello.htm and put this text in it:

<html>
<body>
<applet code=HelloWorld width=300 height=200>
</applet>
</body>
</html>

The bit that loads the java applet into the html page is of course the applet tag. Just put the name of the class after the attribute, “code”. Height and width attributes can be used as well if you need. Now open the HTML file and hey presto, the applet is in the web page. Shut down the HTML page before you do any recoding and then re-open.

Now, back to explaining the applet code.

import java.applet.Applet;

This imports our native Java applet class for us to use.

import java.awt.Graphics;

This imports our Graphics class, which is used for drawing lines, squares, circles, text.

public class HelloWorld extends Applet

This means that our custom HelloWorld class extends the Applet class. That is, it is a subclass of Applet. Therefore we have access to all the methods of Applet, and we can extend our custom class to do further things.

public void init() { }

This method is empty but is used to initialize any variables or objects that we will be using later. It is a necessary method, so let's get used to it.

public void paint(Graphics g) {

This method is our drawing method. This method draws anything that is in our applet to the screen. Note that we have passed an instance(g) of the Class Graphics to it. Later notice how we use the Graphics class to draw.

g.drawString("Hello World", 20, 20);

The drawString method is similar to the method we used in Judo. drawString is a method of the Graphics class. We have an instance of the Graphics class - g. drawString takes a String and an x and a y coordinate. What other methods do you think that the Graphics class has. Experiment with it using your knowledge from Judo. Hint - drawRectangle? fillCircle? Play with this.

How do you set the color? Do you remember how you did it in Judo? Same same. Experiment.

Hint:

g.setColor(Color.BLUE);
g.fillRect(0,0,100,200);

I will carry on with the Graphics class and do more stuff with applets. This is a very basic intro.

Java Colors

Colors for drawing are selected by using the java setColor method. These are the predefined colours.

Usage:

g.setColor(Color.red);
g.setColor(Color.GREEN);
g.setColor(Color.yellow);

etc..

Java Custom Colors

If you want to create your own custom colour, then you can use the Color constructor that takes red, green, and blue parameters. Like so:

Color(redLevel, greenLevel, blueLevel);

The three levels are on a scale from 0 to 255.

etc.. etc..

To use a custom color, write code like the following.

Color c = new Color(150, 250, 150);
g.setColor(c);

Java drawRect and fillRect

Similar to the judo drawing methods, these methods can be used to draw unfilled or filled Rectangles.

Templates:

drawRect(x, y, width, height);
fillRect(x, y, width, height);

Usage:

g.drawRect(20, 30, 200, 100);
g.fillRect(50, 50, 150, 100);

Try this new applet:

    import java.applet.Applet;
    import java.awt.*;
    public class RectPaint extends Applet {
	// initialize
	public void init() {
	}
 
	// paint
	public void paint(Graphics g) {
	    g.setColor(Color.red);
	    g.fillRect(20, 20, 200, 100);
	    g.setColor(Color.yellow);
	    g.drawRect(40, 40, 160, 60);
	}
    }

Compile it.

And then create your html file to load it:

<html>
<body>
<applet code=RectPaint width=300 height=200>
</applet>
</body>
</html>

Next we will do some more work on the Graphics class.

Resources