StoplightGraphics

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

StoplightGraphics.java

import acm.program.*;
import java.awt.event.*;
import javax.swing.*;
 
/**
 * This class displays four buttons at the south edge of the window.
 * Pressing a button lights the indicated lamp in the stoplight or
 * advances the stoplight to its next configuration.
 */
public class StoplightGraphics extends GraphicsProgram {
 
	/** Initialize the buttons and create the stoplight */
	public void init() {
		setSize(500, 500);
 
		add(new JButton("Green"), SOUTH);
		add(new JButton("Yellow"), SOUTH);
		add(new JButton("Red"), SOUTH);
		add(new JButton("Advance"), SOUTH);
 
		signal = new Stoplight();
		add(signal, getWidth() / 2, getHeight() / 2);
		addActionListeners();
	}
 
	/** Listen for a button action */
	public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();
		if (command.equals("Advance")) {
			signal.advance();
		} else if (command.equals("Red")) {
			signal.setState(Stoplight.RED);
		} else if (command.equals("Yellow")) {
			signal.setState(Stoplight.YELLOW);
		} else if (command.equals("Green")) {
			signal.setState(Stoplight.GREEN);
		}
	}
 
	/* Private instance variables */
	private Stoplight signal;
 
	private static final long serialVersionUID = 1;
}


Stoplight.java

import acm.graphics.*;
import acm.util.*;
import java.awt.*;
 
/**
 * This program represents a graphical stoplight with its origin point
 * at the center.
 */
public class Stoplight extends GCompound {
 
	/* Public constant for the colors */
	public static final Color RED = Color.RED;
	public static final Color YELLOW = Color.YELLOW;
	public static final Color GREEN = Color.GREEN;
 
	/** Creates a new Stoplight object, which is initially red */
	public Stoplight() {
		GRect frame = new GRect(STOPLIGHT_WIDTH, STOPLIGHT_HEIGHT);
		frame.setFilled(true);
		frame.setColor(Color.DARK_GRAY);
		add(frame, -STOPLIGHT_WIDTH / 2, -STOPLIGHT_HEIGHT / 2);
 
		redLamp = createLamp(0, -STOPLIGHT_HEIGHT / 4);
		yellowLamp = createLamp(0, 0);
		greenLamp = createLamp(0, STOPLIGHT_HEIGHT / 4);
		add(redLamp);
		add(yellowLamp);
		add(greenLamp);
 
		setState(RED);
	}
 
	/** Changes the state of the stoplight to the indicated color */
	public void setState(Color color) {
		state = color;
		redLamp.setColor((state == RED) ? RED : Color.GRAY);
		yellowLamp.setColor((state == YELLOW) ? YELLOW : Color.GRAY);
		greenLamp.setColor((state == GREEN) ? GREEN : Color.GRAY);
	}
 
	/** Returns the current state of the stoplight */
	public Color getState() {
		return state;
	}
 
	/** Advances the stoplight to the next state */
	public void advance() {
		if (state == RED) {
			setState(GREEN);
		} else if (state == YELLOW) {
			setState(RED);
		} else if (state == GREEN) {
			setState(YELLOW);
		} else {
			throw new ErrorException("Illegal stoplight state");
		}
	}
 
	/* Craetes a new GOval to represent one of the three lamps */
	private GOval createLamp(double x, double y) {
		GOval lamp = new GOval(x - LAMP_RADIUS, y - LAMP_RADIUS,
							   2 * LAMP_RADIUS, 2 * LAMP_RADIUS);
		lamp.setFilled(true);
		return lamp;
 
	}
 
	/* Private constants */
	private static final double STOPLIGHT_WIDTH = 50;
	private static final double STOPLIGHT_HEIGHT = 100;
	private static final double LAMP_RADIUS = 10;
 
	/* Private instance variables */
	private Color state;
	private GOval redLamp;
	private GOval yellowLamp;
	private GOval greenLamp;
}
개인 도구
이름공간
변수
행위
둘러보기
구성원
연구
연구실
기타
도구모음
인쇄/내보내기