October 2012: I am giving this website an update. Do you have any suggestions for improvements or examples to be added? Feel free to email me with suggestions. I cannot promise to reply to your email, but I will eventually read it and try to improve this website.

This site has moved to a new location - please update your links :-)

Java Applet Tutorial

This site is a quick-and-dirty introduction to writing Java applets. A set of example applets are given to be used as exercises. Feel free to download the source code herein, try it out on your own machine, and modify it. Graphical and interactive aspects of applets are emphasized. If you have no programming experience at all, you'll probably need additional learning resources. Please see the note below for first-time programmers.

Before getting started, you'll need a compiler for Java, so that you can translate source code into something executable. You could, for example, download the Java Software Development Kit (abbreviated as JDK), which includes a compiler, utilities, example applets, and documentation. Be sure to get the JDK and not the JRE (Java Runtime Environment) -- the former allows you to compile Java programs, the latter only allows you to run them.

Many programmers also like to have an IDE (integrated development environment - a program that allows you to edit source code within a window and press a single button to compile and run your code) to help them work. If you download the JDK with Netbeans from Oracle's website, you'll have an IDE included with the development kit. Another popular IDE for Java is Eclipse (to work with Eclipse, it's probably best to first install the JDK, and next install Eclipse, so that Eclipse can detect the presence of the JDK during its installation process). Personally, I don't use any IDE, so I install the JDK without Netbeans or Eclipse, and run the Java compiler directly on a command line, as described in the first lesson below.

After getting a compiler, you can try out the examples in this tutorial. The first one, "Drawing Lines", walks you through the process of creating an applet.

Drawing Lines
Drawing Other Stuff
Color - introduces arrays
Mouse Input - introduces showStatus( ) and Vector
Keyboard Input
Threads and Animation - introduces System.out.println( )
Backbuffers - introduces Math.random( ) and Graphics.drawImage( )
Painting
Clocks
10  Playing with Text - introduces 2D arrays and hyperlinks
11  3D Graphics - introduces classes
12  Odds and Ends


All of these examples were designed to be small and, hopefully, easy to absorb.

If you're looking for books on Java, O'Reilly publishes some good ones, although the ones I've seen are most useful to people with at least a bit of prior programming experience. Java in a Nutshell by David Flanagan covers the basic features of Java, including the syntax of the language, data structures, object-oriented features, and threads. Determined novices with no programming experience may find it useful. Java Foundation Classes in a Nutshell by David Flanagan describes how to create GUIs with widgets and how to draw basic graphics, among other things. Java 2D Graphics by Jonathan Knudsen, also published by O'Reilly, discusses in detail the graphics features of the Java 2 platform.

First-time programmers will probably find that the explanations given in this tutorial are too brief and leave out too many details. Basic information on the Java language can be found here and here (try to focus on understanding the syntax of variables, expressions, loops, and flow control structures at first -- we hardly make use of any object-oriented concepts in this tutorial). There are some books on Java for first-time programmers: this site recommends Java: An Introduction to Computer Science and Programming by Walter Savitch et al., Problem Solving With Java by Elliot B. Koffman and Ursula Wolz, and Introduction to Programming Using Java: An Object-Oriented Approach by David M. Arnow and Gerald Weiss.

Terminology : Note that the term function is used in this tutorial in place of the more modern (and object-oriented) term method.