|
1. The Hello applet
Programmers who want to try out a new programming
language usually first write a little program that puts a text like "Hello
World" on the screen.
The Hello program is usually not too complicated, so that the programmer can
easily see
what a simple program in the new language looks like. Just type the following
text into edit or notepad:
/*
A simple program in Java, which puts
the text "Hello World" on the screen
*/
import java.applet.*;
import java.awt.*;
public class Hallo extends Applet {
public void paint (Graphics g) {
g.drawString ("Hello World",
60, 30);
}
}
You can type this program into almost any editor and then save it as Hello.java.
Important: if you use a word processor like MS-Word, you should save it
as plain text under the name Hello.java (don't forget the extension!).
The compiler javac.exe
The program Hello.java is just text. We can read it, but on the Internet
it doesn't do anything special.
But with this text you can make a file - called an applet - that can be run by any good
Internet browser which is equipped with the Java Virtual Machine. Such
a file can be made with the help of a Java-compiler.
A Java-compiler can be downloaded for free, for instance from
here.
The compiler uses the text Hello.java to make Hello.class.
Let's have a look at the first few lines:
/*
A simple program in Java, which puts
the text "Hello World" on the screen
*/
These lines do nothing at all. If you write
/* before a piece of text and */ after it,
the compiler will skip over that piece of text. It is called comment and
only meant to be read by humans.
There is a different way to mark text as comment - that is to write two slashes
before each line, like this:
// A simple program in Java, which puts
// the text "Hello World" on the screen
Some Java programs can be compiled in such a way that they can be run by an Internet-browser.
The file
Hello.class (just like any other Java program for the Internet) is called
an applet.
An applet needs special information. In order to import this information, every applet
has to start with:
import java.applet.*;
import java.awt.*;
In order to make a Java program into an Internet program (an 'applet'), it must
get ("inherit") specific properties of the class
Applet. That's done by the expression: extends Applet,
as you see here:
public class Hello extends Applet {
The Hello-applet has only one part that has visible output, which is
paint.
Such a part of a program that performs a certain task is often called a function.
But Java programmers usually call it a
method.
When you hire a carpenter, he will usually bring a toolbox to do the job. The
method paint also needs a toolkit - which belongs to the Graphics
class, and is usually called g, but you may give it another name if you like.
One of the tools that you'll find in a toolkit of the class Graphics, is
called
drawString. We have to make it clear to the
compiler that this method comes from the toolbox g -
which is done like this:
g.drawString ("Hello World",60,30);
The numbers ,60,30 mean that the text
"Hello World" must be depicted 60 pixels from the left side and 30 pixels
from the top.
In this tutorial we'll use an applet window will be 320 pixels wide by 240 pixels
high.
Exercise 1.1
Install a workingJava development environment on your own computer.
|
Exercise 1.2
Write an applet which 'paints' your name five times: in the four corners of
the applet window and one in the middle. This must be done by using drawString
five times, each time with other coordinates.
|
To chapter 2
Main Menu Java Tutorial
To homepage
(c) 2003, Thomas J.H. Luif
| |