The first thing that we need to do is to set up your directories for CPSC170 and make Eclipse recognize them.
Now open Firefox and go to the CPSC170 home page at http://cs.roanoke.edu/CPSC170A. Bookmark this page, then follow the link for lab1 to open this document in the browser.
The files BouncingBall.java and BouncingBallPanel.java animate a bouncing ball in a window like we created in class. Look at the code for the BouncingBallPanel class. Notice in the constructor that the timer object that generates the periodic events is started using the start() method. The Timer class also has a stop() method that when envoked stops the timer from generating more events. Add to the BouncingBallPanel a button that when pressed toggles the animation of the bouncing ball, that is if the ball is moving and the button is pressed it stops, and if the ball is stopped and the button is pressed the ball begins moving.
The files KeyboardControl.java and KeyboardControlPanel.java use keyboard events to echo what characters are typed with the keyboard. Information on keyboard events can be found in your book on page 298. Download these files, run the program, and look at the code for the panel. Notice the following:
Modify KeyboardControl.java and KeyboardControlPanel.java to draw a rectangle that moves based on presses of the arrow keys. Begin by first making the preferred size of the panel larger and then adding a paintComponent method that draws the rectangle. Make sure that the location of the rectangle is determined by instance variables. Then add code to the keyPressed method that modifies the rectangle's location instance variables if an arrow key is pressed. (Don't forget to repaint the frame.) You can use the event.getKeyCode method and constants KeyEvent.VK_UP, KeyEvent.VK_DOWN, KeyEvent.VK_LEFT, KeyEvent.VK_RIGHT as is shown in the code sample in your book on page 301. Finally, add code to prevent the rectangle from being able to move off of the screen.
Combine the bouncing ball and keyboard controlled rectangle to create a simple one-player version of Pong. Begin by creating two new java files Pong.java and PongPanel.java. The main method of the Pong.java file should create a JFrame and add a PongPanel to its content pane. The PongPanel class should have a timer that animates a bouncing ball as in the BouncingBall.java, except the ball should not bounce off of the bottom side of the window. If the ball hits the bottom side of the window the animation should stop. (Recall, you can use the stop() method of the Timer class to stop the animation.) Then add to the program a keyboard controlled flat rectangle at very bottom of the window. It should only be able to be moved left and right. Finally, add code that will bounce the ball if it hits the rectangle.
Hand In: Tar your lab directory and e-mail it to your instructor with cpsc170 lab1 in the subject.