import javax.swing.JPanel; import javax.swing.JComboBox; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.Color; public class GameSpeedChooser extends JPanel { // OVERVIEW: GameSpeedChooser manipulates the interface for selecting speeds // and maintains state for the current object type. // // When clicking on a spot on the grid, it will be set gamespeed to selected. // Rep: private int selectedClass; private JComboBox combolist; private String[] classes; public String[] teams; GameSpeedChooser () // EFFECT: Initializes this to a GameSpeedChooser with no speeds. { selectedClass = 1; classes = null; combolist = new JComboBox (); combolist.addActionListener (new ActionListener () { public void actionPerformed (ActionEvent e) { JComboBox cb = (JComboBox) e.getSource (); String classname = (String) cb.getSelectedItem (); setSelectedClass (classname); } } ); add (combolist); } public void addClass (String className) throws ClassNotFoundException // MODIFIES: this, the display panel // EFFECTS: If className is the name of a loadable Java class, sets the current object // to the type loaded by className. If className is not available, throws ClassNotFoundException. { //Class newClass = Class.forName (className); // throws ClassNotFoundException if not found int newClass = stringToInt(className); String [] oldclasses = classes; if (oldclasses == null) { classes = new String [1]; selectedClass = newClass; } else { classes = new String [oldclasses.length + 1]; } if (oldclasses != null) { for (int i = 0; i < oldclasses.length; i++) { classes[i] = oldclasses[i]; } } classes[classes.length - 1] = className; combolist.addItem (className); } public int getSelectedClass() // EFFECTS: Returns the currently selected class (possibly null). { return selectedClass; } public void setSelectedClass (String classname) // MODIFIES: this // EFFECTS: Sets the currently selected class to the class associated // with the button press (described by the ActionEvent). { int newClass = stringToInt (classname); selectedClass = newClass; } public int stringToInt(String c1) { //Effect: returns int value for string else 1; if(c1=="Normal") return 1; if(c1=="Fast") return 3; if(c1=="Faster") return 10; if(c1=="Fastest") return 200; return(1); } }