| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- /**
- * Copyright (c) 2016 committers of YAKINDU and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- * Contributors:
- * committers of YAKINDU - initial API and implementation
- *
- */
- package traffic.light;
- import java.awt.Graphics;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import javax.imageio.ImageIO;
- import javax.swing.JPanel;
- /**
- *
- * @author Tesch
- *
- */
- public class TrafficLight extends JPanel {
- private boolean red = false;
- private boolean yellow = false;
- private boolean green = false;
- private BufferedImage trafficLight;
- private BufferedImage redLight;
- private BufferedImage greenLight;
- private BufferedImage yellowLight;
-
- public TrafficLight() {
- try {
- trafficLight = ImageIO.read(new File("images/trafficlight_empty.jpg"));
- redLight = ImageIO.read(new File("images/red_light.png"));
- greenLight = ImageIO.read(new File("images/green_light.png"));
- yellowLight = ImageIO.read(new File("images/yellow_light.png"));
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public boolean isRed() {
- return red;
- }
- public void setRed(boolean red) {
- this.red = red;
- }
- public boolean isYellow() {
- return yellow;
- }
- public void setYellow(boolean yellow) {
- this.yellow = yellow;
- }
- public boolean isGreen() {
- return green;
- }
- public void setGreen(boolean green) {
- this.green = green;
- }
- private static final long serialVersionUID = 1L;
- @Override
- public void paint(Graphics graphics) {
- graphics.drawImage(trafficLight, 25, 25, null);
- if (green) {
- graphics.drawImage(greenLight, 26, 236, null);
- }
- if (red) {
- graphics.drawImage(redLight, 25, 25, null);
- }
- if (yellow) {
- graphics.drawImage(yellowLight, 26, 140, null);
- }
- }
- }
|