I'm using a few interactors in the following class. All of them function perfectly i.e. they do what I want. However, they don't work visually. For example, when I press the JButton, which is intended to clear the screen it does so, but the JButton doesn't press down. This is fine for the JButton, but for the Radio Buttons it poses a problem. When I click to toggle the radio buttons I can toggle what's going on on-screen, but the selection of the Radio Button doesn't toggle. Why is this happening? I've stared at the code for hours and just can't figure it out. (I hope my description was clear...here's the code...). Documentation for the acm libraries can be found here.
package forces;
import java.util.*;
import javax.swing.*;
import acm.graphics.*;
import java.awt.event.*;
public class InteractiveClosedSystem extends ClosedSystem {
private GOval newMass;
private JRadioButton positiveRB;
private JRadioButton negativeRB;
public void init() {
masses = new ArrayList<Mass>();
add(new JButton("Clear"), SOUTH);
initRadioButtons();
addActionListeners();
addMouseListeners();
addKeyListeners();
}
private void initRadioButtons() {
// Radio button group for charge
ButtonGroup chargeBG = new ButtonGroup();
positiveRB = new JRadioButton("Positive");
negativeRB = new JRadioButton("Negative");
// Add all radio buttons to button group
chargeBG.add(positiveRB);
chargeBG.add(negativeRB);
// Set initial radio button selection
positiveRB.setSelected(true);
// Add all radio buttons to control bar
add(new JLabel(" Charge"), SOUTH);
add(positiveRB, SOUTH);
add(negativeRB, SOUTH);
}
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if (cmd.equals("Clear")) {
masses.removeAll(masses);
removeAll();
}
}
public void mouseClicked(MouseEvent e) {
newMass = new GOval(e.getX(), e.getY(), 30, 30);
newMass.setFilled(true);
if (positiveRB.isSelected()) {
newMass.setColor(Mass.POSITIVE_COLOR);
} else {
newMass.setColor(Mass.NEGATIVE_COLOR);
}
add(newMass);
}
}