0

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);
    }
}
Robin Green
  • 32,079
  • 16
  • 104
  • 187
rbharvs
  • 45
  • 6
  • 1
    For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Mar 14 '12 at 03:01
  • Just deleted a few thousand characters. I don't want to get rid of the swing interactor initializations in case that's part of the problem but I tried to shorten it nonetheless. – rbharvs Mar 14 '12 at 03:08
  • Sounds like someone somewhere is not calling repaint() properly. – Jakub Zaverka Mar 14 '12 at 03:10
  • *"Just deleted a few thousand characters.."* Did you read the link? BTW please learn how to format code correctly. You've done it two times on this thread, where the closing `}` is not in the formatted code. Please fix it, and take more care in future (check how the post formats in the preview, before sending it out to others). – Andrew Thompson Mar 14 '12 at 03:11
  • What I mean is that I edited out a lot of stuff to simplify the example. Rather then have a whole click and drag to add section of code, I just shortened it to a quick click to add. Do you mean there's information missing? – rbharvs Mar 14 '12 at 03:14
  • *"Do you mean there's information missing?"* Do you mean you tried to compile the code you posted (in a new project)? That should answer your question (except for the ACM lib. - if you have that installed in the JRE). – Andrew Thompson Mar 14 '12 at 03:16
  • I did compile the code I posted and it has the same issues my actual code has. So whatever error I'm making should be found in the code above. I just have no idea where that error is or what it could be. – rbharvs Mar 14 '12 at 03:20
  • After some testing I realized my code is correct. There's something wrong with the run configurations. Thanks anyways. – rbharvs Mar 14 '12 at 03:54

2 Answers2

0

I had the same problem with these interactors. Since the acm package is relatively old, it doeesn't work with newer JRE libraries than Java 1.5, like 1.6 or 1.7. I downloaded this version from here: http://www.filehippo.com/download_jre_32/3446/ or here: http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-javase5-419410.html (You need to have a free Oracle account for the second one) and the interactors now work perfectly fine. If you use Eclipse after the installation you go on the project: Right click -> Properties -> Java Build Path -> Libraries (tab) -> Add Library -> JRE System Library -> Next -> Installed JREs -> Add -> Standard VM -> Next -> Directory e.g. C:\Program Files (x86)\Java\jre1.5.0_10 -> Finish -> Check the jre1.5.0_10 box -> OK -> Finish -> OK

mgus
  • 808
  • 4
  • 17
  • 39
0

I had the exact same problem and had big trouble phrasing it in Google. The SOUTH and EAST position were just not working, while NORTH and WEST were fine.

Then I found the answer from mgus which led me to download the acm.jar (315 KB) file from the source (http://cs.stanford.edu/people/eroberts/jtf/acm.jar). I used the Stanford CS106A acm.jar (406 KB) which was apparently different.

Problem solved for me.