Apologies for the recent interruption in service; I accidentally allowed the domain to expire.

Blox! Source

(Last Update 14:30 on Wednesday, 12 January 2000)

// Blox! applet
// Neil Rashbrook
// Last modified January 3 1999
import java.awt.*;
public final class blox102 extends java.applet.Applet implements Runnable {
private Thread timer;
private Frame cursorFrame;
private Canvas squares[];
private int map[];
private int hole;
private int score;
private long startTime, stopTime;
public void init() {
squares = new Canvas[16];
super.setLayout(new java.awt.GridLayout(4, 4));
for (int i = 0; i < 16; i++) {
Canvas square = new Canvas();
squares[i] = square;
super.add(square);
square.setBackground(Color.lightGray);
}
Component comp = super.getParent();
while (comp != null && !(comp instanceof Frame)) comp = comp.getParent();
if (comp instanceof Frame) cursorFrame = (Frame)comp;
map = new int[16];
for (int i = 0; i < 16; i++) map[i] = i;
hole = 15;
shuffle();
timer = new Thread(this);
timer.start();
super.validate();
}
public void start() {
if (stopTime > 0 && score != 15) {
long now = System.currentTimeMillis();
startTime += now - stopTime;
stopTime = 0;
status();
}
}
public void stop() {
if (stopTime == 0 && score != 15) stopTime = System.currentTimeMillis();
}
public void destroy() {
timer.interrupt();
}
public boolean mouseMove(Event evt, int x, int y) {
status();
return true;
}
public boolean mouseDrag(Event evt, int x, int y) {
status();
return true;
}
public boolean mouseEnter(Event evt, int x, int y) {
for (int i = 0; i < 16; i++) if (evt.target == squares[i]) {
if (cursorFrame != null) {
if (i == hole) cursorFrame.setCursor(Cursor.MOVE_CURSOR);
else if ((i & 3) == (hole & 3)) cursorFrame.setCursor(i < hole ? Cursor.S_RESIZE_CURSOR : Cursor.N_RESIZE_CURSOR);
else if ((i & 12) == (hole & 12)) cursorFrame.setCursor(i < hole ? Cursor.W_RESIZE_CURSOR : Cursor.E_RESIZE_CURSOR);
}
Canvas square = squares[map[i]];
square.setBackground(Color.gray);
square.repaint();
return true;
}
return false;
}
public boolean mouseExit(Event evt, int x, int y) {
if (cursorFrame != null) cursorFrame.setCursor(Cursor.DEFAULT_CURSOR);
for (int i = 0; i < 16; i++) if (evt.target == squares[i]) {
Canvas square = squares[map[i]];
square.setBackground(Color.lightGray);
square.repaint();
return true;
}
return false;
}
public boolean mouseDown(Event evt, int x, int y) {
for (int i = 0; i < 16; i++) if (evt.target == squares[i]) {
int j = 0;
if (i == hole) shuffle();
else if ((i & 3) == (hole & 3)) j = i < hole ? -4 : 4;
else if ((i & 12) == (hole & 12)) j = i < hole ? -1 : 1;
if (j != 0) {
Canvas square = squares[map[i]];
square.setBackground(Color.lightGray);
square.repaint();
while (hole != i) {
map[hole] = map[hole + j];
hole += j;
}
map[hole] = 15;
squares[15].setBackground(Color.gray);
squares[15].repaint();
if (stopTime == -1) {
startTime = System.currentTimeMillis();
stopTime = 0;
}
score();
}
return true;
}
return false;
}
private void shuffle() {
for (int i = 0; i < 15; i++) {
int j;
do j = (int)(Math.floor(Math.random() * 15));
while (j == i || j == hole);
int k = map[i];
map[i] = map[j];
map[j] = k;
}
for (int i = 0; i < 15; i++) {
int j;
do j = (int)(Math.floor(Math.random() * 15));
while (j == i || j == hole);
int k = map[i];
map[i] = map[j];
map[j] = k;
}
stopTime = -1;
score();
}
private void score() {
score = 0;
for (int i = 0; i < 15; i++) if (map[i] == i) score++;
if (score == 15 && stopTime >= 0) stopTime = System.currentTimeMillis();
status();
}
private void status() {
StringBuffer status = new StringBuffer(24).append(score).append(" blox correct");
if (stopTime >= 0) status.append("...");
if (stopTime > 0) status.append((stopTime - startTime) / 1000);
if (stopTime == 0) status.append((System.currentTimeMillis() - startTime) / 1000);
super.showStatus(status.toString());
}
public void run() {
try {
for (;;) {
if (stopTime == 0) status();
Thread.sleep(1000);
}
} finally {
return;
}
}
public static void main(String[] args) {
String[] applet = {"blox.html"};
sun.applet.AppletViewer.main(applet);
}
}

No Frames Version
Customization Page
Back to Blox! Page


640661
38.107.179.227