CPSC 170 Lab 1: Events

Getting Started

The first thing that we need to do is to set up your directories for CPSC170 and make Eclipse recognize them.

  1. Open an xterm.
  2. In your home directory, create a subdirectory called cpsc170 (mkdir).
  3. Open Eclipse.
  4. From the File menu, select the Switch Workspace option.
  5. Alter the path to reflect the cpsc170 directory you just created and press ok. Eclipse will appear to restart. (You may need to close the welcome screen)
  6. From the File menu, expand the New option and select Project.
  7. In the following dialog, verify that "Java Project" is highlighted and select "next".
  8. In the "Project Name" field, type "Labs" (Note the capital "L") and press Finish.
  9. Repeat this process to create an "Assign" project.
  10. In the Labs project, create a package called lab1 (note the lowercase "l"). Observe the JRE being used - it should read java-6. If it doesn't, you will need to re-import the RCEclipsePrefs file that you used last term

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.

Timer Events

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.

Keyboard Events

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.

Pong

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.