You are on page 1of 2

Java: A1 - First Applets

Page 1

Java Applets notes

A1 First Applets

The A series of notes concern Applets in Java. They are supported in various ways by the E (extended example), H (html), F (further Java) G (GUIs) and W (web background), series of notes.

Let's start learning how to build applets by looking at a couple of examples, and briefly explaining them. Next lecture will go into a bit more detail.

A1.1 A First Applet


Heres the classic "Hello world" program done as an applet: import java.applet.Applet; import java.awt.*; // Makes Graphics and drawString available. public class Hi extends Applet { public void paint(Graphics g) { g.drawString("Hello World", 20, 20); } } "An applet" is an instance of a class which extends java.applet.Applet. When the extension-of-Applet class has been compiled, a browser can run the resulting .class file - in effect the JVM in the browser implicitly instantiates the class and then starts the instance. An applet runs rather differently than a normal program: after the class has been instantiated, the browser occasionally calls certain object methods of the applet instance: class Applet provides null defaults for those methods, so the calls do nothing unless we provide over-rides for the methods; the above example provides an over-ride for the paint(Graphics) method of class Applet: this method is called shortly after the instantiation of the applet object. When an applet is instantiated, class Applet's constructor also instantiates an object of class Graphics (from the library java.awt), and associates that with the applet object (we don't need to know the details). The parameter of paint gives you a reference to the applet's graphics object.
Instances of Graphics are also automatically associated with instances of certain other java.awt classes - we'll meet a couple more in this unit. Note the difference between the words Applet and applet - confusing those is a common beginner's mistake.

Class Graphics provides a number of methods to draw in the display pane of the applet (in other words, in the part of the browser display window reserved for the applet). drawString is such a method - it takes three parameters: a string, to be "drawn" in the display pane; two integers, x and y, specifying where in the display pane the string is to be drawn: the x specifies the distance from the left edge of the pane to where the drawn string is to start; the y specifies the distance from the top of the display pane to the top of the drawn string; both measurements are in pixels. The string is drawn is a default font (face, size, and style) - there are facilities in java.awt to change the font used by subsequent calls of drawString. Obviously, we needed to import java.applet so we could extend Applet, and so that paint would be visible; likewise we had to import java.awt in order that we have visibility to java.awt.Graphics and it's drawString method.

A1.2 A Second Applet


This second applet (from Bell & Parr) illustrates how an applet programmer may include their own methods in the applet, in addition to paint(...). This applet draws different sized triangles, with the triangle drawing method being invoked from paint(...). import java.awt.*; import java.applet.Applet; public class TriangleEG extends Applet { public void paint(Graphics g) { drawTriangle(g, 80, 200, 100, 110); drawTriangle(g, 125, 220, 60, 70); } //end paint() method private void drawTriangle(Graphics g, int bottomX, int bottomY, int base, int height) { g.drawLine(bottomX, bottomY, bottomX+base, bottomY); g.drawLine(bottomX+base, bottomY, bottomX+base/2, bottomY-height); g.drawLine(bottomX+base/2, bottomY-height, bottomX, bottomY); } //end drawTriangle() method } //end TriangleEG class

The bold fragments of the above example are the only innovation - all they do is provide a method that is not an over-ride of an Applet method - so it's a "normal" method, just like methods in non-applet programs you've written previously.

http://userweb.port.ac.uk/~lesterk/java/lect/a1-first-applets.html

13/11/2010 2:31:11

Java: A1 - First Applets

Page 2

So all there is to say is that drawLine takes 4 integer parameters - x1,y1,x2,y2 - x1,y1 is a position to start drawing a line, and x2,y2 the position to end it: like the x,y of drawString, x's are pixel distances from the left of the display pane, and y's are pixel distances down from the top of the display pane. [So it's rather like the traditional x,y co-ordinates of maths, except that the y is down, whereas it's up in maths.]

Last updated June 2005

Back to the Java notes index page

http://userweb.port.ac.uk/~lesterk/java/lect/a1-first-applets.html

13/11/2010 2:31:11

You might also like