001    package swingui;
002    
003    import java.awt.BorderLayout;
004    import java.awt.GridLayout;
005    import java.awt.event.ActionEvent;
006    import java.awt.event.ActionListener;
007    
008    import javax.swing.JButton;
009    import javax.swing.JLabel;
010    import javax.swing.JPanel;
011    import javax.swing.JTextField;
012    
013    import physics3d.Vect3;
014    
015    import components.GameObject;
016    
017    /** A VelocityPanel allows the user to modify the velocity of a gameobject by directly
018     * modifying textfields. If the input is not a valid number, it is ignored. 
019     * 
020     * Strange, strange things may happen if the user sets the velocity too high or too low.
021     */
022    public class VelocityPanel extends JPanel implements ActionListener {
023            //fields
024            /** the object whose velocity we are editing **/
025            private GameObject obj;
026            
027            //components
028            private JTextField velx;
029            private JTextField vely;
030            private JTextField velz;
031            private JButton setVelButton;
032            
033            public VelocityPanel() {
034                    /////////////////
035                    //ball velocity//
036                    /////////////////
037                    
038                    //the velocity fields
039                    int columns = 3;
040                    velx = new JTextField();
041                    velx.setColumns(columns);
042                    vely = new JTextField();
043                    vely.setColumns(columns);               
044                    velz = new JTextField();
045                    velz.setColumns(columns);
046                    setVelButton = new JButton("Set Ball Velocity");
047                    setVelButton.addActionListener(this);
048                                    
049                    //layout
050                    JPanel bvPanel = new JPanel(new GridLayout(3, 2));
051                    bvPanel.add(new JLabel("x: "));
052                    bvPanel.add(velx);
053                    bvPanel.add(new JLabel("y: "));
054                    bvPanel.add(vely);
055                    bvPanel.add(new JLabel("z: "));
056                    bvPanel.add(velz); 
057                    
058                    this.setLayout(new BorderLayout());
059                    add(bvPanel, BorderLayout.CENTER);
060                    add(setVelButton, BorderLayout.SOUTH);
061            }
062            
063            /** set the object of interest **/
064            public void setObject(GameObject obj) {
065                    this.obj = obj;
066                    if (obj != null) {
067                            configureVelocity();
068                    } 
069            }
070            
071            /**
072             * @requires selected instanceof Ball
073             */
074            private void configureVelocity() {
075                    if (obj == null)
076                            throw new RuntimeException("cannot configure null obj velocity");
077                    Vect3 v = obj.getVelocity();
078                    velx.setText(String.valueOf(v.x()));
079                    vely.setText(String.valueOf(v.y()));
080                    velz.setText(String.valueOf(v.z()));
081            }
082            /** when clicked, set the velocity of the current object **/
083            public void actionPerformed(ActionEvent e) {
084                    String xs = velx.getText().trim();
085                    String ys = vely.getText().trim();
086                    String zs = velz.getText().trim();
087                    
088                    try { 
089                            double x = Double.valueOf(xs).doubleValue();
090                            double y = Double.valueOf(ys).doubleValue();
091                            double z = Double.valueOf(zs).doubleValue();
092                            obj.setVelocity(new Vect3(x, y, z));
093                    } catch (Exception ex) { return; }
094            }
095            
096            public String getName() { return "Velocity"; }
097    }