解答は
- ホスト名: earth.mlab.im.dendai.ac.jp
- ディレクトリ: /home/submit/JavaBasic/winter/[学籍番号]
に提出しなさい。クラスファイル (〜.class) は提出しなくてよい。 提出には gFTP 等の ftp ソフトを用いること。
最低限の単位取得のためには、問題1の提出が必須。 問題2の提出は任意 (中上級者向け課題) 。
例題のボールのアニメーションを拡張し、 2つのボールが移動するアニメーションを考える。 ボールとボールの間に線を描くようにすると ラインアートのようなアニメーションが可能である。
ソースは以下のとおり。 (LineArt.java)
import java.awt.*;
import javax.swing.*;
class LineArt {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle("Line Art");
frame.setSize(640, 480);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyPanel panel = new MyPanel();
frame.getContentPane().add(panel);
frame.setVisible(true);
}
}
class MyPanel extends JPanel implements Runnable {
private Ball ball1;
private Ball ball2;
public MyPanel() {
setBackground(Color.white);
ball1 = new Ball(100,100,10,5,0,0,630,450);
ball2 = new Ball(200,100,5,10,0,0,630,450);
Thread refresh = new Thread(this);
refresh.start();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
ball1.forward();
ball2.forward();
g.setColor(Color.red);
g.drawLine(ball1.getX(), ball1.getY(), ball2.getX(), ball2.getY());
}
public void run() {
while(true) {
repaint();
try {
Thread.sleep(20);
}
catch(Exception e) {
}
}
}
}
class Ball {
private int x;
private int y;
private int vx;
private int vy;
private int left;
private int right;
private int top;
private int bottom;
public Ball(int x, int y, int vx, int vy, int l, int t, int r, int b) {
this.x = x;
this.y = y;
this.vx = vx;
this.vy = vy;
right = r;
left = l;
top = t;
bottom = b;
}
public void forward() {
x = x + vx;
y = y + vy;
if (x < left || x > right) {
vx = -vx;
}
if (y < top || y > bottom) {
vy = -vy;
}
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
このプログラムを元に独自の工夫をし、 アニメーションを行うプログラムを作成しなさい。 例えば、次のようなアニメーションが考えられる。
サンプルの実行方法:
$ java -jar ファイル名.jar
mainメソッドを書くクラスは LineArt とし、 ファイル名は LineArt.java とする。
例題のプログラムを元に、 グラフィックス表示を行う独自のゲーム性のあるプログラムを作成しなさい。 各自のアイデアを元にすることを推奨するが、 思いつかない場合、例えば次のようなゲームが考えられる。
腕に自信のある人は、 以下のような極めて高度なゲームの制作にチャレンジするのも良い。
ソースコードのファイル名は Game.java とする。 画像データなどがあればそれらを含めて、 ゲームを構成するすべてのファイルを提出しなさい。 なお、ソースコードを複数のファイルに分割して開発する場合 (Java のプログラムはクラスごとに独立したファイルに分割できる)、 すべてのソースコードのファイルを提出すること。
Game.java の冒頭のコメントにゲームの内容の説明と、 構成されるファイルを一覧で示すこと。
注意: 開発期間を考え無理のないものを企画し、計画的に作業をすすめること。 あまり欲張ると期限内に完成しない可能性がある。
注意: Javaのグラフィックスの表示速度は、 Windows上の市販のゲームソフトのように高速ではないので、 複雑な表示を高速で行うことが求められるゲームは実現が難しい可能性がある。