// // Slope1(斜面でのボールの落下実験) 2001/09/10 by ONOZATO Takehisa // // // import java.awt.*; import java.awt.event.*; import java.applet.*; public class Slope1 extends Applet implements MouseListener,MouseMotionListener,ActionListener{ Image ofs; Graphics grf; //オフスクリーン int ofsWidth=500; int ofsHeight=280; Color ofsBG=new Color(255,239,223); Button btn[]=new Button[2]; //ボタンのインスタンス変数 int originX=20; int originY=260; //原点 int unitX=20; int unitY=20; //単位長当たりのピクセル数 double angl=15; //∠Aの大きさ(度数,初期値) double anglr; //∠Aの大きさ(ラジアン) double L=22; //斜辺ABの長さ(m,定数) int px[]=new int[3]; int py[]=new int[3]; //3頂点のピクセル座標 boolean flgB; //点Bが移動中 boolean flgCircle; //ガイドの円が表示中 int gx,gy; //ポインターのピクセル座標 int xmin=390; int xmax=480; //ポインターの読取り範囲 int ymin=20; int ymax=240; boolean flg; //ボールが移動中 double t; //経過時間(sec) double dt=0.02; //時間の増分(sec,定数) double s; //斜辺上の落下距離(m) double x,y; //斜辺上の動点の座標(m) double gg=9.8; //重力加速度(定数) public void init(){ ofs=createImage(ofsWidth,ofsHeight); grf=ofs.getGraphics(); setBackground(ofsBG); addMouseListener(this); addMouseMotionListener(this); btn[0]=new Button("Start"); btn[0].addActionListener(this); btn[0].setActionCommand("0"); add(btn[0]); btn[1]=new Button("Clear"); btn[1].addActionListener(this); btn[1].setActionCommand("1"); add(btn[1]); anglr=angl*Math.PI/180; px[0]=picX(0); py[0]=picY(0); px[1]=picX(L*Math.cos(anglr)); py[1]=picY(L*Math.sin(anglr)); px[2]=px[1]; py[2]=picY(0); Font fnt=grf.getFont(); //フォント String fname=fnt.getName(); int fstyle=fnt.getStyle(); grf.setFont(new Font(fname,fstyle,14)); //14pt t=0; flg=false; flgB=false; flgCircle=false; //初期設定 } public void update(Graphics g) { paint(g); } public void paint(Graphics g){ if(flg){ if(x>0){ t=t+dt; DrawBack(grf); DrawBall(grf); g.drawImage(ofs, 0, 0, this); shortSleep(); repaint(); }else{ t=0; flg=false; btn[0].setLabel("Start"); //初期設定 DrawBack(grf); DrawBall(grf); g.drawImage(ofs, 0, 0, this); repaint(); } }else{ DrawBack(grf); DrawBall(grf); g.drawImage(ofs, 0, 0, this); } } /*** しばらく休む ***/ public void shortSleep(){ try{ Thread.sleep(10); //ミリ秒 } catch(Exception e){} } /*** ボタンのイベントプロシージャ ***/ public void actionPerformed(ActionEvent e){ if(e.getActionCommand()=="0"){ flg=!(flg); if(flg){ btn[0].setLabel("Stop "); }else{ btn[0].setLabel("Start"); } }else{ t=0; flg=false; //初期設定 } repaint(); } /*** マウスのイベントプロシージャ ***/ public void mousePressed(MouseEvent e){ gx=e.getX(); gy=e.getY(); if(!(flg) && t==0 && Math.abs(gx-px[1])<5 && Math.abs(gy-py[1])<5){ flgB=true; } } public void mouseDragged(MouseEvent e) { gx=e.getX(); gy=e.getY(); if(!(flg) && t==0 && flgB==true && gx>xmin && gxymin && gy