Creating an Applet

Before you write an applet, you must know there are other applets and AWT(even
graphical image) that were already created for you. This keep you from having to
create everything from scratch. First you need to call up these graphical interfaces
and applets. To do this, you will need to type these lines in the your text.

import java.applet.*;

import java.awt.*,

The word import, means to send in. We're asking the program to send in the
applets and graphical images for us, into the applets we're creating. The * means,
all that are available to us. The semicolon tells the program that that's the end of
what I want you to do. Note: Semicolon are vital at the end of these lines.

The next part of our applet we're writing will be the information part. Like the
information printed on top of a music cassette, it tells you the title of the song or
album, the artist who sings it, and the company who made it. Will will be adding
text like these on our applet, to let other people know that we created this applet,
what its called(title), and what it does. To do this we must type in:

/**

*By XXX author

*Hello World

*This will display Hello ThinkQuest on the computer screen

*/

 

In these line we just written, labels our applet. /** tells our program that "I want
to include a label for my applet, and this is how is begins", each line with the *
tells the program, "this is part of my label, this is what I want my label to say." */ this tells your program "I'm done with my label." Note: You must remember that
you have to tell the program when you're beginning your label using the /** and
when you want to end using the */.

Remember when you import some applet in earlier. Well, we'll be using one of
them but we'll be adding something in to make it the way we want it. In the
upcoming next line, we want the program to use the HelloWorld applet, the
HelloWorld applet, when run, display the word HelloWorld. We will be changing
the text to what we want it to say. We will be typing this line in:

 

public class HelloWorld extends Applet { ... }

 

Let me explain word for word what you have just type in. The word "public"
means that this is available to everyone. "class" means we want to use the basic
instruction that's build into the applet. HelloWorld is the name of the applet we
want to use. The words "extends Applet" means that we want to do something
different from the original applet. With our {} we have ....., the brackets tells the
program, this is the beginning and end of what I want you to do differently. The ... is the many different instruction to tell the program what to do. This is what
we're going to replace the ... with. First we want to name what we want to display
and than associate the name with the instruction of how and what we want to display.

 

Label helloLabel = new Label ("Hello, ThinkQuest.") ;

 

In this line, I gave a name to the text I want to display, by typing in "Label
helloLabel". The "Label" part tells the program I want this to be named, and
"helloLabel", which we type in after the word "Label", the name we want. We put
in the equal sign to tell the program that this is what the name does. "new Label"
is telling the program to change the Hello World text from the HelloWorld applet to this,

we open(start) with a (

than quote "

what we want it to say

than unquote "

we than close it with )

and end it with ; to say that we're done with that line.

 

Now that we have told our program what to do and what to change, we must now
tell it when to do it, and how we want to do it. This is what we will type in:

 

public void init() {

setBackground (Color.red) ;

add(helloLabel) ;

}

 

The word public is saying we want to make it available for everyone. Void means
we don't want to see a result come back to us. This is important because
sometimes certain server might want a reply to the applet but for this applet, we
don't. "init()" tells the program to initialized(run) right away when the applet is
receive. We than tell the program, start like this, so we put a { at the beginning.
We told the program that we wanted a background color, so we typed in
"setBackground (Color.red)". You can choose any color you want. We end with a
; to tell the program that we're done with that line. The next line is
"add(helloLabel)", this tells the program to run the "helloLabel". If you don't
remember what the helloLabel does, refer back to the labeling part. We end the
line with ; to tell the program that we're done with the line. One the next line we
put in the } to tell the program that we're done with that section. We close our
whole applet with another }, telling the program, we're done with all the
instruction we want change. This is how your text should of looked:

 

import java.applet.*;

import java.awt.*,

/**

*By XXX author

*Hello World

*This will display Hello, ThinkQuest on the computer screen

*/

public class HelloWorld extends Applet {

Label helloLabel = new Label ("Hello, ThinkQuest.") ;

public void init() {

setBackground (Color.red) ;

add(helloLabel) ;

}

}

 

Notice that the second to the last bracket lined with the instruction it close. As
like the last bracket which lines up the the first instruction, "Label helloLabel", it
relates with(close for). This help organize things and let you and the viewer of
your program get a quick look at where an instruction starts and end. Now you're
done creating your first applet.

Save it as "HelloThink.java".

If you have the JDK, go to a DOS prompt, you then run the "javac DriveLetter:\Location Of Your Java applet\HelloThink.java" at the prompt. If all
goes well, when you compile this file, you will get "HelloThink.class".

On a Macintosh, you could drag-and-drop the file onto the Java Compiler.

Next

Home Page | Table of Contents | Introduction | Programming | Extras