您的位置:寻梦网首页编程乐园Java天地Core JavaJava Lecture Notes
Introduction
Content
Apply
Reflect
Extend
previous.gif
 (3087 bytes)
next.gif
 (2959 bytes)

 
Extend Index
Extend Page # 5

Listing of CapacityApplet.java ?version 3

// <APPLET code="CapacityApplet .class" width=275 height=200></APPLET>
// CapacityApplet - version 3
import java.applet.Applet;
import java.awt.*;
public class CapacityApplet extends Applet
  {
  // instance variables
  int capacity[] = new int[4];
  // has to be an instance variable, since referred to in "init()"
  //and "paint()"

  // so cannot be local to either method
  int total = 0;
  // methods
  public void init()
    {
    capacity[0] = 55;
    capacity[1] = 34;
    capacity[2] = 80;
    capacity[3] = 101;
    // calculate total
    int index; for( index = 0; index < capacity.length; index++)
    total = total + capacity[ index ];
    }
    public void paint( Graphics g )
    {
    // local variables for position of text
    int x = 10;
    int y = 10;
    // display class name in window
    g.drawString("Capacity Applet - version 3", x, y );
    y = y + 25;
    // loop to display each element of 'capacity'
    int index;
    for( index = 0; index < capacity.length; index++)
      {
      g.drawString("capacity[" + index + "] = " + capacity[index], x,
      y );

      y = y + 20;
      }
    // display total
    g.drawString("Total of values is: " + total, x, y);
    }
  } // class 
Back to top
basicline.gif (169 bytes)

RITSEC - Global Campus
Copyright © 1999 RITSEC- Middlesex University. All rights reserved.
webmaster@globalcampus.com.eg