SHARE
TWEET

Untitled

a guest Jan 22nd, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import javax.swing.JFrame;
  2. public class TrafficLight
  3. {
  4.     //----------------------------------------------
  5. // Creates the main program frame.
  6. //----------------------------------------------
  7. public static void main(String[] args)
  8. {
  9. JFrame frame = new JFrame("Traffic Light");
  10. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  11. frame.getContentPane().add(new TrafficControlPanel());
  12. frame.pack();
  13. frame.setVisible(true);
  14.     }
  15. }
  16. //**********************************************************************************
  17. //**********************************************************************************
  18. //**********************************************************************************
  19.  
  20. import javax.swing.*;
  21. import java.awt.*;
  22. import java.awt.event.ActionEvent;
  23. import java.awt.event.ActionListener;
  24. public class TrafficControlPanel extends JPanel
  25. {
  26.     private JButton switchLight;
  27.     TrafficLightPanel lightPanel = new TrafficLightPanel();
  28. public TrafficControlPanel()
  29. {
  30. switchLight = new JButton("Change Light");
  31. switchLight.addActionListener(new ChangeListener());
  32. add(lightPanel, BorderLayout.CENTER);
  33. add(switchLight, BorderLayout.SOUTH);
  34. }
  35. private class ChangeListener implements ActionListener
  36. {
  37. public void actionPerformed(ActionEvent event)
  38. {
  39.     lightPanel.change();
  40. }
  41. }
  42. }
  43. //**********************************************************************************
  44. //**********************************************************************************
  45. //**********************************************************************************
  46.  
  47.  
  48. //********************************************************************
  49. //  TrafficLightPanel.java
  50. //********************************************************************
  51. import javax.swing.*;
  52. import java.awt.*;
  53. public class TrafficLightPanel extends JPanel
  54. {
  55. private int currentState = 0;
  56. private final int NUM_LIGHTS = 3; // the number of lights
  57. private final int X = 50, Y = 10, WIDTH = 50, HEIGHT = 130; //box
  58. //size
  59. private final int DIAMETER = 30; // light diameter
  60. private final int X_OFFSET = 10, Y_OFFSET = 10; // offsets to
  61. //position the lights in the box
  62. private final int PANEL_WIDTH = 150, PANEL_HEIGHT = 230; // the size
  63. //of a traffic control panel
  64.    //-----------------------------------------------------------------
  65.    //  Creates the traffic light panel
  66.    //-----------------------------------------------------------------
  67. public TrafficLightPanel()
  68. {
  69.       setPreferredSize(new Dimension(PANEL_WIDTH, PANEL_HEIGHT));
  70.    }
  71. public void paintComponent(Graphics page)
  72. {
  73.       super.paintComponent(page);
  74. int lightOn = currentState % NUM_LIGHTS;
  75. setBackground(Color.white);
  76. page.setColor(Color.lightGray);
  77. page.fillRect(X, Y, WIDTH, HEIGHT);
  78. // add code to determine the light to turn on and turn on the
  79. //light
  80. if (lightOn == 0)
  81. {
  82.     page.setColor(Color.red);
  83.     page.fillOval(X + X_OFFSET, Y + Y_OFFSET, DIAMETER, DIAMETER);
  84.     page.setColor(Color.darkGray);
  85.     page.fillOval(X + X_OFFSET, Y + (2 * Y_OFFSET) + DIAMETER,
  86.             DIAMETER, DIAMETER);
  87.     page.fillOval(X + X_OFFSET, Y + (3 * Y_OFFSET) + (2 * DIAMETER),
  88.             DIAMETER, DIAMETER);
  89. }
  90. if (lightOn == 1)
  91. {
  92.     page.setColor(Color.green);
  93.     page.fillOval(X + X_OFFSET, Y + (3 * Y_OFFSET) + (2 * DIAMETER),
  94.             DIAMETER, DIAMETER);
  95.     page.setColor(Color.darkGray);
  96.     page.fillOval(X + X_OFFSET, Y + Y_OFFSET, DIAMETER, DIAMETER);
  97.     page.fillOval(X + X_OFFSET, Y + (2 * Y_OFFSET) + DIAMETER,
  98.             DIAMETER, DIAMETER);
  99. }
  100. if (lightOn == 2)
  101. {
  102.     page.setColor(Color.yellow);
  103.     page.fillOval(X + X_OFFSET, Y + (2 * Y_OFFSET) + DIAMETER,
  104.             DIAMETER, DIAMETER);
  105.     page.setColor(Color.darkGray);
  106.     page.fillOval(X + X_OFFSET, Y + Y_OFFSET, DIAMETER, DIAMETER);
  107.     page.fillOval(X + X_OFFSET, Y + (3 * Y_OFFSET) + (2 * DIAMETER),
  108.             DIAMETER, DIAMETER);
  109. }
  110. }
  111. public void change()
  112. {
  113.       currentState++;
  114. repaint();
  115.    }
  116. }
  117. //**********************************************************************************
  118. //**********************************************************************************
  119. //**********************************************************************************
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top