1、プログラム
import java.applet.*; import java.awt.*; import java.awt.event.*; import java.util.*; import java.text.*; public class car extends Applet implements Runnable { protected Thread w_Thread = null; //スレッドの作成 protected Image w_offscreen; int w_iti=29; //車の位置 int w_speed=0; //車のスピード int w_add=0; //加速度 Button wb_start,wb_up,wb_down; //ボタンの宣言 public void init() { wb_start = new Button("スタート"); //スタートボタンの作成 wb_start.addActionListener(new ActionListener() { //ボタンのアクション public void actionPerformed(ActionEvent e){ w_iti = 30; } }); wb_up = new Button("加速度up"); //加速ボタンの作成 wb_up.addActionListener(new ActionListener() { //ボタンのアクション public void actionPerformed(ActionEvent e){ w_add = w_add + 1; } }); wb_down = new Button("加速度down"); //減速ボタンの作成 wb_down.addActionListener(new ActionListener() { //ボタンのアクション public void actionPerformed(ActionEvent e){ w_add = w_add - 1; } }); add (wb_start); //ボタンの配置 add (wb_up); add (wb_down); } public void offpaint(Graphics g) { //グラフィック g.setColor(Color.cyan); //背景 g.fillRect(0,0,500,200); g.setColor(Color.black); //道路 g.fillRect(0,80,500,20); g.setColor(Color.yellow); g.fillRect(00,100,150,15); g.setColor(Color.white); g.fillRect(150,100,200,15); g.setColor(Color.green); g.fillRect(350,100,150,15); g.setColor(Color.white); //操作説明 g.drawString("道 路", 220,96); g.setColor(Color.black); g.drawString("加速", 60,113); g.drawString("等速",235,113); g.drawString("減速",415,113); g.drawString("1.[加速度up]、[加速度down]ボタンで、[加速度]を設定して下さい", 20, 40); g.drawString("2.[スタート]ボタンで、走り出します", 20, 60); g.drawString("[加速]の部分で、スピードが上がります", 30, 135); g.drawString("[等速]の部分で、スピードは一定です", 30, 155); g.drawString("[減速]の部分で、スピードが下がります", 30, 175); g.drawString("[加速]・[減速]は、指定された[加速度]で決まります", 30, 195); car_paint(g,w_iti); } void car_paint(Graphics g, int w_iti) { //車のグラフィック int[] w_xp = new int[8]; //車の外観 int[] w_yp = new int[8]; w_xp[0] = w_iti-20; w_yp[0] = 77; w_xp[1] = w_iti-20; w_yp[1] = 70; w_xp[2] = w_iti-12; w_yp[2] = 70; w_xp[3] = w_iti-8; w_yp[3] = 65; w_xp[4] = w_iti+8; w_yp[4] = 65; w_xp[5] = w_iti+12; w_yp[5] = 70; w_xp[6] = w_iti+20; w_yp[6] = 70; w_xp[7] = w_iti+20; w_yp[7] = 77; g.setColor(Color.red); g.fillPolygon(w_xp,w_yp,8); g.setColor(Color.black); //タイヤ g.fillOval(w_iti-13,74,6,6); g.fillOval(w_iti+7,74,6,6); g.setColor(Color.black); g.drawString("車",w_iti-5,76); g.drawString("加速度 :"+Integer.toString(w_add), 400, 20); //加速度表示 g.drawString("スピード:"+Integer.toString(w_speed), 400, 40); } public void paint(Graphics g) { if (w_offscreen == null) { try { Dimension w_dimension = new Dimension(500,200); w_offscreen = createImage(w_dimension.width, w_dimension.height); } catch (Exception e) { } } if (w_offscreen != null) { Graphics w_offGC = w_offscreen.getGraphics(); offpaint(w_offGC); g.drawImage(w_offscreen, 0, 0, this); w_offGC.dispose(); } } public void update(Graphics g) { paint(g); } public void start() { //スレッドの開始 if (w_Thread == null) { w_Thread = new Thread(this); w_Thread.start(); } } public void stop() { //スレッドの停止 if (w_Thread != null) { w_Thread.stop(); w_Thread = null; } } public void run() { //スレッドの実行 while (true) { //whileのループ car_iti(); //車の位置計算 repaint(); //グラフィックの描写 try { //try~catch文(エラー検出) Thread.sleep(10); //0.01秒の停止 } catch(Exception e) { } } } public void car_iti() { //車の位置計算 if (w_iti < 30) { w_speed = 0; } else { if (w_iti < 150) { w_speed = w_speed + w_add; } else { if (w_iti > 350) { w_speed = w_speed - w_add; if (w_speed < 0) { w_speed = 0; } } } w_iti = w_iti + w_speed/100; } } } |
2、考察
1,スレッド(マルチスレッド)初期状態ーー→実行準備完了ーーー | | |→ブロック状態 ↓ ↓ | 終了状態ーー→ 実行状態 ーーー |
ThreadX tx = new ThreadX(); //スレッドの起動 tx.start(); |
3、感想
今回はかなりの苦労をようしました。テストもつまってるし、レポートも難しいし全然寝る時間も無くて。 でも終わった達成感が今まで以上にありました。なんとか期限内に終われてよかったです。4、参考文献・HP
・独習Java第2版 『ジョゼフ・オニール著』