Simon Source

(Last Update 15:32 on Wednesday, 12 January 2000)

// Simon applet
// Neil Rashbrook
// Last modified December 28 1999
import java.applet.AudioClip;
import java.awt.Component;
import java.awt.Color;
import java.awt.Event;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
public class simon extends java.applet.Applet implements Runnable {
private quadrant q[];
private AudioClip clips[] = new AudioClip[5];
private static java.util.Random random = new java.util.Random();
private int pos, m[], n[], current, x, y, s;
private java.awt.Rectangle b;
private boolean disabled;
public void init() {
for (int i = 0; i < 5; i++) {
AudioClip c = super.getAudioClip(super.getCodeBase(), "" + i + ".au");
c.play();
c.stop();
clips[i] = c;
}
try {
((java.awt.Container)Class.forName("process").newInstance()).add(this);
} catch (Exception e) {
e.printStackTrace();
}
}
public boolean isFocusTraversable() {
return true;
}
public boolean keyDown(Event evt, int key) {
if (disabled) return false;
n = m = null;
super.showStatus("Practice Mode");
if (key < '1' || key > '9') return false;
int r, l = 0;
n = new int[pos = key -= 48];
for (; key > 0; n[--key] = l = r) while ((r = (random.nextInt() & 3) + 1) == l);
(new Thread(this)).start();
return true;
}
public boolean mouseDown(Event evt, int x, int y) {
if (current != 0) mouseUp(null, 0, 0);
if (!disabled) for (current = 4; current > 0; current--) if (q[current].contains(x, y)) {
Graphics g = super.getGraphics();
if (m == null || current == m[--pos]) {
clips[current].loop();
q[current].fill(g, true);
sleep(100);
} else {
clips[0].loop();
q[m[pos]].fill(g, true);
super.showStatus("Failed Level " + m.length);
sleep(1000);
clips[0].stop();
q[m[pos]].fill(g, false);
current = 0;
m = null;
}
g.dispose();
return true;
}
return false;
}
public boolean mouseEnter(Event evt, int x, int y) {
super.showStatus(m != null ? "Level " + m.length : n != null ? "Failed Level " + n.length : "Practice Mode");
return true;
}
public boolean mouseMove(Event evt, int x, int y) {
return true;
}
public boolean mouseUp(Event evt, int x, int y) {
if (current != 0) {
Graphics g = super.getGraphics();
clips[current].stop();
q[current].fill(g, false);
g.dispose();
current = 0;
if (m != null && pos == 0) {
n = new int[pos = m.length + 1];
System.arraycopy(m, 0, n, 1, m.length);
m = null;
while ((n[0] = (random.nextInt() & 3) + 1) == n[1]);
(new Thread(this)).start();
}
return true;
}
return false;
}
public void paint(Graphics g) {
if (!super.bounds().equals(b)) {
b = super.bounds();
x = b.x + (b.width >> 1);
y = b.y + (b.height >> 1);
s = (b.width < b.height ? b.width : b.height) >> 1;
q = new quadrant[] {
null,
new quadrant(Color.blue, x - s / 4, y - s / 4, s * 2 / 3, s / 5, 90),
new quadrant(Color.red, x + s / 4, y - s / 4, s * 2 / 3, s / 5, 0),
new quadrant(Color.green, x + s / 4, y + s / 4, s * 2 / 3, s / 5, -90),
new quadrant(Color.yellow, x - s / 4, y + s / 4, s * 2 / 3, s / 5, -180)
};
}
g.fillRoundRect(x - s, y - s, s + s, s + s, s * 7 / 5, s * 7 / 5);
for (int i = 1; i < 5; i++) q[i].fill(g, i == current);
}
public void run() {
disabled = true;
super.showStatus("Level " + n.length);
Graphics g = super.getGraphics();
try {
Thread.sleep(500);
for (int i = n.length; i > 0; ) {
int r = n[--i];
clips[r].loop();
q[r].fill(g, true);
Thread.sleep(400);
clips[r].stop();
q[r].fill(g, false);
Thread.sleep(100);
}
m = n;
} finally {
g.dispose();
disabled = false;
return;
}
}
private static boolean sleep(long millis) {
try {
Thread.sleep(millis);
return false;
} catch (InterruptedException e) {
return true;
}
}
public void start() {
super.requestFocus();
super.showStatus("Practice Mode");
}
}
class quadrant {
Color dim, bright;
int x, y, r, s, a;
quadrant(Color col, int x, int y, int r, int s, int a) {
this.dim = new Color(col.getRGB() & 0xFF808080);
this.bright = col;
this.x = x;
this.y = y;
this.r = r;
this.s = s;
this.a = a;
}
public boolean contains(int x, int y) {
switch (a) {
case 90: case -180: x = this.x - x; break;
case 0: case -90: x -= this.x; break;
default: return false;
}
if (a < 0) y -= this.y;
else y = this.y - y;
if (x < y) {
int t = x;
x = y;
y = t;
}
if (x < 0) return x * x + y * y <= s * s;
if (y >= 0) return x * x + y * y <= r * r;
x -= r - s;
if (x >= 0) return x * x + y * y <= s * s;
return y + s >= 0;
}
public void fill(Graphics g, boolean b) {
int r2 = r + r, rs = r + s, s2 = s + s;
g.setColor(b ? bright : dim);
g.fillArc(x - r, y - r, r2, r2, a, 90);
g.fillRoundRect(x - s, y - (a < 0 ? s : r), s2, rs, s2, s2);
int x = this.x;
switch (a) {
case 90: case -180: x -= r; break;
case 0: case -90: x -= s; break;
}
g.fillRoundRect(x, y - s, rs, s2, s2, s2);
}
}
class process extends java.awt.Container implements java.awt.event.ActionListener, java.awt.event.KeyListener, java.awt.event.MouseListener {
Component c;
java.awt.PopupMenu p;
public Component add(Component c) {
c.addKeyListener(this);
c.addMouseListener(this);
p = new java.awt.PopupMenu("Start");
for (int i = 1; i <= 9; i++) p.add(new java.awt.MenuItem("Level " + i)).addActionListener(this);
c.add(p);
return this.c = c;
}
public void actionPerformed(java.awt.event.ActionEvent e) {
c.keyDown(null, e.getActionCommand().charAt(6));
}
public void keyPressed(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
c.keyDown(null, e.getKeyChar());
}
public void mouseClicked(MouseEvent e) {
if (e.isPopupTrigger()) p.show(c, e.getX(), e.getY());
}
public void mouseEntered(MouseEvent e) {
e.getComponent().mouseEnter(null, 0, 0);
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
if (!c.mouseDown(null, e.getX(), e.getY())) mouseClicked(e);
}
public void mouseReleased(MouseEvent e) {
if (!c.mouseUp(null, 0, 0)) mouseClicked(e);
}
}

Frames Version
Customization Page
Back to Simon Page


623593
38.107.191.102