Game Development Reference
In-Depth Information
fields representing the GUI components, the SpringSimulator class declares a SpringODE object
as one of its fields.
public class SpringSimulator extends JFrame implements ActionListener
{
private JTextField massTextField;
private JTextField muTextField;
private JTextField kTextField;
private JTextField x0TextField;
private JLabel massLabel;
private JLabel muLabel;
private JLabel kLabel;
private JLabel x0Label;
private JButton startButton;
private JButton resetButton;
private JPanel drawingPanel;
private GridBagConstraints gbc;
SpringODE spring;
When the Start button is pressed, the actionPerformed method declared in the
SpringSimulator class is called. Initial values for the mass, damping coefficient, spring constant,
and initial location are obtained from the values inside the text fields. A SpringODE object is
created based on these initial values. As with the other games created in this topic, a Timer
object is used to control the execution of the simulation. When the start method is called
on the Timer object, the simulation begins and the spring starts to move.
// The actionPerformed() method is called when
// the "Start" button is pressed.
public void actionPerformed(ActionEvent event) {
// Get the initial values from the text field
double mass = Double.parseDouble(massTextField.getText());
double mu = Double.parseDouble(muTextField.getText());
double k = Double.parseDouble(kTextField.getText());
double x0 = Double.parseDouble(x0TextField.getText());
// Create a SpringODE object
spring = new SpringODE(mass, mu, k, x0);
// Start the spring moving using a Timer object
// to slow down the action.
gameTimer.start();
}
The Timer object is set up to call the actionPerformed method declared in the GameUpdater
class (an inner class of the SpringSimulator class) every 0.05 seconds. The method is very
Search WWH ::




Custom Search