Your first look at an Applet!

This document was written by CS 290W TA Joshua Kay and was last modified

Okay, let's start programming! First you need to check out Welcome to Java. We here at the CS290w staff have written you an updated version of the classic 'Hello, World' program... actually we have written three different versions of it, and included the source code, too. Let's take a look at the source code for Applet #1

1 //WelcomeAgain 2 //Another SuperEasy applet from squash 3 4 //importing neccessary packages 5 import java.awt.*; 6 import java.applet.*; 7 8 //public class is the name of the program 9 public class WelcomeAgain extends Applet 10 { 11 TextField welcAgain=new TextField("Welcome Again!"); 12 13 public void init() 14 { 15 add(welcAgain); 16 } 17 } 18 19 //Are you starting to get the hang of it? ****NOTE line numbers ARE NOT present when you write the program. They merely appear here to ease the explanation. IMPORTANT! If you put line numbers as such in your program it WILL NOT COMPILE!

Okay, lines 1-2 are comments. Java has several commenting structures, adapted from C and C++. If you add //, then the rest of the line is a comment. For example:
//This line is a comment, JAVA STYLE!

In Java you can also use
/* This is a comment ... */.
Anything between the /* and */ will be commented out. For example:
/*whoo hgoo we
are commenting in java*/

Okay Line 3 is blank - this is known as WHITE SPACE in programming. White space is irrelevant - it is merely used by programmers as a way to make their code more readable. Compilers ignore White Space.

Line 4 is another comment, obviously. We are importing packages. This is much like importing libaries in other programming languages. These packages are predefined, or pre-programmed pieces that allow easy access to functionality in programs. Let's look at the packages...

Line 5 - import java.awt.*;
The java.awt package is the Java Abstract Windowing Toolkit. It contains the predefined graphics that Java already knows. We will get to more of this in a minute.

Line 6 - import java.applet.*;
The java.applet package contains all of the predefined methods of the applet classes. In other words, rather than explaining to our compiler every time that we are writing an applet, and what an applet is, and all of the neccessary mumbo-jumbo needed to communicate with the browser - we are merely importing the java.applet class, which already does all of this.

Are you confused by the *? This is common notation in computing. When you write import java.awt.*; you are saying "I want to import EVERY piece of the java.awt package. To explain further, in this program we merely need one piece of the java.awt package, and that piece is java.awt.TextField. However, rather that specifying this, we are importing the entire package. This does make the program somewhat bigger, but in small programs like this the compile time and runtime differences are negligible.

Line 7 - White Space

Line 8 -Comment

Line 9 - Whenever you write a java applet, you will have a public class. This class will be the name of your file as well, with a .java suffix. in other words, the public class here was named WelcomeAgain, so the filename was called WelcomeAgain.java.

Line 10 - { This is known as the squiggly bracket, and is paired with its counterpart on line 17. They contain the contents of every class, conditional statements, etc. Don't worry, you'll get the hang of this quickly.

Line 11 -
TextField welcAgain=new TextField("Welcome Again!");
Remember when we imported java.awt.*. Well, as we already explained, this contains java.awt.TextField - which is a predefined class, containing methods of its very own. Here we are creating an OBJECT of type java.awt.TextField. Don't worry about what this means, we'll get to it real soon. We Promise.

This is the basic way of creating a new object...
Classname objectName=new Classname(Parameters).

Thus, TextField is the Classname. welcAgain is the objectName. The '= new' means "Hey Java, dynamically allocate memory for me and use it to create this new object." The Parameters are whatever parameters the class looks for when you create a new object. In the case of TextField, the parameter is a string, enclosed in quotes, which will be the text represented inside the TextField.

Line 13 - public void init() - This is the method automatically called by the applet class when it initializes itself. By default, the method doesn't do anything other than allocate memory for the applet to run. However, we can override the method by adding things inside the infamous {} squiggly braces.

Line 15 - add(welcAgain); Okay, this is an important concept to get, here. Basically we have created an object of class TextField, right? Well, now we have to add that object to the Applet. Do you get it? We have this object, just floating around randomly. But people will look only at the final product, our Applet. So in order for people to see the TextField, we have to paste it onto our Applet by adding it. This is the same for Buttons, Labels, etc...

And that's it. You'll notice the {} that surround the contents of public void init(). This is the same as enclosing a class, as already explained. Also you may notice that some lines end with a semicolon(;) - this is neccessary. Anywhere you make a statement, it must end with a semicolon. There are exceptions with conditionals (if...else/do...while) but we will get to those later.