import java.applet.*; import java.awt.*; public class Demo extends Applet { Image image; int count; public void init() { image = getImage(getDocumentBase(), "World.jpg"); count = 1; } public void paint(Graphics g) { g.drawImage(image, 0, 0, this); g.setColor(Color.red); for (int y = 15; y < size().height; y += 15) { int x = (int) (size().width/2 + 30*Math.cos(Math.PI*y/75)); g.drawString("Hello", x, y); } showStatus("Paint called " + count + " time" + ((count > 1) ? "s" : "")); count += 1; } }You can get the source here. |
||||||
![]() |
Lecture 2 | Slide 14 | 6.837 Fall '98 | ![]() |
Since all Java code must be contained within an object, an object called Demo is defined in this file (here is a copy). This object is derived from a previously defined object called Applet. The Applet object encapsulates all of the basically functionality provided to, and available from, a web page. For more details about the methods available to an Applet see pages 233-236 of Flanagan. Often objects are extended in order to add new functionality, in the form of methods. In this case we aren't adding any new methods, instead we are overriding two existing methods, called init( ) and paint( ), that are inherited from the Applet base class. Thus, the Demo object that we define behaves much like an Applet, the only difference is in the modifications to the overridden classes. You don't even need to know any of the details of how a class works, beyond the interfaces to methods, in order to modify the original code. This is a simple example of the power of object-oriented programming.
The Demo class declares two new instance variables. The first variable declared is an Image object with the name image. Note that this declaration does not actually create an Image object. Instead the Image is created by the first line of the init( ) method. The second instance variable is a primitive int type named count. These variables are called instance variables because each individual instance of a Demo object will have its own copy of these variables.
The first method overridden by Demo is init( ). Init( ) has no declarations and only two statements. As mentioned earlier the first statement creates an Image object by calling the getImage( ) method. Do you have any idea to which object the getImage( ) method belongs? Here's a hint, any class may reference its own instance variables or methods without using a object prefix. Thus, getImage( ) must be a local method of Demo objects. However, there is no declaration in our code, this demonstrates another advantage of the OOP approach, the getImage( ) method was inherited from the Applet class, along with a whole lot more.
The second method declared by Demo is paint( ), and it expects a Graphics object as an argument. Locally, the Graphics object passed to paint( ) is referenced with the name g. The first two statements are calls to g's drawImage( ) and setColor( ) methods. The third statement is a for loop. The first statement of the for-loop declares the method variable y and assigns it an initial value. The second boolean expression establishes a loop invariant that must be true before the body of the for loop is entered each time through. The third statement of the loop updates the value of the y variable after each execution of the for-loop body.
The body of the for loop is a block statement containing a declaration,
and statement. The declaration of the x method variable which
initialized with a expression which makes reference to a Math
object. Notice, that no Math object
has been declared in the Demo object or any of its methods.
This is allowed because, the methods of Math objects are all declared
as static
thus making them available without declaring
an instance. We'll discuss this technique more at our next meeting.
The second statement calls the drawString( ) method of
the graphics object.
The fourth statement of the paint( ) method calls the Demo showStatus( ) method inherited from Applet. Notice the string concatenation used within the argument of the showStatus( ) call. There is an implied type conversion taking place that allows the int instance variable count to be converted into a String object (this is an example of where Java is a little less than strict in its type checking). Also, notice the use of the ? operator that I used to make my 7th grade english teacher happy, should she wonder onto this web page. The last statement of the paint() method updates the count instance variable.