// 040240B 土田マーク彰 // (オプションでない部分の)説明: // 雪だるまは色、中心、半径を少しずつ変えて円を重ね書きすることによっ // て立体感を出した。星は色と位置をランダムに変えて10個の記号を描いて // いくようにした。月と重なっているのは少々変だが。 import java.awt.*; import java.awt.event.*; class OptKadai1108 extends Frame { boolean treeVisible; // クリスマスツリー表示フラグ public OptKadai1108(String title) { super(title); treeVisible = false; addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e) { int key = e.getKeyChar(); if(key == 'q') { System.exit(0); } } }); addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { int mx = e.getX(), my = e.getY(); if (400 <= mx && mx <= 600 && 150 <= my && my <= 450) // 雪だるまの付近でプレスしたとき、クリスマスツリーを表示 { treeVisible = true; repaint(); } } public void mouseReleased(MouseEvent e) { if (treeVisible) { treeVisible = false; // どこでボタンをはなしてもツリーを消す repaint(); } } }); } public static void main(String[] args) { OptKadai1108 frame = new OptKadai1108("OptKadai1108"); frame.setSize(640, 480); frame.setVisible(true); } public void paint(Graphics g) { int i; g.setColor(Color.black); // 夜空 g.fillRect(0, 0, 640, 480); g.setColor(Color.yellow); // 三日月 g.fillOval(-20, -20, 240, 240); g.setColor(Color.black); g.fillOval(-50, -50, 240, 240); String[] stars = {"*", "s", "#", "^", "o", "+", "\"", "i", "x", "?"}; // 星 g.setFont(new Font("Times", Font.PLAIN, 12)); for (i = 0; i <= 300; i++) { int red = (int)(Math.random()*255); // 色はランダム int green = (int)(Math.random()*255); int blue = (int)(Math.random()*255); g.setColor(new Color(red, green, blue)); int x = (int)(Math.random()*639); // 位置もランダム int y = (int)(Math.random()*210); g.drawString(stars[i % 10], x, y); } g.setColor(Color.gray); // 地面 g.fillRect(0, 220, 640, 260); g.setColor(Color.darkGray); // 地面の線 for (i = 0; i <= 15; i++) { g.drawLine(0, 220 + i*i, 639, 220 + i*i); } for (i = 0; i <= 20; i++) // 雪だるまの身体 { g.setColor(new Color(180 + 3*i, 180 + 3*i, 180 + 3*i)); g.fillOval(400 + 2*i/3, 250 + 2*i/3, 200 - 2*i, 200 - 2*i); } for (i = 0; i <= 20; i++) // 雪だるまの頭 { g.setColor(new Color(180 + 3*i, 180 + 3*i, 180 + 3*i)); g.fillOval(430 + 3*i/5, 150 + 3*i/5, 120 - 2*i, 120 - 2*i); } g.setColor(Color.black); // 雪だるまの左目 int[] lex = { 465, 463, 468, 473, 471 }; int[] ley = { 186, 190, 194, 195, 186 }; g.fillPolygon(lex, ley, 5); int[] rex = { 508, 505, 511, 516 }; // 右目 int[] rey = { 185, 193, 195, 188 }; g.fillPolygon(rex, rey, 4); int[] nx = { 487, 482, 497, 492 }; // 鼻 int[] ny = { 198, 225, 228, 194 }; g.fillPolygon(nx, ny, 4); g.setColor(Color.orange); // 口(にんじん) int[] mx = { 460, 458, 520, 518 }; int[] my = { 236, 244, 250, 230 }; g.fillPolygon(mx, my, 4); g.setColor(Color.green); // にんじんの葉 int[] mlx = { 519, 519, 522, 523 }; int[] mly = { 235, 245, 248, 232 }; g.fillPolygon(mlx, mly, 4); if (treeVisible) // クリスマスツリー { g.setColor(new Color(165, 42, 42)); // 幹 g.fillRect(235, 370, 30, 90); g.setColor(new Color(0, 100, 0)); // 葉 int[] tx = { 250, 210, 230, 190, 225, 170, 330, 275, 310, 270, 290 }; int[] ty = { 100, 180, 180, 270, 270, 370, 370, 270, 270, 180, 180 }; g.fillPolygon(tx, ty, 11); } } }