import java.awt.*;
import java.applet.*;
import java.lang.*;
public class Dice extends Applet
{
Graphics sG;
Graphics bG;
Image bI;
Color back = new Color(192, 192, 255);
Color black = new Color(0, 0, 0);
Color ivory = new Color(240, 240, 240);
Color marked = new Color(255, 192, 192);
Color diecolor;
int value = 0;
public void init()
{
bI = createImage(this.size().width, this.size().height);
bG = bI.getGraphics();
drawdice();
}
public void paint(Graphics g)
{
g.drawImage(bI, 0, 0, this);
sG = getGraphics();
}
public void update(Graphics g)
{
g.drawImage(bI, 0, 0, this);
}
public void drawdice()
{
bG.setColor(back);
bG.fillRect(0, 0, 501, 267);
for (int row = 0; row < 6; row = row + 1)
{
for (int col = 0; col < 6; col = col + 1)
{
diecolor = ivory;
if (2 + row + col == value)
{
diecolor = marked;
}
onedie(row, col, 0, col + 1);
onedie(row, col, 1, row + 1);
}
}
}
public void onedie(int row, int col, int off, int pips)
{
int x = 3 + 84 * col + 38 * off;
int y = 3 + 44 * row;
bG.setColor(diecolor);
bG.fillRect(x, y, 37, 37);
bG.setColor(black);
bG.drawRect(x, y, 36, 36);
if (pips == 1)
{
bG.fillOval(x + 16, y + 16, 5, 5);
}
if (pips == 2)
{
bG.fillOval(x + 7, y + 7, 5, 5);
bG.fillOval(x + 26, y + 26, 5, 5);
}
if (pips == 3)
{
bG.fillOval(x + 16, y + 16, 5, 5);
bG.fillOval(x + 4, y + 4, 5, 5);
bG.fillOval(x + 29, y + 29, 5, 5);
}
if (pips == 4)
{
bG.fillOval(x + 7, y + 7, 5, 5);
bG.fillOval(x + 26, y + 7, 5, 5);
bG.fillOval(x + 7, y + 26, 5, 5);
bG.fillOval(x + 26, y + 26, 5, 5);
}
if (pips == 5)
{
bG.fillOval(x + 16, y + 16, 5, 5);
bG.fillOval(x + 4, y + 4, 5, 5);
bG.fillOval(x + 29, y + 29, 5, 5);
bG.fillOval(x + 4, y + 29, 5, 5);
bG.fillOval(x + 29, y + 4, 5, 5);
}
if (pips == 6)
{
bG.fillOval(x + 4, y + 7, 5, 5);
bG.fillOval(x + 16, y + 7, 5, 5);
bG.fillOval(x + 28, y + 7, 5, 5);
bG.fillOval(x + 4, y + 26, 5, 5);
bG.fillOval(x + 16, y + 26, 5, 5);
bG.fillOval(x + 28, y + 26, 5, 5);
}
}
public boolean mouseDown(Event evt, int mx, int my)
{
value = (int) (java.lang.Math.floor((mx - 3)/84) +
java.lang.Math.floor((my - 3)/44) + 2);
drawdice();
sG.drawImage(bI, 0, 0, this);
return true;
}
}
Copyright c 1997 by
Frank Wattenberg, Department of Mathematics, Montana State University,
Bozeman, MT 59717