package KinematicSimulator; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.AdjustmentEvent; import java.awt.event.AdjustmentListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JScrollBar; /** * This is the Frame that contains all the simulation controls. * * @author Steve */ class KinematicController extends JFrame implements ActionListener { KinematicEventListener m_listener = null; int gravity = 60; int friction = 3; int elasticity = 0; int radius = 20; public KinematicController() { super("Kinematic Controller"); this.getContentPane().setLayout(new GridBagLayout()); GridBagConstraints last = new GridBagConstraints(); last.gridwidth = GridBagConstraints.REMAINDER; last.weightx = 1.0; last.fill = last.BOTH; last.insets = new Insets(5, 5, 5, 5); GridBagConstraints item = new GridBagConstraints(); item.weightx = 1.0; item.insets = new Insets(5, 5, 5, 5); last.fill = last.BOTH; JScrollBar jsb; this.getContentPane().add(new JLabel("Gravity"), item); this.getContentPane().add( jsb = new JScrollBar(JScrollBar.HORIZONTAL), last); jsb.setValue(60); jsb.addAdjustmentListener(new AdjustmentListener() { public void adjustmentValueChanged(AdjustmentEvent e) { gravity = e.getValue(); updatePhysics(); } }); this.getContentPane().add(new JLabel("Friction"), item); this.getContentPane().add( jsb = new JScrollBar(JScrollBar.HORIZONTAL), last); jsb.setValue(3); jsb.addAdjustmentListener(new AdjustmentListener() { public void adjustmentValueChanged(AdjustmentEvent e) { friction = e.getValue(); updatePhysics(); } }); this.getContentPane().add(new JLabel("Elasticity"), item); this.getContentPane().add( jsb = new JScrollBar(JScrollBar.HORIZONTAL), last); jsb.setValue(0); jsb.addAdjustmentListener(new AdjustmentListener() { public void adjustmentValueChanged(AdjustmentEvent e) { elasticity = e.getValue(); updatePhysics(); } }); this.getContentPane().add(new JLabel("Radius"), item); this.getContentPane().add( jsb = new JScrollBar(JScrollBar.HORIZONTAL), last); jsb.setValue(20); jsb.addAdjustmentListener(new AdjustmentListener() { public void adjustmentValueChanged(AdjustmentEvent e) { radius = e.getValue(); m_listener.changeRadius(radius); } }); JButton button; this.getContentPane().add(button = new JButton("Clear"), last); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { m_listener.removeEntities(); } }); this.setBounds(400, 0, 300, 180); this.setVisible(true); } public void setKinematicEventListener(KinematicEventListener listener) { m_listener = listener; //set initial physics m_listener.kinematicEvent(new NewtonianPhysicsStrategy()); } protected void updatePhysics() { m_listener.kinematicEvent( new NewtonianPhysicsStrategy( gravity, elasticity, friction, radius)); } public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("Exit")) { this.dispose(); } } }