In this lab, we will learn how to handle events in Java. The tutorials in the lab will be presented by the TA step-by-step. However, you are free to work at your own pace on the lab. The lab assignment questions will not be presented by the TA.
This tutorial will demonstrate how to draw shapes on a panel.
Create a new project called Lab10 and download the three sample files: TutorialPanel.java, GUITutorial.java, and TutorialFrame.java. Run the code to see this:
A class that implements the ActionListener interface must implement the one method actionPerformed. An ActionEvent occurs when a user clicks on or interacts with a component. In the sample code, the check box, combo box, and submit button all respond to user clicks. There is a separate class created for each one, and an instance of the class is created and added to the component. Note: If you do not add the listener class to the component, it will not respond to events! The code looks like this:
This tutorial demonstrates how to handle menu events (ActionEvent), keyboard events (KeyEvent), and mouse events (MouseEvent). To start, download the code for DrawShapesAppletEvents.java and put it into the project Lab10. Run the code. This program allows the user to draw shapes by selecting particular colors and shapes from the menu.
When a menu item is selected by the user, an ActionEvent is created. To detect menu selections, each menu item needs to have an ActionListener added to it. In the code below, all menu items share the same ActionListener class called MenuListener that has one method called actionPerformed. This method contains an if statement that detects which of the menu items was clicked and acts accordingly. The first seven lines of code (from the constructor) create a MenuListener object and adds it to each menu item. The next section of code has the classes MenuListener (to detect clicks) and and its method actionPerformed that is run when an event happens.
Keyboard events occur when the user presses a key. The class KeyAdapter has been extended to create the class MyKeyListener. This keyboard listener class has been added to the applet to detect key presses. On a key press, the method keyPressed detects if one of the arrow keys are pressed and then moves the last shape to a new location based on the arrow key pressed. The first three lines of code are from the applet constructor. They add a mouse listener, mouse motion listener, and key listener to the applet that allow it to respond to these events. Mouse motion events are handled by extending the MouseMotionAdapter class.
A mouse event occurs when the user moves the mouse or clicks one of its buttons. The class MouseAdapter has been extended to create the class MouseClickListener. This listener class has been added to the applet to detect mouse events. On a mouse click event, a new shape is drawn at the location of the click. On a mouse move, the coordinates of the mouse location are output to the console.
The asteroids game needs to respond to keyboard events to move the ship and fire bullets. We will also finish the menu.
File Name | Description | Differences from Lab 8 |
---|---|---|
Asteroids.java | Main Game Class | Download new file. New features: new draw method, moveObjects, ship movement, game timer |
AsteroidsFrame.java | Game Frame | Download new file New task: implement ship movement with arrow keys and fire bullet with space bar ; detect menu events |
Asteroid.java | Asteroid class | Download new file. New features: improved explode method |
GameObject.java | GameObject superclass | Download new file. New features: detect collisions, moving and wrapping of objects |
Bullet.java | Bullet class | Use your Lab 9 file or download new file. |
Ship.java | Ship class | Download new file. New features: exploding animation, ship acceleration, move and fire bullets, new draw method |
IMClientConnection.java | Client network code | New file. Download it. |
All tasks change only the file AsteroidsFrame.java.
For this lab assignment, each group should submit using Connect the AsteroidsFrame.java file.