Advertisement
Guest User

Untitled

a guest
Oct 4th, 2015
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.43 KB | None | 0 0
  1. import java.awt.*;
  2. import javax.swing.*;
  3. import java.awt.event.*;
  4.  
  5. /**
  6. * Class TrafficLights - At the click of a button, change the traffic lights
  7. *
  8. * @author Mickey Mouse
  9. * @version JDK 8
  10. * @course CSCI 1130-01
  11. * @date 10-2-15
  12. */
  13. public class TrafficLights extends JApplet implements ActionListener {
  14. private JButton WAIT, STOP, GO;
  15.  
  16. private boolean clickWAIT = false;
  17. private boolean clickSTOP = false;
  18. private boolean clickGO = false;
  19.  
  20. private int carX = 200;
  21.  
  22. /**
  23. * Called by the browser or applet viewer to inform this JApplet that it
  24. * has been loaded into the system. It is always called before the first
  25. * time that the start method is called.
  26. */
  27. public void init()
  28. {
  29. setLayout( new FlowLayout()); // changes the layout from BorderLayout to FlowLayout
  30.  
  31. WAIT = new JButton ("WAIT"); //adds WAIT label to button
  32. WAIT.setForeground(Color.yellow); //changes the label to yellow
  33.  
  34. //adds the WAIT JButton to the screen
  35. add (WAIT);
  36. WAIT.addActionListener(this);
  37.  
  38.  
  39. GO = new JButton ("GO"); //adds GO label to button
  40. GO.setForeground(Color.green); //changes the label to green
  41.  
  42. //adds the button to the screen
  43. add (GO);
  44. GO.addActionListener(this);
  45.  
  46.  
  47. STOP = new JButton ("STOP"); //adds STOP label to button
  48. STOP.setForeground(Color.red); //changes the label to red
  49.  
  50. //adds STOP JButton to screen
  51. add (STOP);
  52. STOP.addActionListener(this);
  53.  
  54.  
  55. }
  56.  
  57.  
  58.  
  59. /**
  60. * Called by the browser or applet viewer to inform this JApplet that it
  61. * should start its execution. It is called after the init method and
  62. * each time the JApplet is revisited in a Web page.
  63. */
  64. public void start()
  65. {
  66. // provide any code requred to run each time
  67. // web page is visited
  68. }
  69.  
  70. /**
  71. * Called by the browser or applet viewer to inform this JApplet that
  72. * it should stop its execution. It is called when the Web page that
  73. * contains this JApplet has been replaced by another page, and also
  74. * just before the JApplet is to be destroyed.
  75. */
  76. public void stop()
  77. {
  78. // provide any code that needs to be run when page
  79. // is replaced by another page or before JApplet is destroyed
  80. }
  81.  
  82. /**
  83. * Paint method for applet.
  84. *
  85. * @param g the Graphics object for this applet
  86. */
  87. public void paint(Graphics g)
  88. {
  89. super.paint(g);
  90.  
  91. //declares and retrieves the images from their file locations
  92. Image img = getImage(getDocumentBase(), "stoplights.png");
  93. Image img2 = getImage(getDocumentBase(), "car.jpg");
  94.  
  95. g.drawImage( img, 50, 100, 300, 350, 0, 0, 5000, 5000, this ); //draws and resizes the stoplights image
  96. g.drawImage( img2, carX, 400, 1000, 1000, 0, 0, 5000, 5000, this); //draws and resizes the car image
  97.  
  98. //draw and fill an oval red for the STOP stoplight when STOP is pressed
  99. if (clickSTOP == true);
  100. {
  101. g.drawOval(63, 112, 30, 30);
  102. g.setColor(Color.red);
  103. g.fillOval(63, 112, 30, 30);
  104. clickSTOP = false;
  105. }
  106.  
  107.  
  108. //draw and fill an oval yellow for the WAIT stoplight when WAIT is pressed
  109. if (clickWAIT == true);
  110. {
  111. g.setColor(Color.black);
  112. g.drawOval(63, 148, 30, 30);
  113. g.setColor(Color.orange);
  114. g.fillOval(63, 148, 30, 30);
  115. clickWAIT = false;
  116. }
  117.  
  118. //draw and fill an oval green for the GO stoplight when GO is pressed
  119. if (clickGO == true);
  120. {
  121. g.setColor(Color.black);
  122. g.drawOval(63, 184, 30, 30);
  123. g.setColor(Color.green);
  124. g.fillOval(63, 184, 30, 30);
  125. clickGO = false;
  126. }
  127.  
  128.  
  129.  
  130. }
  131.  
  132.  
  133. public void actionPerformed(ActionEvent event)
  134. {
  135. /*
  136. * Links the JButtons and the graphic sequences to display the lights
  137. *
  138. */
  139. if(event.getSource() == GO) //display green if GO is clicked
  140. {
  141. clickGO = true;
  142. carX -=15;
  143. repaint();
  144. }
  145. if (event.getSource() == WAIT) //display yellow if WAIT is clicked
  146. {
  147. clickWAIT = true;
  148. repaint();
  149. }
  150. if (event.getSource() == STOP) //display red if STOP is clicked
  151. {
  152. clickSTOP = true;
  153. repaint();
  154. }
  155.  
  156. }
  157.  
  158.  
  159. /**
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement