Report#6
課題
Java Applet/AWTについて学び、Java Appletのオリジナルプログラムを作成
し解説せよ。
目次
1,プログラムのソース
2,実行結果
3,考察
4,反省・感想
1,プログラムのソース
001: import java.applet.Applet; //インポートする。
002: import java.awt.*;
003: import java.util.*;
004:
005: public class bar extends java.applet.Applet implements Runnable{
006: MediaTracker mt0, mt1; //MediaTrackerでmt0とmt1を宣言
007: Image ball, bg; //Imageでballとbgを宣言
008:
009: int time = 10; //表示する時間
010: int life = 3; //残りのライフ
011: int ballx, bally; //ボールの位置
012: int barx = 150, bary = 320; //バーの位置
013: int ballw = 15, ballh = 15; //ボールの画像の幅&高さ
014: int barw = 40, barh = 4; //バーの幅&高さ
015: int m = 10; //端の余裕分
016: int s; //スコア
017:
018: boolean loop = true; //boolean型で繰り返しのための変数を宣言
019: Thread th = null; //スレッド変数
020:
021: Dimension d; //表示領域
022: Image offs; //オフスクリーン
023: Graphics grf;
024:
025: public void init() {
026: mt0 = new MediaTracker(this); mt1 = new MediaTracker(this);
//読み込んだのをmt0に入れる
027: ball = getImage(getCodeBase(), "ball.gif"); bg =
getImage(getCodeBase(), "jellybeans04.jpg"); //ファイルの読み込み
028: mt0.addImage(ball,0);
029: d = size(); //表示領域を入手する
030: offs = createImage( d.width, d.height);
031: grf = offs.getGraphics();
032: ballx = m + (int)(Math.random() * (float)(d.width -
(m*2+ballw+1)));
033: bally = 50; //初期位置の設定、上とこの行
034: }
035:
036: public void update(Graphics g) {
037: if (mt0.checkID(0) == false) { //もしfalseなら
038: g.setColor(Color.black); //文字色は黒で
039: g.fillRect(0, 0, d.width, d.height);
040: g.drawString("Loading...", 110, 175); //x=110、y=175に
Loadingと表示
041: return; //値を返す
042: }
043:
044: grf.setColor(Color.gray); //色を灰色に指定
045: grf.fillRect(0,0,d.width, d.height); //塗る
046:
047: grf.drawImage(bg, m, m, d.width-m*2, d.height-m*2, this);
//背景設定
048:
049: grf.setColor(Color.white); //色を白に指定
050: grf.fillRect(barx, bary, barw, barh); //バーの配置
051:
052: grf.drawImage(ball, ballx, bally, this); //ボールを描く
053:
054: grf.setColor(Color.black); //色を黒に指定
055: grf.drawString("Score : "+s, 10, 10); //表示
056: if ( life < 0 ) { //もしlifeが0より下なら
057: g.setColor(Color.black); //黒で
058: grf.drawString("Game Over !", 110, 175); //表示
059: }
060: if(s==700 ) { //もしsが700なら
061: grf.setColor(Color.black); //黒で
062: grf.drawString("Perfect ! HAHAHA~!",80,175); //表示
063: }
064:
065: g.drawImage(offs, 0, 0, this); //イメージを描く
066: }
067:
068: public void paint(Graphics g) {
069: update(g);
070: }
071:
072: public boolean mouseMove(Event e, int x, int y) {
073:
074: barx = x; //バーの位置を決める
075:
076: if ( barx < m ) { //もしbarxがmより少ないなら
077: barx = m; //mをbarxにいれる
078: }
079: if ( barx + barw > d.width - m ) { //barx+barwがd.width-m
より小さいなら
080: barx = d.width - m -barw; //d.width - m -barwをbarxに
入れる
081: }
082:
083: repaint();
084: return true;
085: }
086:
087: public void start() {
088: if(th == null) { //もしthがnullなら
089: th = new Thread(this);
090: th.start(); //開始
091: }
092: }
093:
094: public void run() {
095: int dx = 2, dy = 2;
096: int oldx=0;
097:
098: try{
099: mt0.waitForID(0);
100: } catch (InterruptedException e) {
101: return;
102: }
103:
104: while(loop) { //繰り返し
105: if (bally + ballh >= bary & bally + ballh <= bary+barh & ballx + ballw >= barx & ballx <= barx+barw ) {
106: dy = -2;
107: if ( ballx < barx || ballx + ballw > barx + barw ){
108: oldx = dx;
109: if ( oldx == 0 ) {
110: if ( ballx < barx ) {
111: dx = -2;
112: }
113: if ( ballx + ballw > barx + barw ) {
114: dx = +2;
115: }
116: } else {
117: dx = 0;
118: }
119: }
120: s = s + 10; //得点の加算
121: if(s!=0 & (s%50)==0 & time > 0 ) { //ボー
ルのスピードを上げる
122: time = time-1;
123: }
124: }
125: if (ballx < 0 + m ) {
126: dx = 2;
127: }
128: if (ballx + ballw > d.width - m) {
129: dx = -2;
130: }
131: if (bally < 0 + m) {
132: dy = 2;
133: }
134: if ( bally + ballh > d.height - m ) {
135: ballx = m + (int)(Math.random() * (float)(d.width -
(m*2+ballw+1)));
136: bally = 50;
137: life = life - 1;
138: }
139: if ( life < 0 || s==700) {
140: loop = false;
141: }
142:
143: ballx = ballx + dx;
144: bally = bally + dy;
145: repaint();
146:
147: try{ //例外処理
148: Thread.sleep(time);
149: } catch (InterruptedException e){}
150: }
151: }
152:
153: public void stop() {
154: if ( th != null ) { //もしthがnullでないのなら
155: th.stop(); //ストップ
156: th = null;
157: }
158:
159: }
160: }
2,実行結果
こちら
3,考察
- 1〜3行 :インポートする。
- 6〜23行 :ゲームを作るのに必要な変数などの宣言。
- 27〜28行 :イメージファイルの読み込みをする。
- 29〜31行 :オフスクリーンを設定する。
- 32〜33行 :ボールの初期の位置の設定。ランダムで決まるようになっ
ている。
- 36〜42行 :読み込んでいる時のメッセージの表示。
- 44〜47行 :フィールドの外を灰色、中を緑で塗る。一度灰色で塗って、上
から緑で塗る感じ。
- 49〜50行 :バーを描く。白色で描いて、配置を行う。
- 52行:ボールを描く。
- 54〜63行 :点数の表示の仕方。色を黒に設定、表示の文字、if文でライフ
が0になった時の表示の仕方とスコアが700になった時にパーフェクトと表
示するようにしている。
- 65行 :オフスクリーンのイメージを一挙に実際の表示領域に描く。
- 76〜81行 :バーがフィールドから出ないようにする処理。
- 87〜92行 :thがnullのときスレッドを実行させる。
- 95行:ボールの進む量を表す変数。
- 98〜102行 :全部のイメージの読み込みを待つ。
- 104行 :ずっと繰り返し。
- 105行ボールがバーに当たった時の処理。
- 106〜107行 :バーに当たると上へ跳ね返す。
- 108〜109行 :バーの端に当たった時の処理。
- 110〜115行:垂直に来たボールが左端に当たったら、左斜めに跳ね返す。右
端に当たったら、右斜めに返す。
- 117行 :斜めから来たボールは垂直に跳ね返す。
- 120〜123行:得点を10加える。もし、スコアが0以外かつ50で割った余り
が0で、かつtimeが0より大きい時、timeにtime-1した値を入れる。
- 125〜133行:左、右、上の壁にぶつかった時の処理。ぶつかったら反転して
跳ね返す。
- 134行 :バーの下に行った時の処理。
- 135〜136行 :一番下に行ったら初期値にボールを戻す。
- 137行 :ライフを1減らす。
- 139〜145行:ゲーム終了の判定をする。ライフが0より下になるかスコアが
700になったら終了。
- 153〜159行:スレッドを止める。thがnullでないときが条件。
4,反省・感想
最後の最後までかなり時間がかかってしまった。
2年からはまじめに頑張りたい。