// // Dice100(100個のサイコロをふる試行) 2001/09/08 by ONOZATO Takehisa // // // import java.awt.*; import java.awt.event.*; import java.applet.*; public class Dice100 extends Applet implements ActionListener{ Image ofs; Graphics grf; //オフスクリーン int ofsWidth=450; int ofsHeight=350; Color ofsBG=new Color(255,239,223); //背景色 Image imgDice; //サイコロのサンプル画像 int imgLeft=370; int imgTop=28; //表示位置 String imgfile="Dice.gif"; //画像ファイル名 int imgX[]=new int[6]; int imgY[]=new int[6]; //各サンプル(0〜5)の表示位置 int num[]=new int[6]; //各目のカウント(0〜5) Button btn1,btn2; //ボタン boolean flg; //false:クリア true:試行結果表示 Font fnt; String fname; int fstyle; //フォント public void init(){ ofs=createImage(ofsWidth,ofsHeight); grf=ofs.getGraphics(); imgDice=getImage(getCodeBase(),imgfile); for(int i=0; i<6; i++){ imgX[i]=imgLeft; imgY[i]=imgTop+30*i; } btn1=new Button("Trial"); btn1.addActionListener(this); add(btn1); btn2=new Button("Clear"); btn2.addActionListener(this); add(btn2); flg=false; fnt=grf.getFont(); //フォント fname=fnt.getName(); fstyle=fnt.getStyle(); grf.setFont(new Font(fname,fstyle,16)); //16pt } public void update(Graphics g) { paint(g); } public void paint(Graphics g){ grf.setColor(ofsBG); grf.fillRect(0,0,ofsWidth,ofsHeight); //背景 grf.setColor(new Color(0,128,0)); grf.fillRect(14,14,322,322); grf.fillRect(350,14,86,202); grf.drawImage(imgDice,imgLeft,imgTop,this); //サイコロのサンプル btn1.setBounds(370,230,50,25); //ボタンの配置 btn2.setBounds(370,260,50,25); if(flg){drawDices(grf);} g.drawImage(ofs,0,0,this); } /*** ボタンのイベント ***/ public void actionPerformed(ActionEvent e){ if(e.getActionCommand()=="Clear"){ flg=false; }else{ flg=true; } repaint(); } /*** 試行結果の表示 ***/ void drawDices(Graphics g){ for(int i=0; i<6; i++){num[i]=0;} for(int i=0; i<100; i++){ int j=(int)(6*Math.random()); num[j]=num[j]+1; int ix=28-imgX[j]+30*(i%10); int iy=28-imgY[j]+30*(i/10); g.copyArea(imgX[j],imgY[j],24,24,ix,iy); } g.setColor(Color.white); for(int i=0; i<6; i++){ g.drawString(""+num[i],imgX[i]+36,imgY[i]+18); } } }