CPSC 170 Lab 1: Events

Getting Started

Eclipse is installed on the lab machines but it has not been added to the gnome application menu. In order to add it:

  1. Right-click on the gnome menu bar and choose Add to Panel...
  2. In the Add to Panel dialog choose Custom Application Launcher and click Add.
  3. In the Create Launcher dialog type "Eclipse" in the Name text field.
  4. In the the Command text field type "/usr/local/eclipse/eclipse".
  5. Click on the spring icon on the left.
  6. In the Browse icons dialog click on the Browse... button.
  7. In the Browse dialog choose File System from the list of places, browse to the directory /usr/share/app-install/icons, and click the Open button.
  8. After the icons load, scroll down to and double-click eclipse48.png.
  9. Click the OK button in Create Launcher dialog and the Close button in the Add to Panel dialog.
  10. Launch Eclipse by clicking on the application icon you just added.

The first time that you launch Eclipse you will be presented with the Workspace Launcher dialog. A workspace in eclipse is a directory where all programming projects are stored. To create a workspace for this course:

  1. Change the workspace directory listed in the text field of the Workspace Launcher by choosing the Browse.. button.
  2. In the Select Workspace Directory file dialog, click the Create Folder button and type 'CPSC170A', hit enter to create the directory, and finally click the OK button to select the directory as the Eclipse workspace.
  3. Click the Use this as the default check box if you do not want to see this dialog every time you launch Eclipse.
  4. Click the 'OK' button.

After Eclipse finishes launching you will be presented with the welcome screen. This screen contains links to information about Eclipse including tutorials to help you learn Eclipse. If you ever want to come back to this screen you can select Help>Welcome from the application menu bar. For now, click on the button with a curved arrow on the far right of the screen to go to the workbench.

The workbench should be empty because there are no projects in the newly created workspace. A project in eclipse is a collection code with a common purpose. For this class, you will create a new project for each lab (and optionally for each assignment). To create a project for today's lab:

  1. Choose File>New>Project... from the application menu.
  2. In the New Project dialog choose Java Project from the Java folder and click the Next button.
  3. In the Project name text field type "lab1" and under the Project layout section choose the Use project folder as root radio button.
  4. If you do not want to have to change this setting every time you create a new project, click on the Configure default... link, in the Preferences dialog choose the Project radio button, and click the OK button.
  5. Finally click the Finish button.

Notice that in the Package Explorer panel on the left side of the Eclipse window there is now a folder that represents the project directory. If you click the disclosure triangle next to the new project it will show an icon for the JRE System Library. This is how Eclipse shows that your code is linked to the standard java library (java.util, java.lang, etc). In order to create a program in the project you need to add a java file. To do this:

  1. Choose File>New>Class from the application menu bar. This will bring up the New Java Class dialog.
  2. In the Name text field type "HelloWorld".
  3. Click on the public static void main check box and click the Finish button.

Notice in the Package Explorer panel that under the lab1 project is the file HelloWorld.java. In the Editor panel there is a tab that contains the contents of the HelloWorld.java file, including the class declaration and the main method declaration. Add a print statement to the main method to print Hello World and save the file (File>Save or ctl-s). Run the program by choosing Run>Run from the application menu bar or pressing ctl-shift-F11. Note that there is no compile step, Eclipse compiles code in the background while you are editing it. The Console panel should appear at the bottom of the application window with the output that would normally appear on the command line.

Timer Events

The file BouncingBall.java animates a ball in a window like we created in class. Download the BouncingBall program to your lab1 project directory. Note that eclipse is slightly picky about how code is added to a project. If you save a file to a project directory, it does not automatically appear in the Package Explorer. To fix this you can choose the project directory in the Package Explorer and then choose File>Refresh from the application menu bar (or press F5). You should now see the added java file in the Package Explorer and double clicking the file will open it in the Editor panel. If you use File>Open to open a file added to the project directory Eclipse will open the file in the Editor panel, but it will not let you run the program because it has not been added to the project.

Run the BouncingBall program. When the ball gets to the edge of the screen, it just keeps going. Modify the program so that when the ball hits the edge of the screen it should change direction as if it bounced off of the edge. Make sure that the ball does not move off of the screen before bouncing off of the right and bottom sides of the window.

Keyboard Events

The file KeyboardControl.java uses keyboard events to echo what characters are typed with the keyboard like we created in class. Download the file and modify it to draw a rectangle that moves based on presses of the arrow keys by doing the following:

  1. Make the preferred size of the panel larger and add code to the paintComponent method that draws the rectangle. Make sure that the location of the rectangle is determined by instance variables.
  2. 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.
  3. 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. If you have never played pong, it is a game with a ball bouncing around the screen. The player controls a paddle that the ball can bounce off of but can only move along one edge. The goal is to prevent the ball from getting past the paddle.

  1. Create a new java file, Pong.java
  2. Add the code that animates the ball from the BouncingBall program and the code for the keyboard controlled rectangle from the KeyboardControl program.
  3. Modify the program so that the rectangle is one side of the window and only allowed to move along that edge.
  4. Remove the code that causes the ball to bounce on the edge where the rectangle is located and add code that will cause the ball to bounce off of rectangle.
  5. Finally, add code that will display a message if the ball is able to get past the rectangle and off of the screen.

Submit your code on the course Inquire site.