TrafficLightPanel - public Dimension getPreferredSize...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
//******************************************************************** // TrafficLightPanel.java Author: Lewis/Loftus // // Solution to Programming Project 7.8 //******************************************************************** / import javax.swing.*; import java.awt.*; i public class TrafficLightPanel extends JPanel { private int currentState = 0; private final int NUM_LIGHTS = 3; private final int X = 50, Y = 10, WIDTH = 50, HEIGHT = 130; private final int DIAMETER = 30; private final int X_OFFSET = 10, Y_OFFSET = 10; private final int PANEL_WIDTH = 150, PANEL_HEIGHT = 230; //----------------------------------------------------------------- // Creates the traffic light panel //----------------------------------------------------------------- public void TrafficLightPanel() { setPreferredSize(new Dimension(PANEL_WIDTH, PANEL_HEIGHT));
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: } public Dimension getPreferredSize() { return new Dimension(PANEL_WIDTH, PANEL_HEIGHT); } public void paintComponent(Graphics page) { super.paintComponent(page); int lightOn = currentState % NUM_LIGHTS; setBackground(Color.white); page.setColor(Color.lightGray); page.fillRect(X, Y, WIDTH, HEIGHT); if (lightOn == 0) page.setColor(Color.red); else page.setColor(Color.darkGray); page.fillOval(X+X_OFFSET, Y+Y_OFFSET, DIAMETER, DIAMETER); if (lightOn == 1) page.setColor(Color.yellow); else page.setColor(Color.darkGray); page.fillOval(X+X_OFFSET, Y+DIAMETER+2*Y_OFFSET, DIAMETER, DIAMETER); if (lightOn == 2) page.setColor(Color.green); else page.setColor(Color.darkGray); page.fillOval(X+X_OFFSET, Y+2*DIAMETER+3*Y_OFFSET, DIAMETER, DIAMETER); } public void change() { currentState++; repaint(); } }...
View Full Document

This note was uploaded on 05/17/2011 for the course COP 3530 taught by Professor Davis during the Spring '08 term at University of Florida.

Page1 / 2

TrafficLightPanel - public Dimension getPreferredSize...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online