Class 1, Part 2 |
Intro to Java 30-IT-396 |
|
The Corresponding Java Class
-
Many IDEs, such as Sun One, have an option to create an Applet. You
might want to do that.
-
It will create for you a class that extends Applet.
You should make sure that packages:
-
java.applet.Applet;
-
java.awt.Graphics;
are imported. If not, enter the import lines yourself.
-
This class will also have a method called init(), which is similar to our
public static void main(String[] args) method. init() is the first
method called by the browser.
-
Food for thought: do we still want a public static
void main(String[] args) method?
-
You can get the parameter values by passing the name of the parameter (from
the PARAM tag) to the method getParameter(String name).
-
Note that this method does not have an implicit
parameter. Why not?
-
To write text to the browser, make a method.
-
public void paint(Graphics g)
-
In that method, g.drawString, similar to the way you call System.out.println().
-
But, with drawString, you must specify the height and width as ints.
-
example: g.drawString("Celcius temperature is: " + celciusTemp, 50,
25);
-
How does this work? You're overriding the method called paint().
When the browser opens, or the browser is refreshed, Java calls the paint
method on your object.
-
If you do not have one, it simply does what paint does by default - paints
a blank screen.
-
If you do have one, it will perform whatever you put in the method.
In this case, we tell it to print a String.
-
A few more words....
-
There are much sexier interfaces that we can use. We can add buttons,
text input fields, scrollbars, and more. Just about anything you
would see in a GUI application. But that's pretty advanced, so we'll
save that discussion for Java II.
-
You have been warned... since most browsers use an old version of java
- usually 1.0.2 or 1.1, many of the features you are used to in 1.4.1 are
not available.
Example



Created by: Brandan
Jones February 15, 2002