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);