Advertisement
Guest User

program2

a guest
Oct 17th, 2013
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. /*
  2. * Program #2
  3. * Put your name here
  4. */
  5. package Program2;
  6.  
  7. import java.awt.Graphics;
  8. import java.util.Random;
  9. import javax.swing.JFrame;
  10. import static javax.swing.JFrame.EXIT_ON_CLOSE;
  11.  
  12. public class Program2 extends JFrame {
  13.  
  14. // Constant that stores the height/width of the frame:
  15. private static final int FRAME_SIZE = 700;
  16. // Constants that store the top, bottom, left, and right edges of the square
  17. private static final int SQUARE_TOP = 60;
  18. private static final int SQUARE_LEFT = 60;
  19. private static final int SQUARE_RIGHT = 600;
  20. private static final int SQUARE_BOTTOM = 600;
  21. // Constant that stores the spacing (in pixels) between the tunnel lines
  22. private static final int SPACING = 20;
  23. // Variables that store the row and column of the end point of the tunnel.
  24. private static int row, column;
  25. // Random object used to generate random values for row and column
  26. private static Random rand = new Random();
  27.  
  28. // The main() method sets up the JFrame object (guiWindow)
  29. // and calls the loop() method.
  30. public static void main(String[] args) {
  31. Program2 guiWindow = new Program2();
  32. guiWindow.setSize(FRAME_SIZE, FRAME_SIZE);
  33. guiWindow.setDefaultCloseOperation(EXIT_ON_CLOSE);
  34. guiWindow.setVisible(true);
  35. loop(guiWindow);
  36. }
  37.  
  38. // The loop() method repeatedly generates new values for row and column
  39. // It then pauses for 2 seconds and tells the frame to re-draw itself.
  40. public static void loop(Program2 guiWindow) {
  41. while (true) {
  42. column = rand.nextInt(SQUARE_RIGHT - SQUARE_LEFT) + SQUARE_LEFT;
  43. row = rand.nextInt(SQUARE_BOTTOM - SQUARE_TOP) + SQUARE_TOP;
  44. Wait.manySec(2); // pauses the program for 2 seconds.
  45. guiWindow.repaint(); // asks guiWindow to re-draw itself.
  46. }
  47. }
  48.  
  49. @Override
  50. public void paint(Graphics g) {
  51. super.paint(g);
  52. Graphics canvas = getContentPane().getGraphics();
  53.  
  54. // Right here you must enter the code that draws the square
  55. // and the lines from the point (row, column) to the edge of the
  56. // square so that it looks like a tunnel.
  57.  
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement