「Javaでお絵書きしてみる (1)」 では ToyGraphics クラスを使って2次元グラフィックスを描く プログラムを学習しました。
「Javaでお絵書きしてみる (2)」 では、ある物体を$(x,y)$の位置に$w \times h$ の大きさで描く javaのメソッド(関数)を作成しました。
時間の経過とともに画面が変化する(アニメーション)プログラムを作成して下さい。
Sample07.java |
import java.util.*; import java.awt.*; import java.awt.geom.*; public class Sample07 { public static void tree(ToyGraphics tg, int x, int y, int w, int h) { int[] xp = { x+w/2, x+w/4, x+w/3, x, x+w, x+w*2/3, x+w*3/4 }; int[] yp = { y, y+h/3, y+h/3, y+h*2/3, y+h*2/3, y+h/3, y+h/3 }; tg.g2d.setColor(new Color(50,255,50)); tg.g2d.fill(new Polygon(xp,yp,xp.length)); int[] xp2 = { x+w/3, x+w/3, x+w*2/3, x+w*2/3 }; int[] yp2 = { y+h*2/3, y+h, y+h, y+h*2/3 }; tg.g2d.setColor(new Color(200,128,50)); tg.g2d.fill(new Polygon(xp2,yp2,xp2.length)); } public static void snowman(ToyGraphics tg, int x, int y, int w, int h) { tg.g2d.setColor(new Color(232,232,232)); tg.g2d.fill(new Arc2D.Double(x+w/4, y, w/2, h/2, 0, 360, Arc2D.CHORD)); tg.g2d.fill(new Arc2D.Double(x, y+h*2/5, w, h*3/5, 0, 360, Arc2D.CHORD)); tg.g2d.setColor(new Color(128,128,128)); tg.g2d.fill(new Arc2D.Double(x+w*(1.0/2 - 2.0/10), y+h/8, w/10, h/8, 0, 360, Arc2D.CHORD)); tg.g2d.fill(new Arc2D.Double(x+w*(1.0/2 + 1.0/10), y+h/8, w/10, h/8, 0, 360, Arc2D.CHORD)); } static int W=1280; static int H=720; static int N=20; public static void main(String[] args) { ToyGraphics tg = new ToyGraphics(); int[][] trees = new int[500][2]; Random rand = new Random(123); for (int i=0; i<trees.length; i++) { trees[i][0] = rand.nextInt(W * N); trees[i][1] = rand.nextInt(2*H) - H; } int x = 0; int y = 0; int sx = W/8; int sy = H/2; while (x < W) { tg.g2d.setColor(new Color(0,0,0)); tg.g2d.fill(new Rectangle(0,0,W,H)); x += 10; for (int i=0; i<trees.length; i++) { tree(tg, trees[i][0]-x+sx, trees[i][1]-y+sy, 100, 100); } snowman(tg,sx,sy,100,100); tg.repaint(); try { Thread.sleep(50); // 50 msec } catch (Exception e) {} } tg.repaint(); } } |
「ターミナル」上でタイプする: Sample07.javaの実行例 |
% javac Sample07.java |
オリジナリティ溢れる美しい画像を出力するJavaのプログラムを作成して下さい。 ただし、時間経過と共に画面が変化するようにして下さい。 単に幾何学的な模様だけでなく、動物、乗物、建物など、意味のある構成要素を 登場させることを条件にします。 ファイル名は GReport03.java としましょう。
正しく動作するプログラムのみを提出すること。
作成したプログラムが正しく動作することを確認したら、 「宿題提出Web:1年ゼミ:課題3」 http://ynitta.com/class/1semi/local/handin/up.php?id=kadai3 から提出して下さい。 提出ページの 「提出ファイル」に自分が作成した GReport03.java を指定し、 「コメント欄」には何の絵であるかの説明を記入して下さい。
提出した後は、正しく提出されていることを http://ynitta.com/class/1semi/local/handin/list.php?id=kadai3 で必ず確認しておいて下さい。
〆切は1週間後の10:30です。
GReport03.java |
public class GReport03 { ....(略)... public static void main(Strings[] args) { ToyGraphics tg = new ToyGraphics(); ....(略)... for (double clock=0.0; clock < 1.0; clock += 0.001) { tg.g2d.setColor(new Color(0,0,0)); tg.g2d.fill(new Rectangle(0,0,1280,720)); // 画面全体を黒く塗り潰して ....(略)... // clock の値によって位置、方向、大きさなどを変更して画像を描く ....(略)... tg.repaint(); // clock時点での画像を表示する try { Thread.sleep(50); // 50 msec だけプログラムの実行を停止する } catch (Exception e) {} } } } |