Chapter 1 Creating your first applet Let's get started right away by writing an Applet. We'll start with a classic. THe tradition in programing is to start any new language with a program that says "Hello, world!!" We'll use java to do exactly that. *The Hello World program PD: Insert screen shot of hello world in Netscape **Comments about Hello world screen shot This is an HTML page accessed in the normal way by a normal browser. The words 'Hello world' are NOT in HTML (although you can't really tell from the screen) An applet is embedded on the screen (in some browsers, it will be a different color than the rest of the screen) There is also some normal html on the screen *Applets from the HTML point of view PD: Insert screen shot of Hello.html in notepad Comments: Basically, this is a normal HTML page The only new thing is the tags It has a few interesting features code refers to the class file which contains the java code height and width determine how large to draw the applet on the screen Stuff between and will appear if the browser does not support java or java is turned off. The link points to the .java file, which contains the program in a readable form. PD: begin sidebar Why do we have Hello.java and Hello.class? Java is a unique language. It uses some sneaky tricks so that a program can be run on many different kinds of computers. Basically, the programs we will write are simply text files. They will all end with the .java extension. The computer cannot run these files directly. We will run them through a special program called a compiler, which will generate a .class file from the .java file. The .class file is the program that the browser can read. If it helps, you can think of it like this: Humans can read the .java file, computers use the .class version. Of course, you don't have to put a link to your java files if you don't want to, but I will do so in this book, so you can always look easily at the code. PD: end sidebar *Writing your java programs Java programs can be written in any text editor you wish. If you want, you can use plain old notepad, vi or any other text editor that came with your operating system. You might find that you want an editor with more power, such as emacs or some other 'programmer's editor.' It doesn't really matter, as long as you can save your files in plain text. You should probably stay away from word processors, though, because they make it unecessarily difficult to save in plain text form. *So, dig in to that code!! **Source code of Hello.java PD: insert screen shot of Hello.java in notepad Here is the source code (that's a fancy name for human-readable version) of Hello.java. It looks prety intimidating, but it's not as bad as it looks. Essentially, it is broken into a few major parts: comments (arrow to // lines) import statements (arrow to import lines class definition line (arrow to public class...) init method definition (arrow to public void ...) code body (arrow to add(new ...) structure ending lines (arrow to } lines) **Making Comments PD insert screen shot with // lines highlighted Ironically, the most important thing to learn in a programming language is the command which the computer ignores. Comments are used to leave notes to programmers inside the code. It's a great idea to use comments like this at the beginning of a program to describe who wrote the program, when, and what it is supposed to do. I will use comments also inside programs when I'm doing something tricky. Comments in Java start with the // characters **Importing libraries Java applets are shipped around the Internet, so size is very important. For example, you don't want to have to wait for the database modules to download if your applet doesn't use them. The core of java is very small. Use import statements to bring in libraries that will customize the version of java to suit your particular needs. You will almost always (OK, in this book, you WILL always) import the java.awt.* and java.applet.* libraries. ***Getting the Graphical Tools (arrow to import java.awt.*) awt stands for Abstract Windowing Toolkit. It is the special trick that allows Java to make GUI programs across so many platforms. Essentially, you will make an object like a text box or a button, and the awt will convert that to the appropriate object in the user's operating system. Since almost all applets are graphical, they will usually use the awt library. ***Getting the Applet Tools (arrow to import java.applet.*) the applet tools are a series of objects which java uses to make applets. The term 'Applet' means a special kind of application which can be run inside a web browser. Again, since this is a book on java applets, we will always have this line in our programs. There are java programs which are not applets, but that is another book (or two.) **The class definition line (arrow to public class ... ) ***public ***class ***Hello ***extends ***Applet **Setting up the playground (arrow to public void init line) ***public ***void ***init() **The meat of the program (highlight add(new... line) ***add ***new ***label ***("Hello World!!") ***the semicolon **Ending things ***ending the init function ***ending the applet definition ***indentation ***comments *Practicing with Hello.java It's a great idea to practice with a known quantity. Try typing in the code to the Hello.java program in a text editor and save it as "Hello.java" Warning: Capitalization and spelling are important!! If you refer to the file as "hello.java" and it is internally referenced as "Hello.java" it will not work!! End Warning *Installing the JDK The programs in this book are all designed for the Java 1.2 JDK, although most will also run with an earlier (1.1.8) compiler. The 1.2 compiler is included in the CD-ROM that came with this book. Install the JDK, if you haven't already done so. *Compiling your program Java programs are generally compiled from the command line. Exit to the command line by opening a DOS window if you're on a Windows machine or a command shell in the unix - Linux world. Move to the appropriate subdirectory and type 'javac Hello.java' *Fixing mistakes Errors are a part of life in programming. The compiler probably complained about something. Nine times out of ten, you either have not installed the compiler correctly (try to compile one of my programs from the CD to check this out) or you have made a mistake in typing. The error messages in Java are reasonably useful, if a bit cryptic. Programming languages are very picky. Be sure you have typed everything exactly correctly. *No news is good news. PD: insert screen shot of DOS screen javac Hello.java If the compiler did not complain, that is very exciting news. Now when you look into the directory, you will see Hello.class in addition to Hello.java. Hello.class is compiled into a format that the browser can read as an applet. *Writing some HTML PD: repeat screen shot of HTML page You're still not completely done. Java applets cannot run without a web page to 'live in' You can write java programs that 'stand alone,' but that is not the focus of this book. For now, all of your java programs will require a web page. Again, you can just copy the web page I have given you in your text editor. *Looking at the applet PD: insert screen shot of applet under netscape *Using appletviewer PD: insert screen shot of applet under appletviewer **Why use appletviewer?