Copyright (c) SEMM NL All rights reserved.
Author : Paul Hamaker. Part of JavaLessons.com
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 ( Firefox, for instance ) initiates construction of the applet and 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 ....
The Simplest.class file contains Java instructions ( bytecodes ) that can be interpreted by a Java Virtual Machine, a software-simulated Java computer, available for most platforms.
This means that a Java class can run almost everywhere without the need to compile it again.
In this case it's an Applet extension, so it can run within a browser with Java support or in a viewer.
Java is CaSe-SensitivE, so 'Paint' would not be recognized as the name of the paint method, for example.
As a consequence, this method would NOT be called. It would be accepted by the compiler, however.
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 )
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
- write constants entirely in capitals
MAX (not in the example)
The argument is called gr. We could have named it differently :
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.