Game Development Reference
In-Depth Information
The GolfGame2 class is quite similar to the GolfGame class, so rather than discuss the entire
code, we'll only focus on the differences. As always, the entire code listing can be downloaded
from the Apress website. In addition to declaring fields for the four new text fields, the GolfGame2
class also declares a DragProjectile object that will represent the golf ball.
import javax.swing.*;
import java.awt.*;
import javax.swing.border.BevelBorder;
import java.awt.event.*;
import javax.swing.Timer;
public class GolfGame2 extends JFrame implements ActionListener
{
// GUI component declarations not shown ...
// The golf ball is a DragProjectile.
private DragProjectile golfball;
As was the case with the games developed previously, when the Fire button is pressed, the
actionPerformed method declared in the GolfGame2 class is called. The method goes through a
similar process as in the original Golf Game. The values from the text fields are obtained, and
from these values a DragProjectile object representing the golf ball is created. The start
method is then called on the Timer to start the simulation.
// The actionPerformed() method is called when
// the Fire button is pressed.
public void actionPerformed(ActionEvent event) {
// Get the initial quantities from the text fields.
double vx0 = Double.parseDouble(vxTextField.getText());
double vy0 = Double.parseDouble(vyTextField.getText());
double vz0 = Double.parseDouble(vzTextField.getText());
double mass = Double.parseDouble(massTextField.getText());
double area = Double.parseDouble(areaTextField.getText());
double cd = Double.parseDouble(cdTextField.getText());
double density = Double.parseDouble(densityTextField.getText());
distanceToHole = Double.parseDouble(distanceTextField.getText());
// Create a DragProjectile object representing the golf ball.
golfball = new DragProjectile(0.0, 0.0, 0.0,
vx0, vy0, vz0, 0.0, mass, area, density, cd);
// Update the display.
updateDisplay();
// Start the box sliding using a Timer object
// to slow down the action.
gameTimer.start();
}
Search WWH ::




Custom Search