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();
}
}
|