In this course, you will be creating Java applets, which are programs that can be run over the World Wide Web. A Java applet requires a web page in order to view it. And, in order for everyone to see your applet, both the web page and applet code must be saved in your public_html directory within your acpub account. In this course, we will put all of our Java programs in separate directories within the course directory, cps4, you created earlier within your public_html directory. Each time you transfer your work into your cps4 directory, using FTP, it is important that you create a separate directory for each assignment or you will eventually make a mistake and copy over your previous work.
Additionally, it is important that you create your own temporary working space on the classroom machines. For example, before starting each exercise, create a directory whose name is the combination of you and your partners acpub IDs within the local directory C:\classes\cps4. Without such a system, you may lose your work because you saved over someone else's work or transfer the wrong file (that had the same name, but different content).
Once you have selected a kind of project, you must name your project and pick some place to save it on
the classroom machine. For this exercise, please use the project name Picture and save it in the directory
C:\classes\cps004\
When you open a GP Applet project, you should find the following code in the file Applet.java. Every
program you write this semester will include at least this code since it is the starting point of your Java
programs.
For example, you should note that the code shown above is highlighted with colors when viewed in the
Visual J++ editor (blue words are keywords in Java, meaning you cannot name anything in your code
with the same name, and light green words are comments, meaning they are ignored within your
program). Additionally, the editor will attempt to indent your code properly so that it is easier to read.
However, unlike word processors, none of this information is stored when the code is saved because it is
only used to help make your coding easier --- not to present your code on the web or in a book.
You should now edit the code shown above, that appears in the file Applet.java within your project to
resemble the code shown below. Be careful that you type it just as it appears because the punctuation and
case is very important in Java. You will notice sometimes after you type return to end a line, the cursor
will automatically move to the next line properly lined up or indented. This is a feature your should get
used to using because when it does not move to the correct position on the next line, the editor is trying to
let you know you have not typed something correctly above that line.
Until you save your file, it is stored in a temporary buffer (the region in Visual J++ in which you are
typing). You should save your project often to ensure none of your changes are lost inadvertently. You
will not be granted any leniency if you lose your work because you did not save it properly. You can save
your project by selecting "Save All" from the file menu. Note, you chose where to save your project when
you created it at the beginning of the tutorial. Everything you work on within the project will be saved in
that folder, so you should not attempt to save individual files separately.
If there is a syntax error in the program, a message will appear in the Task window at the bottom of the
environment. To find the error within your code, simply double-click on the error with the left mouse
button. This will take your editor's cursor to the line on which the error appears, so you can use it to cycle
through multiple errors, fix them, and then recompile. If there are multiple error messages, fix the first
one only and then recompile. Often one error causes the compiler to list additional errors that are
based solely on the first error.
An example of an error message is:
Also note that while you are typing, most syntax errors are underlined with a red squiggly line, much like
spelling errors are highlighted in modern word processors. If you hold your mouse over the underlined
code, a small balloon box should appear describing the guess as to the problem. This can help you fix your
errors even before compiling! If you do not understand the error message, talk to one of the course staff.
When the program has been successfully compiled continue with the next section. The compiled code is
automatically saved in a file called Applet.class In general, a file with a .class extension is created for
each Java class you have in your project (you can verify this by opening your project folder using
Windows Explorer). The compiled code is important when running your program over the WWW, but it
can always be recreated from your original source code by building your project again.
In order to view your program, you should click on the first option from the Debug menu, Start. This
should start a browser for you pointed at the index.html web page that is part of your project. It may take
a few seconds for your applet to appear, but be patient. You can watch the status information at the
bottom of the browser window to verify its progress. If the browser does not start automatically, a dialog
box will appear asking you to verify that you want to view the results using your web page. In this case,
select index.html from the choice box in the middle of the dialog and click the Okay button.
If you have done everything correctly, you should see the following:
Since most everything in Java is an object, we must also create an object to represent the attribute we
want to change about the shape. Likewise, we must name it so that it can be referred to later. Putting it
all together, for example, you can set the position of the oval by calling its SetPosition function and
passing it a GP.Attributes.Coordinate object after you have created it using new, like this:
You can find a list of shapes and their attributes
here.
/*
* Authors: YOUR NAME HERE
* YOUR PARTNER'S NAME HERE
* Date: TODAY'S DATE
* Course: CPS 4
* Purpose: Practice Java
*/
public class Applet extends GP.Containers.Applet
{
public Applet ()
{
}
}
Be sure to include your names at the top of each class you write.
Editing Java Code
To type in your program you need to use a plain text editor (as distinct from a word processor that
typically adds extra information to the text you type to make it appear in different styles). The center of
the Visual J++ environment is one such text editor, although you may also use Microsoft's Notepad or
something similar. The advantage of using the editor provided with Visual J++ is that it "understands"
the code you write and attempts to help you as you create your program.
/*
* Authors: Robert Duvall
* Date: July 9, 2001
* Course: CPS 4
* Purpose: Practice Java
*/
public class Applet extends GP.Containers.Applet
{
public Applet ()
{
new GP.Shapes.Oval();
}
}
Be sure to use your own names at the top of each class you write .
Building a Project
In order to view the results of your Java applet, you must compile, or build, your project. A compiler
transforms your reasonably human readable Java code into machine readable code the computer can
understand. You can build your project by selecting the first option from the Build menu.
Undefined name 'GP.Shape.Oval'
This message tells you a guess as to what is wrong (in this case, it does not recognize the name you have
used (because you are missing an 's' on the end of Shapes, a common error when using GP)). You do not
need to worry about what line the error occurs on since clicking on the error message takes you there. If
you do not have any errors with the first compile, you might want to modify the program so that there is
an error. For example, remove a ";" and then compile the program.
Viewing your Project
Finally, we are ready to view your applet! An applet is a program designed to run within a web browser. It
should be viewable in any current browser although it may sometimes behave slightly differently within
different browsers depending on their Java implementation.
Once you have seen that your applet does what you expect, you need to close the browser to return to the
Visual J++ environment. Do this by clicking on the "X" in the top right corner of the browser or by
selecting the End option from the Debug menu.
Changing Shapes Attributes
If you did everything correctly in the previous section, then you should have seen part of a black oval
appear in the upper left corner of the window --- not very appealing. However, you can change the size,
color, and position of the oval by setting its attributes. However, before you do this, you need to make a
name for your oval so you can refer to it later. We can do this by changing our applet as follows:
/*
* Authors: Robert Duvall
* Date: July 9, 2001
* Course: CPS 4
* Purpose: Practice Java
*/
public class Applet extends GP.Containers.Applet
{
public Applet ()
{
GP.Shapes.Oval oval = new GP.Shapes.Oval();
}
}
The first "word" in the line now refers to the type of the variable, or the kinds of things to which this
name can refer. The second word, is the variable's name. The equals sign, =, does not have its standard
mathematical meaning here, but instead equates the name of the variable on the left side with the thing
created on the right side. Because of this, we call this an assignment statement and read it as "oval is the
name for a new Oval". Right now, it may seem redundant to put the word GP.Shapes.Oval on both sides
of the equals sign, but we will see why this might be useful later. Additionally, note that Visual J++ helps
you write out the long object types by producing pull-down menus that include the available choices after
you type each period.
/*
* Authors: Robert Duvall
* Date: July 9, 2001
* Course: CPS 4
* Purpose: Practice Java
*/
public class Applet extends GP.Containers.Applet
{
public Applet ()
{
GP.Shapes.Oval oval = new GP.Shapes.Oval();
GP.Attributes.Coordinate center = new GP.Attributes.Coordinate(200, 200);
oval.SetPosition(center);
}
}
This places the oval in the center of the applet because the applet's total size, by default, is 400 by 400.
How big the applet appears actually depends on the resolution of the monitor on which you are viewing
the applet. When you create a GP.Attributes.Coordinate object, you specify how far to the right (the x
component) and how far down (the y component) from the top left corner you want to move.
Unfortunately, this is the opposite of how you are used to coordinates in standard geometry. Note,
eventually this process of creating objects must stop somewhere. So, Java lets you create numbers, and a
few other things, by simply typing them literally into your program.