In Java all variables, constants and methods ( routines / functions / procedures ), have to be inside a class.
In this example there's only one method in the class.
To function properly within a HTML-document in a browser, this is a public class, called Simplest, and it extends the class java.applet.Applet, a software component containing Java instructions , present in the Java library that's on your station's hard disk.
The class has an opening and a closing bracket. CURLY brackets.
Inside it is one method that receives one argument / parameter, a reference variable to a java.awt.Graphics component, also in the Java library on your station.
The argument is called gr.
The method also has its brackets ,....
and inside it two methods are called that are in the Graphics component .
Here we ask the Graphics object, referred to by gr, to draw a String starting at 25 pixels from the left side of the applet and 75 pixels from its top .
The statement is closed by a SEMICOLON .
To make the applet stand out in the web-page, the HTML document , the next statement draws a rectangle along its edges .
The browser's plugin initiates construction of the applet and the browser assigns it its proper place within the web-page .
The web-page ,Simplest.html, contains HTML.
In it, we tell the browser the name of the applet class ....
and how much space it should have within the HTML-document.
Simplest.class is the file that results from compiling ( translating ) .....
our code, that has to be .....
in the file Simplest.java, since the name of our class ....
is Simplest. The Simplest.class file contains Java instructions ( bytecodes ) that can be interpreted by a Java Virtual Machine, a software-simulated Java computer, available for every platform. This means that a Java class can run everywhere without the need to compile it again.
In this case it's an Applet extension, so it can run within a browser or viewer, as browsers can have Sun's JVM plugged-in or they detect the presence of the Java Runtime Environment. In both situations, Sun's J2SE JDK download has to be installed. ( Older browsers are an exception, since they have their own, but antiquated, JVM's )
=========== IMPORTANT : ===========
Java is CaSe-SensitivE, so Paint would not be recognized as the name of the paint method, for example. As a consequence, the method would then NOT be called, when needed.
Neither would
public int paint ( java.awt.Graphics gr, String str ) ...
be recognized , because it doesn't match the paint method's description exactly. ( More on this in the Overriding lesson )
======== NOTES ========
It's good practice , to
- start class names with a capital, like
Simplest, Applet, Graphics
- start method names with a lowercase letter, like
paint, drawString, drawRect
- start variables with a lowercase letter, like
gr
and to
- write constants entirely in capitals
MAX (not in the example)
======
The argument is called gr. We could have named it differently :
graphicalContextOfThisSplendidAppletForExample
but then we would have had to use this name instead of gr .
=====
The paint method is called when needed : when the applet becomes visible for the first time, or when it becomes visible again after having been covered or minimized.