|
Letter Applet
LetterApplet Class
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Font;
public class LetterApplet extends Applet {
Letter c;
public void init() {
c = new Letter("a", 36, 0, 0); // create a default letter
c.translate(50,50); // re-position the letter
}
public void paint(Graphics g) {
g.setFont(new Font("Helvetica", Font.BOLD, c.size));
g.drawString(c.name, c.x, c.y);
}
}
One declaration of an instance variable and two definitions of methods:
init: initialization of applet;paint: drawing the letter on the screenGraphics class, which is part of the AWT package, provides
methods for drawing simple geometric shapes, text, and images to the
graphics destination. In our case, the applet included in the Web page
comes with a Graphics object that represents
the pixels on the screen. The paint is
defined for any Graphics object, but it applies here when
the applet calls it for a screen refresh.
Font Class
You can give the Graphics object additional properties about
the font to be used when drawing text. The Font class in
the AWT package supports the choice of the font family
(Helvetica, Courier, ...), style (bold, italic), and font size.
For example, if you want to draw
the letter in boldface Helvetica font at size 36, then you create
a Font object with these properties and you make this the
font choice of the Graphics object:
In order to load thevoid paint (Graphics g) { g.setFont(new Font("Helvetica", Font.BOLD, 36)); g.drawString(c.name, c.x, c.y); }
Font class, you must insert the statement
import java.awt.Font; at the beginning of
the LetterApplet.java file.
randomize Method
void randomize() {
x = (int) (20 + Math.random()*80);
y = (int) (40 + Math.random()*80);
size = (int) (8 + Math.random()*36);
}
We use the mathematical method
random from the java.lang.Math
class. random generates a random floating-point number
uniformly distributed over the segment [0,1].
Because the package java.lang is automatically
imported it suffices to use the "short" name Math.random.
Because the random method yields an objects of type
double, i.e., a double precision floating-point number,
but the instance variables x, y, and
size are of type int, i.e., default-size integers,
we have to explicitly perform type casting by a command of the
following format
draw Methoddraw method
for this purpose. In the LetterApplet paint method, we will only refer to this draw method of
the Letter class.
Thevoid draw(Graphics g) { g.setFont(new Font(fontname, Font.BOLD, size)); g.drawString(name, x, y); }
drawString method draws the letter with given name
at position (x, y). If you had wished to underline
the character, assuming that the letter width is 10 pixels and that the
line should be two pixels lower than the character itself, then you
would have added the statement g.drawLine(x,y+2, x+10, y+2);.
Putting a box around the character could be accomplished with the
drawRect method. Other shapes are available in the
Graphics class.
Letter Class
import java.awt.*;
class Letter {
String name;
String fontname;
int size;
int x,y;
Letter(String c, int s, int a, int b) {
// constructor to initialize letter and position
name = c;
fontname = "Helvetica";
size = s;
x = a;
y = b;
}
void draw(Graphics g) {
g.setFont(new Font(fontname, Font.BOLD, size));
g.drawString(name, x, y);
}
void randomize() {
x = (int) (20 + Math.random()*80);
y = (int) (40 + Math.random()*80);
size = (int) (8 + Math.random()*36);
}
void translate(int a, int b) {
x = x+a;
y = y+b;
}
}
LetterApplet Appletg on to the draw method of
the letter. Moreover, we add a button with the title "randomize"
and an action method to handle the click on the button.
import java.applet.Applet;
import java.awt.*;
public class LetterApplet extends Applet {
Letter c;
public void init() {
add(new Button("randomize"));
c = new Letter(); // create default letter a
}
public boolean action(Event evt, Object arg) {
c.randomize(); // choose random properties of letter
repaint();
return true;
}
public void paint(Graphics g) {
c.draw(g);
}
}
Let us have a closer look at the new parts in the above Java code that are highlighted:
import java.awt.Button to our
existing group of import statements, we load all classes
of the AWT package at once via import java.awt.*.init method we
Letter object;action implements what should happen when the button is
pressed:
true to mark that the event handling
has been taken care of.