applets example 1

Please read through the following Java code and the accompanying html code, together with the Java source code comments syntax-highlighted in green. Then read the notes that follow. It would also be helpful to read over the notes on the Color class at

// initial demo of applets

// Ian Downey 11 November 2006

// in a file called HelloWorld.java (compiled to HelloWorld.class)

// see below for notes.

import java.applet.Applet;

import java.awt.*;

// HelloWorld inherits all the properties of Applet and then adds a few of its own.

public class HelloWorld extends Applet

{

// code in the browser called the pain method below.

// many programmers call the painter object g; I’ve called it painter

public void paint(Graphics painter)

{

painter.setColor(new Color(255, 255, 0));

painter.fillRect(0, 0, getSize().width, getSize().height);

// 0, 0 refer to the coordinates of top left,

// getSize().width, getSize.height refer to the co-ordinates of bottom right

// send a message to painter to tell it to set its font to…

// send a message to painter to tell it to set its colour to red

// send a message to painter to tell it to put the words “H…” at coords. shown

painter.setFont(new Font("Sans Serif", Font.PLAIN, 32));

painter.setColor(new Color(255,0,0));

painter.drawString("Hello World", 5, 50);

painter.setFont(new Font("Serif", Font.BOLD, 32));

painter.setColor(new Color(0,255,0));

painter.drawString("Hello World", 165, 60);

painter.setFont(new Font("Monospaced", Font.ITALIC + Font.BOLD, 32));

painter.setColor(new Color(0,0,255));

painter.drawString("Hello World", 65, 70);

// the last number in the call to the Font constructor is the point size of the Font,

// in this case 32 point

// The coordinates in the call to the drawString method define the (x, y)

// values that are used to position the point at which painter starts painting.

// The origin of coordinates is the bottom left of the applet’s space on the

// html page.

}

}

An html file to be placed in the same folder, on the server, as HelloWorld.class:

<html>

<head>

<title> Java applet test </title>

</head>

<body bgcolor="#C0C0C0">

Please click <a href="applet.doc">this</a> link to see the Java source code and html file that would

produce the text <i>HelloWorld</i> three times, that you see here:<p>

<applet code = "HelloWorld.class" width = "356" height = "89"</applet>

<p>You see that the writing appears in different colours and in different styles.&nbsp;

Experiment further!</p>

</body>

</html>

Consisder, now the line in the above html code:

<applet code = "HelloWorld.class" width = "356" height = "89"</applet>

When the browser comes to execute this code, it constructs an object of type HelloWorld, using values 356 and 89 as arguments. The two values define the length and the height of the box on the html page in which the applet will run. The browser then sends a number of messages to the new object. Among those are paint and init. (The browser automatically sends a batch of messages to any new Java object that it is just after constructing.) In the case of our applet here, the new object can responds to the paint message but not to the init message or to any of the other messages. That’s because the only method that we have in the HelloWorld class is the paint method. That then is the only message that objects constructed to be of type HelloWorld can respond to.

Applications have public static void main.

Applets have public void paint instead and/or public void init etc instead.

Each application consists of one or more packages, each with one or more files, each with one or more classes, each with one or more methods. Exactly one is main.

Each applet consists of one or more packages, each with one or more files, each with one or more classes, each with one or more methods. Exactly one is paint or init or etc.

Our paint method is called by software written by those who wrote the web browser. We have to set up a parameter of type Graphics to accommodate the argument that will be inserted into it. It’s good that our paint method is given an object of type Graphics, because it enables us to call a whole host of methods that belong to the Graphics class. We do this, of course, by sending messages to the Graphics object such as drawString. That, as always, makes a call to the method of the same name.

To explain the ideas in the last paragraph in more detail: We must suppose that there’s a program in the Internet Explorer or Netscape browser that has the usual public static void main method. One thing that this program does is to construct an object of type HelloWorld and then send a paint message to that object together with an argument of type Painter. That’s where our software comes into play. We’re providing software that runs hand in glove with the system software.

additional points to note:

ConsidergetSize().width in the software above:

The getSize method has a return statement that returns an object of type Rectangle similar to our own Rectangle class. Thus the expression getSize() is of type Rectangle. Rectangle objects each have double values width and a height.

Hence getSize().width can be read as the width of the Rectangle that the getSize method returns.

By way of comparison, reconsider the PointCalcs.add method for our Point class software. It returned a Point. Thus we could have had:

PointCalcs.add(p1, p2)._dX

Meaning the x part of the Point got by adding p1 to p2.

Note that the following line:

painter.setColor(new Color(255, 255, 0));

could be replaced by two

Color yellow = new Color(255, 255, 0);

painter.setColor(yellow);

There is, however, no virtue in doing so except for the sake of understanding better what’s going on. I’ve chosen to call my Color object yellow. I could have called it redGreen, or some nonsense name such rabbitStew, or whatever I wanted. It just seemed an obvious choice to call it yellow.

Exercise:

Divide up the following line into two lines:

painter.setFont(new Font("Sans Serif", Font.PLAIN, 32));

summary of how applets are run:

(possibly repeating much of what has already been written):

Whena webbrowser carrying out the instructions in some html code, sees a reference to a Java class (<applet...>...</applet>), it constructs an object of that class (a class that you or I or another programmer wrote) and immediately sends some messages to it.

Among these messages is a paint message. If we want our applet to respond to that paint MESSAGE, then, of course, we must supply a paint METHOD.

(Aside: We’re well aware at this stage in our studies that when we send a message to an object, there must be a method in the object’s class to respond to that message. For example, if we send a print message to a Point object, then the print method in the class Point is executed.)

We must also supply a parameter to receive the Graphics object that the browser will send as input. In the code above, the parameter is called painter.

Our paint method code then takes over and sends messages to painter (the Graphics object) telling it to dip itself in yellow paint, take on this or that Font and then to paintsome sort oftext into thebox, on the web page,in which the applet is to run.

If an applet doesn't have a paint method, then the applet ignores the paint message that Windows/Internet Explorer ALWAYS sends to an applet. Many applets have purposes that have nothing to do with painting their boxes; they might do something else like cause a piece of music tobe played.

some interesting things to consider about the above code:

1. Note that I’ve made the background of the html page grey and the background of the Java applet window yellow, so that you can see where the Java applet window is. In practice you could have both of these with the same colour so that the applet blends into the html page.

2. It’s rather curious that whereas in first year we were very often writing code that made calls to methods that were part of the background computer system, now we’re writing methods that system software, in this case a browser, calls. A rather strange inversion!

3. We see from:

painter.setFont(new Font("Sans Serif", Font.PLAIN, 32));

painter.setColor(new Color(255,0,0));

painter.drawString("Hello World", 5, 50);

  • that the Font constructor expects to get a string , and then two integers, the first of which is defined in the Font class itself.
  • that the setFont method expects to get a Font object
  • that the Color constructor expects to get 3 integers
  • that the setColor class expects to get a Color object
  • that the drawstring expects to get a String and two ints.
  • that the setFont, setColor and drawString methods all have return type void.

In fact the setColor method is also happy to get an int, namely one of the constants defined in the Color class.

Thus we could have had painter.setColor(Color.red);

The setColor method has two definitions of itself and it uses one of them if it is handed a Color object, and the other if it’s handed an int.

some questions and answers:

Q1. How does the browser respond to the following html code?

<applet code = "HelloWorld.class" width = "356" height = "89"</applet>

Answer:

It constructs an object of type HelloWorld, passing to the constructor the width and the height specified. Then it sends about six standard messages to the newly constructed HelloWorld object. Among those are init and paint. In the case of our example, all six are ignored except for the paint message. The new object is able to respond to the paint message because there is a paint method in the HelloWorld class that we wrote.

Q2. Does the browser always send these six messages (including paint and init) to any newly constructed applet object.

Answer:

Yes, it does, even although some of them will be ignored.

Q3. Does the newly created applet object have to be able to respond to at least one of the messages sent?

Answer:

Yes, it does; otherwise it has no way of starting itself. It would be a useless applet object if it weren't able to respond to at least one of the standard messages.

1