1. Home
  2. Computing & Technology
  3. Java

Building Your First Java Applet

By , About.com Guide

2 of 10

Write the Java Source Code

Writing the Source Code

Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.

I’m using Notepad to create my Java source code file. Open up your chosen editor, and type in this code:

//Reference the required Java libraries
import java.applet.Applet;
import java.awt.*;

//The applet code
public class FirstApplet extends Applet {

    public void paint(Graphics g) {

      //Draw a rectangle width=250, height=100
      g.drawRect(0,0,250,100);

      //Set the color to blue
      g.setColor(Color.blue);

      //Write the message to the web page
      g.drawString("Look at me, I'm a Java Applet!",10,50);
   }
}

Don’t worry too much about what the code means. For your first applet, it’s more important to see how it’s created, compiled and run.

Explore Java
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Java
  4. Tutorials
  5. Write the Java Source Code

©2009 About.com, a part of The New York Times Company.

All rights reserved.