This document was written by CS 290W TA Joshua Kay and was last modified
Okay, let's look at Applet #2. We have already explained a lot of the basics, so we should be able to fly through it, right? :)We will lose some of you here, but that's okay. We're explaining this fact so that those of you who have a little more programmming experience can get jumpstarted. This is all really basic Java stuff, and relatively easy to learn. So don't worry, it will all be explained. :)
Lines1-4- Comments, Whitespace
Line 5-Note we are importing only what we need, and not the entire package!
Line 6- import java.awt.Graphics; - what is this? this is the Graphics portion of the awt package.
Line 9- Creating a new public Class named Welcome - note that the filename will then be Welcome.java
Line 11- public void paint(Graphics g)
What is this???? Well, it is a method of the Applet. What does it do?
Much as it sounds, this method paints onto the applet. Let's look at
the structure:
There are two things to know about the paint() method. First, we see that this method is declared as public, just as the Applet is. This makes it available to all classes and their methods. There is a second reason the method is public, though, and we will explain that in a moment.
When you write applets, there are several 'standard' methods defined in the applet superclasses that you will commonly override in your applet class. These include methods to initalize the applet(public void init() - from applet #1), to make it start running, to handle mouseEvents (clicks or movement of the mouse), etc.
One of these standard methods is the paint() method. The paint method actually displays your applet on screen. The default actually doesn't do anything - it's an empty method. By overriding paint(), you're telling the applet what to draw on an empty screen.
The second reason that paint() is public is because the superclass paint() - the empty method of the applet class - is also public. If you try to change this you will get an error when you compile the class.
Second, note that the paint() method takes a single argument: Graphics g. This is an instance of the Graphics class, which provides platform-independent behavior for rendering fonts, colors, and behavior for drawing basic lines and shapes.
Line 13 - g.drawString("Welcome to JAVA, BABY!",25,25);
This is easier than it looks. g is the parameter passed to the
paint() method. Just look at line 11. g is an object of the Graphics
class, and thus has access to all of the Graphics methods, for
example, drawString(). Hence, typing g.drawString is like saying
"Hey, g!, draw a String!"
The parameters sent with the drawString() method are the string to be "drawn" and the x and y coordinates for where you want the string to begin appearing.
Note that we had a public void init() in applet #1. What happened to it? Well, public void init() is much like public void paint() in that it's a predefined method of the applet superclass. It, too, is an empty method. Applet calls it first, and if nothing is there, then Applet simply moves on.
That's that! If you typed in this program, saved it as Welcome.java, compiled it, and plugged it into an HTML page you would get the same results as you saw earlier.
But what if we changed Line 13? We'll change the string... and change the class name, too, so that we have two separate Applets.