Applets and Methods

Tom Kelliher, CS 116

Sept. 11, 2000

Administrivia

Announcements

Assignment

Read Section 2.3.

From Last Time

Lab recap, intro. to applets.

Outline

  1. Methods: declarations, parameters, inheritance, overriding. Calling.

  2. Comments.

  3. Add class example.

Coming Up

2-D graphics programming in Java.

Applets

Continued from last time:

  1. method declaration:
    public void paint(Graphics g)
    {
       // ...
    }
    
    1. Meanings? Parameters, return value.

    2. The signature never changes.

    3. Method declaration, formal parameters.

    4. Inheritance and Overriding.

      We will nearly always override paint(), which is defined in the Component class:

      Working up the class hierarchy to find a method definition.

  2. Calling an object's method: g.drawString("Hello, world!", 20, 10);
    1. General form:
      returnValue = objectName.methodName(parameter list);
         // "Parameter" is a synonym for "argument."
      

  3. Comments: Purpose of comments. Ways to lessen the need for comments.

Summarizing

  1. What does every applet require, in terms of statements, declarations?

  2. What must a statement end with?

  3. How is a class delimited? A method?

Extending

Purpose of an applet's paint() and init() methods, within the context of MS Windows.

Apply this to Colors.java.

Another Applet Example

Observe:

  1. Adds two numbers, displaying sum.

  2. Documentation conventions.

  3. Instance, local variables.

  4. init() method vs. paint() method.

  5. String concatenation in drawString().

  6. The add() method:
    1. Definition vs. call.

    2. Formal vs. actual parameters.

//======================================================================
//      Project:    CS 116, applet demo
//      Author:     Tom Kelliher
//      File:       Add.java
//      Purpose:    To further demonstrate some simple properties of
//                  applets.
//======================================================================


import java.applet.*;
import java.awt.*;


//======================================================================
//      Class:      add
//      Purpose:    Display the sum of two integers.
//======================================================================

public class Add extends Applet
{
   int augend = -45;
   int addend =  34;
   Font f = new Font("Helvetica",Font.BOLD,18);


   //===================================================================
   //   Method:     init
   //   Purpose:    Set background color.
   //===================================================================

   public void init()
   {
      setBackground(Color.blue);
   }


   //===================================================================
   //   Method:     paint
   //   Purpose:    Set foreground color and font.
   //               Display the sum string.
   //   Input:      A graphics object.
   //===================================================================

   public void paint(Graphics g)
   {
      int sum;

      sum = add(augend, addend);

      g.setColor(Color.yellow);
      g.setFont(f);
      g.drawString(augend + " + " + addend + " = " + sum, 50, 50);
   }


   //===================================================================
   //   Method:     add
   //   Purpose:    Add two integers, returning their value.
   //   Input:      Two integers.
   //   Output:     Returns the sum of the integer.
   //===================================================================

   public int add(int a, int b)
   {
      int s;   // The sum.

      s = a + b;
      return s;
   }
}
Run the applet.



Thomas P. Kelliher
Fri Sep 8 09:20:19 EDT 2000
Tom Kelliher