Javaプログラミング基礎 冬休みの宿題

解答は

に提出しなさい。クラスファイル (〜.class) は提出しなくてよい。 提出には gFTP 等の ftp ソフトを用いること。

単位取得最低限のためには、問題1の提出が必須。 問題2の提出は任意 (中上級者向け課題) 。

プログラムの一部または全部をコピーしたものが提出された場合、 不正行為として扱う。

問題1 (Basic)

例題のボールのアニメーションを拡張し、 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メソッドを書くクラスは BallArt とし、 ファイル名は BallArt.java とする。

問題2 (Standard〜Advanced)

今回の 参考資料 を学んだ上で、 マウスやキーボード入力でアニメーション表示に面白い作用を与えるような、 対話的アニメーション作品を作りなさい。

独自のアイディアに基づく作品とすること。 例えば、以下のようなサンプルが考えられる。

ソースコードのファイル名は InteractiveArt.java とする。 画像データなどがあればそれらを含めて、 構成するすべてのファイルを提出しなさい。 なお、ソースコードを複数のファイルに分割して開発する場合 (Java のプログラムはクラスごとに独立したファイルに分割できる)、 すべてのソースコードのファイルを提出すること。