Tutorials : Brush Up on Basics :

Ben Black Ben Black has been programming Java for a year. His motto is once you have learned the basics, you have conquered the language. He is the main webmaster of the Java resource site Cookie Nest - www.cookienest.com.

Java Basic - Compiling & Running (Cont.)

Now that you know how to compile and run an application, an applet should be easy. But there are several ways in which you can do it. In fact there are three variations that you can do and I'm going to show you all. The first one isn't very practical if you're going to put it onto the web, but you might do it if you can't be bothered making a html file to display the applet.

Hello World Applet Source Code #1

import java.awt.*;
import java.applet.*;

/*
<APPLET CODE = hello_world.class WIDTH = 100 HEIGHT = 50>
</APPLET>
*/

public class hello_world extends Applet
{
  public void paint(Graphics g)
  {
    g.drawString("Hello World", 20, 20);
  }

 

Hello World Applet Bat File Code #1

javac hello_world.java
appletviewer hello_world.java

 

Download Files


Do the same as you did with the application download the files or copy and save, then run the bat file. You should see a window pop up with the words "Hello World" within.



You have just created your first applet. What the bat file does is call the Java compiler to compile the Java and then it uses the appletviewer (a program which comes with the SDK) to display it. Normally this wouldn't work, but since we put the applet  HTML code in between /* and */ the appletviewer can take this information and create a window for the applet to be in.

This technique is okay, but you should really use the next one (Hello World Applet #2) for when you want to display your applets on the web. Here is the code.

Hello World Applet Source Code #2

import java.awt.*;
import java.applet.*;

public class hello_world extends Applet
{
  public void paint(Graphics g)
  {
    g.drawString("Hello World", 20, 20);
  }

 

Hello World Applet Bat File Code #2

javac hello_world.java

 

Hello World Applet HTML Code #2
<HTML>
  <HEAD>
  </HEAD>
  <BODY>
    <!--"CONVERTED_APPLET"-->
    <!-- HTML CONVERTER -->
    <OBJECT
      classid="clsid:CAFEEFAC-0014-0000-0000-ABCDEFFEDCBA"
      WIDTH = "300" HEIGHT = "300" 
      codebase=
"http://java.sun.com/products/plugin/autodl/
jinstall-1_4_0-win.cab#Version=1,4,0,0">

      <PARAM NAME = CODE VALUE = "hello_world.class" >
      <PARAM NAME="type" VALUE="application/x-java-applet;jpi-version=1.4">
      <PARAM NAME="scriptable" VALUE="false">
      <COMMENT>
       <EMBED
         type="application/x-java-applet;jpi-version=1.4"
         CODE = "hello_world.class"
         WIDTH = "300"
         HEIGHT = "300" 
         scriptable=false
         pluginspage= "http://java.sun.com/products/
plugin/index.html#download">

        <NOEMBED>
        </NOEMBED>
        </EMBED>
      </COMMENT>
    </OBJECT>
    <!--
    <APPLET CODE = "hello_world.class"
      WIDTH = "300"
      HEIGHT = "300">
    </APPLET>
    -->
   <!--"END_CONVERTED_APPLET"-->
  </BODY>
</HTML>

 

Download Files


Note:Color coded lines have been split for display purposes

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.