「Javaでお絵書きしてみる (1)」 では ToyGraphics クラスを使って2次元グラフィックスを描く プログラムを学習しました。
ある物体を$(x,y)$の位置に$w \times h$ の大きさで描く javaのメソッド(関数)を作成して下さい。
Sample04.java |
import java.util.*; import java.awt.*; import java.awt.geom.*; public class Sample04 { 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 main(String[] args) { ToyGraphics tg = new ToyGraphics(); tree(tg, 100,100,50,200); tree(tg, 200,100,200,300); tg.repaint(); } } |
「ターミナル」上でタイプする: Sample04.javaの実行例 |
% javac Sample04.java |
Sample05.java |
import java.util.*; import java.awt.*; import java.awt.geom.*; public class Sample05 { 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)); } public static void main(String[] args) { ToyGraphics tg = new ToyGraphics(); snowman(tg, 100,100,50,200); snowman(tg, 200,100,200,300); tg.repaint(); } } |
「ターミナル」上でタイプする: Sample05.javaの実行例 |
% javac Sample05.java |
Sample06.java |
import java.util.*; import java.awt.*; import java.awt.geom.*; public class Sample06 { 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)); } public static void main(String[] args) { ToyGraphics tg = new ToyGraphics(); for (int i=0; i<10; i++) { for (int j=0; j<3; j++) { tree(tg,i*100, 50+j*200, 100+j*50, 200+j*50); } } Random r = new Random(System.currentTimeMillis()); for (int i=0; i<5; i++) { int x = r.nextInt(1280/2); int y = r.nextInt(720/2); int w = 100 + r.nextInt(100); int h = 200 + r.nextInt(200); snowman(tg,x,y,w,h); } tg.repaint(); } } |
「ターミナル」上でタイプする: Sample06.javaの実行例 |
% javac Sample06.java |
オリジナリティ溢れる美しい画像を出力するJavaのプログラムを作成して下さい。 ただし、ある物体を$(x,y)$の位置に$w \times h$の大きさで描く関数を 2種類以上定義して使って下さい。 単に幾何学的な模様だけでなく、動物、乗物、建物など、意味のある構成要素を 登場させることを条件にします。 ファイル名は GReport02.java としましょう。
正しく動作するプログラムのみを提出すること。
作成したプログラムが正しく動作することを確認したら、 「宿題提出Web:1年ゼミ:課題2」 http://ynitta.com/class/1semi/local/handin/up.php?id=kadai2 から提出して下さい。 提出ページの 「提出ファイル」に自分が作成した GReport02.java を指定し、 「コメント欄」には何の絵であるかの説明を記入して下さい。
提出した後は、正しく提出されていることを http://ynitta.com/class/1semi/local/handin/list.php?id=kadai2 で必ず確認しておいて下さい。
〆切は次回の授業の開始時刻0です。
GReport02.java |
public class GReport02 { ....(略)... public static void function_name(ToyGraphics tg, int x, int y, int w, int h) { ....(略)... } public static void main(String[] args) { ToyGraphics tg = new ToyGraphics(); ....(略)... function_name(tg, 100,100,50,200); function_name(tg, 200,100,100,300); ....(略)... tg.repaint(); } } |