Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Program #2
- * Put your name here
- */
- package Program2;
- import java.awt.Graphics;
- import java.util.Random;
- import javax.swing.JFrame;
- import static javax.swing.JFrame.EXIT_ON_CLOSE;
- public class Program2 extends JFrame {
- // Constant that stores the height/width of the frame:
- private static final int FRAME_SIZE = 700;
- // Constants that store the top, bottom, left, and right edges of the square
- private static final int SQUARE_TOP = 60;
- private static final int SQUARE_LEFT = 60;
- private static final int SQUARE_RIGHT = 600;
- private static final int SQUARE_BOTTOM = 600;
- // Constant that stores the spacing (in pixels) between the tunnel lines
- private static final int SPACING = 20;
- // Variables that store the row and column of the end point of the tunnel.
- private static int row, column;
- // Random object used to generate random values for row and column
- private static Random rand = new Random();
- // The main() method sets up the JFrame object (guiWindow)
- // and calls the loop() method.
- public static void main(String[] args) {
- Program2 guiWindow = new Program2();
- guiWindow.setSize(FRAME_SIZE, FRAME_SIZE);
- guiWindow.setDefaultCloseOperation(EXIT_ON_CLOSE);
- guiWindow.setVisible(true);
- loop(guiWindow);
- }
- // The loop() method repeatedly generates new values for row and column
- // It then pauses for 2 seconds and tells the frame to re-draw itself.
- public static void loop(Program2 guiWindow) {
- while (true) {
- column = rand.nextInt(SQUARE_RIGHT - SQUARE_LEFT) + SQUARE_LEFT;
- row = rand.nextInt(SQUARE_BOTTOM - SQUARE_TOP) + SQUARE_TOP;
- Wait.manySec(2); // pauses the program for 2 seconds.
- guiWindow.repaint(); // asks guiWindow to re-draw itself.
- }
- }
- @Override
- public void paint(Graphics g) {
- super.paint(g);
- Graphics canvas = getContentPane().getGraphics();
- // Right here you must enter the code that draws the square
- // and the lines from the point (row, column) to the edge of the
- // square so that it looks like a tunnel.
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement