TrafficLight.java 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /**
  2. * Copyright (c) 2016 committers of YAKINDU and others.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/epl-v10.html
  7. * Contributors:
  8. * committers of YAKINDU - initial API and implementation
  9. *
  10. */
  11. package traffic.light;
  12. import java.awt.Graphics;
  13. import java.awt.image.BufferedImage;
  14. import java.io.File;
  15. import java.io.IOException;
  16. import javax.imageio.ImageIO;
  17. import javax.swing.JPanel;
  18. /**
  19. *
  20. * @author Tesch
  21. *
  22. */
  23. public class TrafficLight extends JPanel {
  24. private boolean red = false;
  25. private boolean yellow = false;
  26. private boolean green = false;
  27. private BufferedImage trafficLight;
  28. private BufferedImage redLight;
  29. private BufferedImage greenLight;
  30. private BufferedImage yellowLight;
  31. public TrafficLight() {
  32. try {
  33. trafficLight = ImageIO.read(new File("images/trafficlight_empty.jpg"));
  34. redLight = ImageIO.read(new File("images/red_light.png"));
  35. greenLight = ImageIO.read(new File("images/green_light.png"));
  36. yellowLight = ImageIO.read(new File("images/yellow_light.png"));
  37. } catch (IOException e) {
  38. e.printStackTrace();
  39. }
  40. }
  41. public boolean isRed() {
  42. return red;
  43. }
  44. public void setRed(boolean red) {
  45. this.red = red;
  46. }
  47. public boolean isYellow() {
  48. return yellow;
  49. }
  50. public void setYellow(boolean yellow) {
  51. this.yellow = yellow;
  52. }
  53. public boolean isGreen() {
  54. return green;
  55. }
  56. public void setGreen(boolean green) {
  57. this.green = green;
  58. }
  59. private static final long serialVersionUID = 1L;
  60. @Override
  61. public void paint(Graphics graphics) {
  62. graphics.drawImage(trafficLight, 25, 25, null);
  63. if (green) {
  64. graphics.drawImage(greenLight, 26, 236, null);
  65. }
  66. if (red) {
  67. graphics.drawImage(redLight, 25, 25, null);
  68. }
  69. if (yellow) {
  70. graphics.drawImage(yellowLight, 26, 140, null);
  71. }
  72. }
  73. }