| 情報学部 | 菅沼ホーム | Java 目次 | 基礎技術目次 | 索引 |
x = x0 + vt (1) x(t+dt) = x(t) + v・dt (2)
001 import java.awt.*;
002 import java.awt.event.*;
003 import javax.swing.*;
004
005 public class Test {
006 public static void main (String[] args)
007 {
008 Win win = new Win("等速直線運動");
009 }
010 }
011
012 class Win extends JFrame
013 {
014 /******************/
015 /* コンストラクタ */
016 /******************/
017 Win(String name)
018 {
019 // JFrameクラスのコンストラクタ(Windowのタイトルを引き渡す)
020 super(name);
021 // Windowの大きさ
022 setSize(640, 470); // +40, +70
023 // ContentPane の取得と MainPanel の追加
024 Dimension d = getSize(); // Windowの大きさ
025 d.width -= 40;
026 d.height -= 70;
027 MainPanel pn = new MainPanel(d); // MainPanel オブジェクトの生成
028 getContentPane().add(pn); // MainPanel オブジェクトを ContentPane に追加
029 // ウィンドウを表示
030 setVisible(true);
031 // イベントアダプタ
032 addWindowListener(new WinEnd());
033 }
034
035 /******************************/
036 /* 上,左,下,右の余白の設定 */
037 /******************************/
038 public Insets getInsets()
039 {
040 return new Insets(50, 20, 20, 20);
041 }
042
043 /************/
044 /* 終了処理 */
045 /************/
046 class WinEnd extends WindowAdapter
047 {
048 public void windowClosing(WindowEvent e) {
049 System.exit(0);
050 }
051 }
052 }
053
054 class MainPanel extends JPanel implements Runnable
055 {
056 boolean state = true;
057 double x, y, x0 = 0.0, t = 0.0, dt = 0.1, v = 20.0;
058 Dimension d;
059 Thread th;
060
061 MainPanel(Dimension d1)
062 {
063 d = d1;
064 x = x0;
065 y = d.height / 2;
066 setBackground(new Color(238, 255, 238)); // 背景色の設定
067 th = new Thread(this); // スレッドの生成とスタート
068 th.start();
069 }
070 // 他ページへ移動の際,一時的にスレッドを停止
071 public void stop()
072 {
073 state = false;
074 }
075 // スレッドの実行
076 public void run()
077 {
078 while (state) {
079 try {
080 th.sleep(33);
081 }
082 catch (InterruptedException e) {}
083 if (x < d.width) {
084 t += dt;
085 x = v * t; // x += v * dt; でも良い
086 }
087 else {
088 x = x0;
089 t = 0;
090 }
091 repaint();
092 }
093 }
094 // 描画
095 public void paintComponent(Graphics g)
096 {
097 super.paintComponent(g); // 親クラスの描画
098
099 g.setColor(Color.green);
100 g.fillOval((int)x-20, (int)y-20, 40, 40);
101 }
102 }
v = v0 + at (3) x = x0 + v0t + 0.5at2 (4) v(t+dt) = v(t) + a・dt (5) x(t+dt) = x(t) + 0.5(v(t) + v(t+dt))・dt (6) ( dt が小さいときの近似式)
001 import java.awt.*;
002 import java.awt.event.*;
003 import javax.swing.*;
004
005 public class Test {
006 public static void main (String[] args)
007 {
008 Win win = new Win("等加速度運動");
009 }
010 }
011
012 class Win extends JFrame
013 {
014 /******************/
015 /* コンストラクタ */
016 /******************/
017 Win(String name)
018 {
019 // JFrameクラスのコンストラクタ(Windowのタイトルを引き渡す)
020 super(name);
021 // Windowの大きさ
022 setSize(640, 470); // +40, +70
023 // ContentPane の取得と MainPanel の追加
024 Dimension d = getSize(); // Windowの大きさ
025 d.width -= 40;
026 d.height -= 70;
027 MainPanel pn = new MainPanel(d); // MainPanel オブジェクトの生成
028 getContentPane().add(pn); // MainPanel オブジェクトを ContentPane に追加
029 // ウィンドウを表示
030 setVisible(true);
031 // イベントアダプタ
032 addWindowListener(new WinEnd());
033 }
034
035 /******************************/
036 /* 上,左,下,右の余白の設定 */
037 /******************************/
038 public Insets getInsets()
039 {
040 return new Insets(50, 20, 20, 20);
041 }
042
043 /************/
044 /* 終了処理 */
045 /************/
046 class WinEnd extends WindowAdapter
047 {
048 public void windowClosing(WindowEvent e) {
049 System.exit(0);
050 }
051 }
052 }
053
054 class MainPanel extends JPanel implements Runnable
055 {
056 boolean state = true;
057 double x, y, x0 = 0.0, t = 0.0, dt = 0.04, v0 = 0.0, v, a = 100.0;
058 Dimension d;
059 Thread th;
060
061 MainPanel(Dimension d1)
062 {
063 d = d1;
064 x = x0;
065 y = d.height / 2;
066 v = v0;
067 setBackground(new Color(238, 255, 238)); // 背景色の設定
068 th = new Thread(this); // スレッドの生成とスタート
069 th.start();
070 }
071 // 他ページへ移動の際,一時的にスレッドを停止
072 public void stop()
073 {
074 state = false;
075 }
076 // スレッドの実行
077 public void run()
078 {
079 while (state) {
080 try {
081 th.sleep(40);
082 }
083 catch (InterruptedException e) {}
084 if (x < d.width) {
085 t += dt;
086 x = v0 * t + 0.5 * a * t * t;
087 // double v1 = v;
088 // double v2 = v + a * dt;
089 // x += 0.5 * (v1 + v2) * dt;
090 // v = v2;
091 }
092 else {
093 x = x0;
094 v = v0;
095 t = 0;
096 };
097 repaint();
098 }
099 }
100 // 描画
101 public void paintComponent(Graphics g)
102 {
103 super.paintComponent(g); // 親クラスの描画
104
105 g.setColor(Color.green);
106 g.fillOval((int)x-20, (int)y-20, 40, 40);
107 }
108 }
vx = vx0 vy = vy0 - gt x = x0 + vx0t y = y0 + vy0t - 0.5gt2
001 import java.awt.*;
002 import java.awt.event.*;
003 import javax.swing.*;
004
005 public class Test {
006 public static void main (String[] args)
007 {
008 Win win = new Win("自由落下");
009 }
010 }
011
012 class Win extends JFrame
013 {
014 /******************/
015 /* コンストラクタ */
016 /******************/
017 Win(String name)
018 {
019 // JFrameクラスのコンストラクタ(Windowのタイトルを引き渡す)
020 super(name);
021 // Windowの大きさ
022 setSize(640, 470); // +40, +70
023 // ContentPane の取得と MainPanel の追加
024 Dimension d = getSize(); // Windowの大きさ
025 d.width -= 40;
026 d.height -= 70;
027 MainPanel pn = new MainPanel(d); // MainPanel オブジェクトの生成
028 getContentPane().add(pn); // MainPanel オブジェクトを ContentPane に追加
029 // ウィンドウを表示
030 setVisible(true);
031 // イベントアダプタ
032 addWindowListener(new WinEnd());
033 }
034
035 /******************************/
036 /* 上,左,下,右の余白の設定 */
037 /******************************/
038 public Insets getInsets()
039 {
040 return new Insets(50, 20, 20, 20);
041 }
042
043 /************/
044 /* 終了処理 */
045 /************/
046 class WinEnd extends WindowAdapter
047 {
048 public void windowClosing(WindowEvent e) {
049 System.exit(0);
050 }
051 }
052 }
053
054 class MainPanel extends JPanel implements Runnable
055 {
056 boolean state = true;
057 double x, y, x0 = 0.0, y0, t = 0.0, dt = 0.04, vx0 = 100.0, vy0 = -200.0, vy, g = 98.0;
058 Dimension d;
059 Thread th;
060
061 MainPanel(Dimension d1)
062 {
063 d = d1;
064 y0 = d.height / 2;
065 x = x0;
066 y = y0;
067 vy = vy0;
068 setBackground(new Color(238, 255, 238)); // 背景色の設定
069 th = new Thread(this); // スレッドの生成とスタート
070 th.start();
071 }
072 // 他ページへ移動の際,一時的にスレッドを停止
073 public void stop()
074 {
075 state = false;
076 }
077 // スレッドの実行
078 public void run()
079 {
080 while (state) {
081 try {
082 th.sleep(40);
083 }
084 catch (InterruptedException e) {}
085 if (x < d.width && y < d.height) {
086 t += dt;
087 x = vx0 * t; // この行と次の行の代わりに,それ以降の5行でも良い
088 y = y0 + vy0 * t + 0.5 * g * t * t;
089 // x += vx0 * dt;
090 // double v1 = vy;
091 // double v2 = vy + g * dt;
092 // y += 0.5 * (v1 + v2) * dt;
093 // vy = v2;
094 }
095 else {
096 x = x0;
097 y = y0;
098 vy = vy0;
099 t = 0;
100 };
101 repaint();
102 }
103 }
104 // 描画
105 public void paintComponent(Graphics g)
106 {
107 super.paintComponent(g); // 親クラスの描画
108
109 g.setColor(Color.green);
110 g.fillOval((int)x-20, (int)y-20, 40, 40);
111 }
112 }
001 import java.awt.*;
002 import java.awt.event.*;
003 import javax.swing.*;
004
005 public class Test {
006 public static void main (String[] args)
007 {
008 Win win = new Win("等速度運動と等加速度運動");
009 }
010 }
011
012 class Win extends JFrame
013 {
014 /******************/
015 /* コンストラクタ */
016 /******************/
017 Win(String name)
018 {
019 // JFrameクラスのコンストラクタ(Windowのタイトルを引き渡す)
020 super(name);
021 // Windowの大きさ
022 setSize(640, 470); // +40, +70
023 // ContentPane の取得と MainPanel の追加
024 Dimension d = getSize(); // Windowの大きさ
025 d.width -= 40;
026 d.height -= 70;
027 MainPanel pn = new MainPanel(d); // MainPanel オブジェクトの生成
028 getContentPane().add(pn); // MainPanel オブジェクトを ContentPane に追加
029 // ウィンドウを表示
030 setVisible(true);
031 // イベントアダプタ
032 addWindowListener(new WinEnd());
033 }
034
035 /******************************/
036 /* 上,左,下,右の余白の設定 */
037 /******************************/
038 public Insets getInsets()
039 {
040 return new Insets(50, 20, 20, 20);
041 }
042
043 /************/
044 /* 終了処理 */
045 /************/
046 class WinEnd extends WindowAdapter
047 {
048 public void windowClosing(WindowEvent e) {
049 System.exit(0);
050 }
051 }
052 }
053
054 class MainPanel extends JPanel implements Runnable
055 {
056 boolean state = true;
057 double x0 = 0.0, x1, x2, y1, y2; // x方向の初期位置と各物体の位置
058 double t = 0.0, dt = 0.04; // 時間とその刻み幅
059 double v0 = 50.0, v1, v2; // 初期速度と各物体の速度
060 double a = 30.0; // 加速度
061 double ang1 = 20.0 * Math.PI / 180.0, ang2 = -20.0 * Math.PI / 180.0; // 進行方向
062 Dimension d; // パネルの幅と高さ
063 Thread th;
064
065 MainPanel(Dimension d1)
066 {
067 d = d1;
068 x1 = x0;
069 y1 = d.height / 2;
070 v1 = v0;
071 x2 = x0;
072 y2 = d.height / 2;
073 v2 = v0;
074 setBackground(new Color(238, 255, 238)); // 背景色の設定
075 th = new Thread(this); // スレッドの生成とスタート
076 th.start();
077 }
078 // 他ページへ移動の際,一時的にスレッドを停止
079 public void stop()
080 {
081 state = false;
082 }
083 // スレッドの実行
084 public void run()
085 {
086 while (state) {
087 try {
088 th.sleep(40);
089 }
090 catch (InterruptedException e) {}
091 if (x2 < d.width) {
092 x1 += v1 * dt * Math.cos(ang1);
093 y1 += -v1 * dt * Math.sin(ang1);
094 double t1 = v2;
095 double t2 = v2 + a * dt;
096 v2 = t2;
097 x2 += 0.5 * (t1 + t2) * dt * Math.cos(ang2);
098 y2 += -0.5 * (t1 + t2) * dt * Math.sin(ang2);
099 }
100 else {
101 x1 = x0;
102 v1 = v0;
103 y1 = d.height / 2;
104 x2 = x0;
105 v2 = v0;
106 y2 = d.height / 2;
107 t = 0;
108 };
109 repaint();
110 }
111 }
112 // 描画
113 public void paintComponent(Graphics g)
114 {
115 super.paintComponent(g); // 親クラスの描画
116
117 g.setColor(Color.green);
118 g.fillOval((int)x1-20, (int)y1-20, 40, 40);
119 g.setColor(Color.red);
120 g.fillOval((int)x2-20, (int)y2-20, 40, 40);
121 }
122 }
| 情報学部 | 菅沼ホーム | Java 目次 | 基礎技術目次 | 索引 |