//****************************************************************************** // roll.java: Applet // //****************************************************************************** import java.applet.*; import java.awt.*; import java.util.*; //============================================================================== // Main Class for applet roll // //============================================================================== public class roll extends Applet implements Runnable { final int GITEMS=22; Thread runner=null; Vector ds; Dice[] dice =new Dice[5]; Button scores[]=new Button[GITEMS]; Label scoresl[]=new Label[GITEMS]; boolean started=false; Button bt_roll, bt_start, bt_sort; BorderLayout layout1; Panel p1,p2,p3; int scoresint[]=new int[GITEMS]; public void start() { if (runner == null) { runner = new Thread(this); runner.start(); } } public void stop() { if (runner != null){ runner.stop(); runner=null; } } public void run() { while (true) { //repaint(); try { Thread.sleep(50); } catch(InterruptedException e){ } } } public void init(){ initGame(); setBackground(Color.lightGray); layout1=new BorderLayout(); setLayout(layout1); p1=new Panel(); p1.setLayout(new FlowLayout(FlowLayout.LEFT)); for (int i=0;i < 5;i++) { dice[i] = new Dice(this); dice[i].face=1; p1.add(dice[i]); } resize (50,50); p2=new Panel(); bt_roll=new Button("Roll"); p2.add(bt_roll); bt_sort=new Button("Sort Dice"); p2.add(bt_sort); bt_start=new Button("Start Game"); p2.add(bt_start); //scores Panel p3=new Panel(); p3.setLayout(new GridLayout(12,4,0,0)); showScores(1, p3); add("North",p1); add("Center",p3); add("South",p2); //show(); } public void initGame() { //initialize new game for (int i=0;i dice[x+1].value){ holder1=dice[x].value; holder2=dice[x+1].value; dice[x].value=holder2; dice[x+1].value=holder1; }//end if }// end inside }//end for }//end method }// end applet Roll class Dice extends Canvas{ Random rand; int r; int value; int face; //1 = back 2 = front boolean selected; Applet a; public Dimension preferredSize() { return new Dimension(60,60); } public void flipFace() { if (this.face==1) {this.face=2;} else if (this.face==2) {this.face=1;} repaint(); } public Dice(Applet a){ rand = new Random(hashCode()); r=10; //setBackground(Color.white); //setForeground(Color.black); this.a = a; } public synchronized void throwDice(){ value=(Math.abs(rand.nextInt())%6)+1; repaint(); } void cheat(int n){ value=n%6; repaint(); } public String toString(){ return new String("num"+value); } public int getNumber(){ return value+1; } public void paint(Graphics g) { Image i_dice; //Dimension d = size(); if (face==2){ switch (value) { case 1: i_dice=a.getImage(a.getDocumentBase(),"dice1.gif"); g.drawImage(i_dice,0,0,this); break; case 2: i_dice=a.getImage(a.getDocumentBase(),"dice2.gif"); g.drawImage(i_dice,0,0,this); break; case 3: i_dice=a.getImage(a.getDocumentBase(),"dice3.gif"); g.drawImage(i_dice,0,0,this); break; case 4: i_dice=a.getImage(a.getDocumentBase(),"dice4.gif"); g.drawImage(i_dice,0,0,this); break; case 5: i_dice=a.getImage(a.getDocumentBase(),"dice5.gif"); g.drawImage(i_dice,0,0,this); break; case 6: i_dice=a.getImage(a.getDocumentBase(),"dice6.gif"); g.drawImage(i_dice,0,0,this); break; } } else { i_dice=a.getImage(a.getDocumentBase(),"dice_back.gif"); g.drawImage(i_dice,0,0,this); } } public boolean mouseDown(Event evt, int x, int y) { deliverEvent(new Event (this,Event.ACTION_EVENT,"")); return true; } }// end class Dice