Home E-Books Special Effects and Game Development in Java How to compile

Special Effects and Game Development in Java(TM) - How to compile and run your applet 

by Anibal Wainstein

2.0.3 How to compile and run your applet 

Save the code example in the last section as an ordinary text file (it must be in ASCII and not in Word or similar format). All the applets require that they are embedded in a HTML page in order to work. Write the following bit of HTML code into a separate file:

<HTML>  
<HEAD><TITLE>My first applet</TITLE></HEAD>                       
<BODY>  
<APPLET CODE="myapplet.class" WIDTH=100 HEIGHT=100>                       
</APPLET>  
</BODY>  
</HTML>  

Here we specify that the applet shall use the myapplet.class file as base and where the width and height is 100 pixels each. I guess that you already understand how HTML works so I will not explain what the rest of the text means.
Save and name the file to "myapplet.html" in the same folder as the Java file.

Depending if you have Sun's or Microsoft's Java development system, you open a DOS window in Windows and write the following into the folder where your Java and HTML files are: 

\pathtojavac\javac myapplet.java     (Sun's JDK) 
\pathtojvc\jvc myapplet.java   (Microsoft's SDK) 

The compilator will examine the file and notify you about the errors it finds and where they are. A common error that everyone may encounter is to forget the semi-colon after a Java statement.

Now you can open the HTML file "myapplet.html" with your favourite web browser. If you use Netscape, you can open the Java console under the program menu (look under tools in Netscape 4.X). The same thing applies to Internet Explorer 4.01 or 4.02. However in version 4.0 of Internet Explorer you must make sure the Java logging option is on before you look at the applet (this will you find under the IE advanced settings). The Java log is written as a text file under the c:\windows\Java\ folder. If you want to spare yourself of this trouble then just upgrade to version 4.01 or 4.02.

You can also click here to see the applet.

2.0.4 Quick ways to show results (showStatus() and println()) 

The Java method println() belongs to the PrintStream class, under the java.io package (IO stands for "In Out" and is used to note communication with the user or the hard drive). You do not have to lay much thought on this, because when a Java Virtual Machine is started you have automatically a println() method ready under the System class and under its communication class out. The class out is pointing on the Java console. By using out you can easily check the values of your variables.
In section 1.2.3 we looked at the arithmetic operators: 

int a = 4;
int b = 2;
//The number 6 will be stored in "c"
c = a + b;           //The number 2 will be stored in "c"
c = a - b;         //The number 8 will be stored in "c"
c = a * b;       //The number 2 will be stored in "c" c = a / b;           
Unfortunately we could not verify that the result was correct. The method println() exist for many types of variables. Write the following applet: 
import java.awt.*;

public class calculationapplet extends Applet {

public void init()
{
    int a = 4;
    int b = 2;
    c = a + b;              
    System.out.println(c);
} }
After writing this and compiled it you will be able to see the number 6 on the Java console. Now you may wonder how it may be that the same println() could write a number, when the same method in the last example wrote a string. This is called function overloading. They are NOT the same methods even though they are named the same. If two methods are named the same but have different arguments, Java will see them as two unique methods. However you cannot have two functions that are named the same and where both require identical arguments. The following println() methods are some of the most used: 
println(boolean)  
println(String)  
println(int)  
println(float)  
println(double

When you run and applet on a web browser you have also access to the status window that is used to be located at the bottom of the screen. There is a method under the Applet class that is called showStatus(). With this you can show strings. Let us take the "Hello Sweden!" example in section 2.0.2 and rewrite it: 

import java.awt.*;  
import java.applet.*; 
public class myapplet extends Applet { public void init() { showStatus("Hello Sweden!"); }

Compile it, run this applet on your web browser and look at the status window. Please note that depending on the web browser you have, this applet may fail because other system messages may overwrite your status bar message. Therefore showStatus() may not always be good to use if you do not update your message continously. We will later see how you can have fun with it anyway.

 


Next Page >>