Creating Java Applets using
JCreator
Applets
are designed to be included in html pages and run either by a web browser such
as Netscape or Internet Explorer, or by Applet Viewer, a program that Sun
supplies with Java. In either case, you
have to create an html document. Since
applets use graphics and are run in windows, they use java.awt. The letters awt stand for the abstract windowing toolkit. Two files in the awt
must be imported into your
program. These are java.awt
and java.applet.Applet. They are in the Java library and the compiler
will find them and include them in your program.
Create
a new file as you did for an application, but this time type an applet. Notice that it extends Applet and that it does not have a main method. The paint method is used in this case to paint a picture on the screen. The methods setColor and fillRect are used to set the pen
color and then to draw an oval in that color.
The first two parameters in fillRect give the
coordinates of the upper left hand corner of the rectangle. And the last two give the width and height of
the rectangle. Type it in and save it on
your disk as Rectangle.java.
// An applet with a class that draws a rectangle.
import java.awt.*;
import java.applet.Applet;
public class Rectangle extends Applet
{
public void
paint (Graphics g)
{
g.setColor
(Color.red); //
This sets the pen color to red.
g.fillRec
(100, 100, 60, 40); // This draws a rectangle with location (100, 100) and diameter
50
} // method paint
}
// class Rectangle
Compile
your applet and correct any errors that the compiler finds. The resulting class file can be linked into
an html document using the <applet> tag.
Create a new file with the following html code. Then save it as Rectangle.html.
<html>
<head>
<title>Rectangle Example</title>
</head>
<body>
<h3>Rectangle
Example</h3>
<applet
code="Rectangle.class"
width="300" height="300"></applet>
</body>
</html>
Since
applets are part of
html pages, the html file is the one to be executed, not the java
file. So when you click on the run file
icon, you will bring up Applet Viewer and not the console window. The results should look like that below.
Now
try changing the size and location of the rectangle and see what you get. Remember to save and compile the applet class
and run the html file. You can also
change the color of the rectangle. There
are a number of colors recognized by the awt
including Color.green, Color.orange,
Color.blue, Color.cyan and Color.magenta. You
can try some of these. You can also draw
several rectangles by including several fillRect
statements. Make sure they are located
in different places or they will overlap.
Drawing
circles is very similar. The command
here is fillOval.
The oval is defined to be the largest oval that will fit inside of the
rectangle with the given location and dimensions. For example,
g.fillOval
(100, 100, 50, 50);
fill draw a circle that fits inside the rectangle whose upper left hand
corner is at (100, 100) and with diameter 50.
If
you decide to draw a circle, save it as Circle.java
and create an html file with the line
<applet code="Circle.class"
width="300" height="300"></applet>
When you run this, you should see a circle
at the same location as the rectangle you drew first.