Java Applet Tutorial This site is meant to be 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. Designers and artists: this tutorial emphasizes visual and interactive aspects of applets. It was made especially for people wishing to create small, graphical, expressive forms with Java. However, 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 Sun's Java Software Development Kit (abbreviated as JDK or SDK), which includes a compiler, utilities, example applets, and a boat-load of documentation. (Mac users can try here, here and here.) Be sure to get the Java SDK and not the JRE (Java Runtime Environment) -- the former allows you to compile java programs, the latter only allows you to run them. After getting a compiler, you can try out the examples. The first one, "Drawing Lines", walks you through the process of creating an applet. Please note that lessons 9-12 are unfinished, as I have yet to get around to completing them. 1 2 3 4 5 6 7
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( )
8 Painting 9 Clocks 10 Playing with Text - introduces 2D arrays and hyperlinks 11 12
3D Graphics - introduces classes Odds and Ends
All of these examples were designed to be small and, hopefully, easy to absorb. If you couldn't find information here that you need, you might try Sun's website, which has copious documentation on Java, along with online tutorials and examples. You might also try the Java FAQ, a copy of which can be found by searching for "java faq" in any good search engine (or just try here -- the same site also hosts a tutorial). If you're looking for books on Java, O'Reilly publishes some good ones, although they are most useful to people with at least a bit of prior programming experience. Java in a Nutshell by David Flanagan (in its 3rd edition, at the time of writing) 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 (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. You might also try getting a feel for some basic programming concepts by learning a simpler language first, such as DBN. Terminology : Note that the term function is used in this tutorial in place of the more modern (and object-oriented) term method.
About these web pages : These exercises were a joy to prepare once I figured out how to write a Perl script that could take the documentation and .java source files I wrote and generate .html files with them. The script also generated the little navigation bars at the top of each exercise page, and made it easy for me to play with different color schemes. Thanks to Richard Streitmatter-Tran for encouraging me to create these lessons !
This tutorial is ( C ) 2000 Michael McGuffin, all rights reserved.
Exercise 1: Drawing Lines Here's the source code for a first applet: import java.applet.*; import java.awt.*; public class DrawingLines extends Applet { int width, height; public void init() { width = getSize().width; height = getSize().height; setBackground( Color.black ); }
}
public void paint( Graphics g ) { g.setColor( Color.green ); for ( int i = 0; i < 10; ++i ) { g.drawLine( width, height, i * width / 10, 0 ); } }
Start up a plain text editor and type in the source code. It's important that you use a plain ASCII text editor (such as "Notepad" on Windows, "SimpleText" on Mac, or "vi" or "emacs" on UNIX) to enter the source code; don't use a word processing application (such as "WordPerfect" or "Word") since they use a proprietary format for files. If you don't want to do any typing, you can download the source file. Save the file as DrawingLines.java. It's important that the filename match the class name in the source code. Now, you have to compile the source code to generate a bytecode file called DrawingLines.class. If you're using Sun's Java Software Development Kit, you can compile by typing javac DrawingLines.java at a command prompt (on Windows, this is done within an MS-DOS shell). Check that the .class file was indeed generated. Then, create a .html file containing the following line:
(If the .html file is not in the same directory as the .class file, you'll have to add a codebase="..." attribute specifying the path to the class file. More information on the