Professional Documents
Culture Documents
Page 1
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.
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.
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
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.]
http://userweb.port.ac.uk/~lesterk/java/lect/a1-first-applets.html
13/11/2010 2:31:11