RandomShapes

Retired DISLab
Swpark (토론 | 기여) 사용자의 2014년 1월 26일 (일) 09:45 버전
(비교) ← 이전 판 | 현재 판 (비교) | 다음 판 → (비교)
이동: 둘러보기, 찾기

RandomShapes.java

/*
 * File: RandomShapes.java
 * -----------------------
 * This file creates ten boxes, ovals, and stars at random locations
 * on the screen, pausing for a suitable interval between each one.
 */
 
import acm.graphics.*;
import acm.program.*;
import acm.util.*;
 
public class RandomShapes extends GraphicsProgram {
 
	/** Runs the program */
	public void run() {
		while (true) {
			for (int i = 0; i < NOBJECTS; i++) {
				addOneRandomShape();
				pause(PAUSE_TIME);
			}
			waitForClick();
			removeAll();
		}
	}
 
	/* Adds a random shape to the canvas */
	private void addOneRandomShape() {
		GObject gobj = createRandomShape();
		gobj.setColor(rgen.nextColor());
		if (gobj instanceof GFillable)
			((GFillable)gobj).setFilled(true);
		double x = rgen.nextDouble(0, getWidth() - gobj.getWidth())
					- gobj.getBounds().getX();
		double y = rgen.nextDouble(0, getHeight() - gobj.getHeight())
					- gobj.getBounds().getY();
		add(gobj, x, y);
	}
 
	/* Generates a random shape whose reference point is the origin */
	private GObject createRandomShape() {
		double width = rgen.nextDouble(MIN_SIZE, MAX_SIZE);
		double height = rgen.nextDouble(MIN_SIZE, MAX_SIZE);
 
		switch (rgen.nextInt(4)) {
		case 0: return new GRect(width, height);
		case 1: return new GOval(width, height);
		case 2: return new GStar(width);
		case 3: return new GFace(width, height);
		default: throw new ErrorException("Illegal shape index");
		}
	}
 
	/* private constants*/
	private static final int NOBJECTS = 10;
	private static final int PAUSE_TIME = 1000;
	private static final double MIN_SIZE = 25;
	private static final double MAX_SIZE = 150;
 
	/* private instance variables */
	private RandomGenerator rgen = RandomGenerator.getInstance();
 
	private static final long serialVersionUID = 1;
}


GStar.java

/*
 * File: GStar.java
 * ----------------
 * This file illustrates the strategy of subclassing GPolygon by
 * creating a new GObject class depicting a five-pointed star.
 */
 
import acm.graphics.*;
 
/**
 * Defines a new GObject class that appears as a five-pointed star.
 */
public class GStar extends GPolygon {
 
	/**
	 * Creates a new GStar centered at the origin that fits inside
	 * a square of the specified size.
	 */
	public GStar(double size) {
		double sinTheta = GMath.sinDegrees(18);
		double b = 0.5 * sinTheta / (1.0 + sinTheta);
		double edge = (0.5 - b) * size;
		addVertex(-size / 2, -b * size);
 
		int angle = 0;
		for (int i = 0; i < 5; i++) {
			addPolarEdge(edge, angle);
			addPolarEdge(edge, angle + 72);
			angle -= 72;
		}
 
		markAsComplete();
	}
 
	/**
	 * Creates a new GStar centered at the point (x, y) that fits inside
	 * a square of the specified size.
	 */
	public GStar(double x, double y, double size) {
		this(size);
		setLocation(x, y);
	}
}


GFace.java

import acm.graphics.*;
 
public class GFace extends GCompound {
 
	/** Construct a new GFace object with the specified dimensions */
	public GFace(double width, double height) {
		head = new GOval(width, height);
		leftEye = new GOval(EYE_WIDTH * width, EYE_HEIGHT * height);
		rightEye = new GOval(EYE_WIDTH * width, EYE_HEIGHT * height);
		nose = createNose(NOSE_WIDTH * width, NOSE_HEIGHT * height);
		mouth = new GRect(MOUTH_WIDTH * width, MOUTH_HEIGHT * height);
 
		add(head, -width / 2, -height / 2);
		add(leftEye, -0.25 * width - EYE_WIDTH * width / 2,
					 -0.25 * height - EYE_HEIGHT * height / 2);
		add(rightEye, 0.25 * width - EYE_WIDTH * width / 2,
					 -0.25 * height - EYE_HEIGHT * height / 2);
		add(nose, 0, 0);
		add(mouth, -MOUTH_WIDTH * width / 2,
				   0.25 * height - MOUTH_HEIGHT * height / 2);
	}
 
	/* Creates a triangle for the nose */
	private GPolygon createNose(double width, double height) {
		GPolygon poly = new GPolygon();
		poly.addVertex(0, -height / 2);
		poly.addVertex(width / 2, height / 2);
		poly.addVertex(-width / 2, height / 2);
		return poly;
	}
 
	/* Constants specifying feature size as a fraction of the head size */
	private static final double EYE_WIDTH 	= 0.15;
	private static final double EYE_HEIGHT	= 0.15;
	private static final double NOSE_WIDTH	= 0.15;
	private static final double NOSE_HEIGHT	= 0.15;
	private static final double MOUTH_WIDTH	= 0.50;
	private static final double MOUTH_HEIGHT = 0.03;
 
	/* Private instance variables */
	private GOval head;
	private GOval leftEye, rightEye;
	private GPolygon nose;
	private GRect mouth;
}
개인 도구
이름공간
변수
행위
둘러보기
구성원
연구
연구실
기타
도구모음
인쇄/내보내기