/** * File: ClickLife.java * Author: Brian Borowski * Date created: May 1999 * Date last modified: November 7, 2008 */ import java.applet.AudioClip; import java.awt.Color; import java.awt.Container; import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.JApplet; import javax.swing.JButton; import javax.swing.JComponent; import java.util.Random; public class ClickLife extends JApplet implements MouseListener { private int width, height, distanceSqr; private JButton lifeButton; private Random generator; private Board board; public void init() { generator = new Random(); final AudioClip audioClip = getAudioClip(getCodeBase(), "Audio/Surprise.au"); lifeButton = new JButton("Click Me!"); lifeButton.addMouseListener(this); lifeButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { audioClip.play(); } }); Board board = new Board(getColor(getParameter("border")), getColor(getParameter("background"))); board.setLayout(new FlowLayout()); board.add(lifeButton); Container contentPane = getContentPane(); contentPane.add(board); width = getSize().width; height = getSize().height; board.setSize(width, height); // In the next iteration, we want to move the button far enough away // from its current position. 1/2 the average of the width and // height of the JApplet seems to work well. int distanceAway = ((width + height) / 2) / 2; distanceSqr = distanceAway * distanceAway; setVisible(true); // Allow users to press the button with keystrokes, since they may // actually want to hear the audio clip. lifeButton.requestFocusInWindow(); } public void mouseEntered(MouseEvent me) { int x, y, prevX, prevY, buttonWidth, buttonHeight; buttonWidth = lifeButton.getSize().width; buttonHeight = lifeButton.getSize().height; prevX = lifeButton.getX(); prevY = lifeButton.getY(); if (generator.nextDouble() < 0.5) { // Generate a random x half of the time. x = generator.nextInt(width - 2 - buttonWidth) + 1; int diff = x - prevX; // Solve for y. y = ((int)Math.sqrt(distanceSqr - diff * diff) + prevY) % (height - 2 - buttonHeight); } else { // Generate a random y the other half of the time. y = generator.nextInt(height - 2 - buttonHeight) + 1; int diff = y - prevY; // Solve for x. x = ((int)Math.sqrt(distanceSqr - diff * diff) + prevX) % (width - 2 - buttonWidth); } lifeButton.setLocation(x, y); } public void mouseExited(MouseEvent me) { } public void mousePressed(MouseEvent me) { } public void mouseClicked(MouseEvent me) { } public void mouseReleased(MouseEvent me) { } private Color getColor(String background) { if ((background == null) || (background.length() != 7) || (background.charAt(0) != '#')) { return Color.white; } else { String hexcolor, red, green, blue; hexcolor = background.substring(1, background.length()); red = hexcolor.substring(0, 2); green = hexcolor.substring(2, 4); blue = hexcolor.substring(4, 6); try { return new Color(Integer.parseInt(red, 16), Integer.parseInt(green, 16), Integer.parseInt(blue, 16)); } catch (NumberFormatException nfe) { return Color.white; } } } } class Board extends JComponent { private Color borderColor, backgroundColor; public Board(Color borderColor, Color backgroundColor) { this.borderColor = borderColor; this.backgroundColor = backgroundColor; setDoubleBuffered(true); } public void paintComponent(Graphics g) { int canvasWidth = getSize().width, canvasHeight = getSize().height; g.setColor(backgroundColor); g.fillRect(0, 0, canvasWidth, canvasHeight); g.setColor(borderColor); g.drawRect(0, 0, canvasWidth - 1, canvasHeight - 1); } }