FileDocCategorySizeDatePackage
Click3.javaAPI DocExample3898Wed Apr 19 11:22:18 BST 2000None

Click3

public class Click3 extends Applet

Fields Summary
Color
puckColor
Box
puck
ColumnOfBoxes[]
targets
ColumnOfBoxes
currentTarget
Constructors Summary
public Click3()

    MouseMotionListener movePuck = new MouseMotionAdapter() {
      public void mouseMoved(MouseEvent e)
      {
	int x = e.getX() - (puck.getSize().width / 2);
	int y = getSize().height - puck.getSize().height;
	puck.setLocation(x, y);
      }
    };

    MouseListener shootTarget = new MouseAdapter() {
      public void mouseClicked(MouseEvent e)
      {
	if (currentTarget != null) {
	  int nBoxes = currentTarget.getComponentCount();
	  if (nBoxes == e.getClickCount()) {
	    currentTarget.removeAll();
	    currentTarget.getToolkit().beep();
	    currentTarget.repaint();
	  }
	}
      }
    };

    for(int i = 0; i < targets.length; i++) {
      int nBoxes = 1 + (int)(Math.random() * 4.0);
      float boxHue = (float)i / (float)targets.length;
      Color boxColor = Color.getHSBColor(boxHue, 0.5f, 0.85f);
      TargetListener tl = new TargetListener(boxColor.brighter());
      targets[i] = new ColumnOfBoxes(boxColor, nBoxes);
      targets[i].addMouseListener(tl);
      targets[i].addMouseMotionListener(tl);
      add(targets[i]);
    }

    add(puck);
    addMouseMotionListener(movePuck);
    addMouseListener(shootTarget);
  
Methods Summary
public java.lang.StringgetAppletInfo()

 
    return "Click3 by Hans Mueller. The final version of the Click applet adds some additional logic to make a pathetic little game out of the parts we've assembled.  Note that we've made a significant change to the TargetListener class: it's no longer static.  As an ordinary nested class it has access to the currentTarget field which it writes each time the mouse enters or exits a target.  The MouseListener called shootTarget reads this field when the user clicks the mouse.  This applet requires JDK 1.1.";
  
public java.lang.String[][]getParameterInfo()

    return null;
  
public static voidmain(java.lang.String[] args)

    WindowListener l = new WindowAdapter()
      {
	public void windowClosing(WindowEvent e) {System.exit(0);}
      };

    Frame f = new Frame("Click");
    f.addWindowListener(l); 
    f.add(new Click3());
    f.setSize(600, 400);
    f.show();