/**********************************************/ /* 線形合同法による擬似乱数とモンテカルロ法 */ /**********************************************/ import java.awt.*; public class algomont extends java.applet.Applet { Image screen; Graphics g_screen; Panel pP,inP,totalP,xnP,aP,bP,cP,butP; TextField pT,inT,totalT,xnT,aT,bT,cT; Label pL,inL,totalL,xnL,aL,bL,cL; Button go,clear; int i,j,xn,a,b,c,xnd,ad,bd,cd,inDot,outDot; double pie,r,x,y; public void init(){ resize(480,340); setBackground(Color.green); /*アプレットの背景を緑にする*/ setFont(new Font("TimesRoman",Font.PLAIN,12)); screen=createImage(300,300); g_screen=screen.getGraphics(); pP=new Panel(); inP=new Panel(); totalP=new Panel(); xnP=new Panel(); aP=new Panel(); bP=new Panel(); cP=new Panel(); butP=new Panel(); this.setLayout((LayoutManager)null); add(pP); add(inP); add(totalP); add(xnP); add(aP); add(bP); add(cP); add(butP); pP.setBounds(316,16,160,40); inP.setBounds(316,56,160,40); totalP.setBounds(316,96,160,40); xnP.setBounds(316,136,160,40); aP.setBounds(316,176,160,40); bP.setBounds(316,216,160,40); cP.setBounds(316,256,160,40); butP.setBounds(316,296,160,40); pP.setLayout(new FlowLayout(FlowLayout.LEFT,4,4)); inP.setLayout(new FlowLayout(FlowLayout.LEFT,4,4)); totalP.setLayout(new FlowLayout(FlowLayout.LEFT,4,4)); xnP.setLayout(new FlowLayout(FlowLayout.LEFT,4,4)); aP.setLayout(new FlowLayout(FlowLayout.LEFT,4,4)); bP.setLayout(new FlowLayout(FlowLayout.LEFT,4,4)); cP.setLayout(new FlowLayout(FlowLayout.LEFT,4,4)); butP.setLayout(new FlowLayout(FlowLayout.LEFT,16,4)); pL=new Label("pie="); inL=new Label("Blue"); totalL=new Label("Blue+Red"); xnL=new Label("xn="); aL=new Label("a="); bL=new Label("b="); cL=new Label("c="); pT=new TextField(12); inT=new TextField(8); totalT=new TextField(8); xnT=new TextField(8); aT=new TextField(8); bT=new TextField(8); cT=new TextField(8); pT.setEditable(false); /*テキスト編集不可*/ inT.setEditable(false); totalT.setEditable(false); go=new Button("G o"); clear=new Button("Clear"); pP.add(pL); pP.add(pT); inP.add(inL); inP.add(inT); totalP.add(totalL); totalP.add(totalT); xnP.add(xnL); xnP.add(xnT); aP.add(aL); aP.add(aT); bP.add(bL); bP.add(bT); cP.add(cL); cP.add(cT); butP.add(go); butP.add(clear); pL.setBackground(Color.green); inL.setBackground(Color.green); totalL.setBackground(Color.green); xnL.setBackground(Color.green); aL.setBackground(Color.green); bL.setBackground(Color.green); cL.setBackground(Color.green); pT.setBackground(Color.white); inT.setBackground(Color.white); totalT.setBackground(Color.white); xnT.setBackground(Color.white); aT.setBackground(Color.white); bT.setBackground(Color.white); cT.setBackground(Color.white); screenInit(); xnd=123; ad=2061; bd=7; cd=65536; xnT.setText(String.valueOf(xnd)); aT.setText(String.valueOf(ad)); bT.setText(String.valueOf(bd)); cT.setText(String.valueOf(cd)); } public void screenInit() { /*画面初期化*/ g_screen.setColor(Color.white); g_screen.fillRect(0,0,300,300); repaint(); inDot=0; outDot=0; inT.setText(String.valueOf(inDot)); totalT.setText(String.valueOf(inDot+outDot)); } public boolean action(Event evt,Object What) { int px,py; if (evt.target==go) { /*Go ボタン*/ try { /* 初期値の設定 */ xn=Integer.parseInt(xnT.getText()); } catch (Exception e) { xn=xnd; xnT.setText(String.valueOf(xn)); } try { /* aの設定 */ a=Integer.parseInt(aT.getText()); } catch (Exception e) { a=ad; aT.setText(String.valueOf(a)); } try { /* bの設定 */ b=Integer.parseInt(bT.getText()); } catch (Exception e) { b=bd; bT.setText(String.valueOf(b)); } try { /* cの設定 */ c=Integer.parseInt(cT.getText()); } catch (Exception e) { c=cd; cT.setText(String.valueOf(c)); } for (i=0;i<1000;i++) { x=ransuu(); y=ransuu(); px=(int)(x*300); /*乱数をグラフの座標に変換*/ py=(int)(y*300); if ((x-0.5)*(x-0.5)+(y-0.5)*(y-0.5)<=0.25) { g_screen.setColor(Color.blue); /*円の中*/ inDot++; } else { /*円の外*/ g_screen.setColor(Color.red); outDot++; } g_screen.fillRect(px,py,1,1); } repaint(); pie=(double)(inDot*4)/(double)(inDot+outDot); /*πを計算*/ pT.setText(String.valueOf(pie)); /* 計算したπの値を表示 */ inT.setText(String.valueOf(inDot)); /* 円の中の点の個数 */ totalT.setText(String.valueOf(inDot+outDot)); /* 点の合計 */ xnT.setText(String.valueOf(xn)); /* 現在の乱数を表示 */ return true; } if (evt.target==clear) { screenInit(); return true; } return false; } public double ransuu() { /* 乱数生成 */ xn=((xn*a)+b) % c; r=(double)xn/(double)c; return r; } public void paint(Graphics g){ g.drawImage(screen,8,8,this); } public void update(Graphics g){ paint(g); } }