JButton, JCheckBox and similar interactors do not change visually
Asked Answered
C

2

3

Here is a simple graphics programs which adds some stars on the screen.

import acm.graphics.*;
import acm.program.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * This program creates a five-pointed star every time the
 * user clicks the mouse on the canvas.
 */

public class DrawStarMap1 extends GraphicsProgram {

    public void init() {
        /* Initializes the mouse listeners */
        addMouseListeners();

        /* The check box starts out in the "on" position */
        fillCheckBox = new JCheckBox("Filled");
        fillCheckBox.setSelected(true);
        add(fillCheckBox, SOUTH);

        /* Clears the screen with a button */
        add(new JButton("Clear"), SOUTH);
        addActionListeners();   
    }

    /* Called whenever the user clicks the mouse.*/
    public void mouseClicked(MouseEvent e) {
        GStar star = new GStar(STAR_SIZE);
        star.setFilled(fillCheckBox.isSelected());
        add (star, e.getX(), e.getY());
    }

    /* Removes all the graphical objects from the canvas */
    public void actionPerformed(ActionEvent e){
        if(e.getActionCommand().equals("Clear")) removeAll();
    }

    /* Private constants */
    private static final double STAR_SIZE = 20;

    private JCheckBox fillCheckBox;
}

And the GStar class:

import acm.graphics.*;

/** Defines a new GObject class t:hat appears as a five-pointed star.
*/
public class GStar extends GPolygon {

     /** Creates a new GStar centered at the origin with the specified
     * horizontal width.
     * @param width The width of the star
     */
 public GStar(double width) {
    double dx = width / 2;
    double dy = dx * GMath.tanDegrees(18);
    double edge = width / 2 - dy * GMath.tanDegrees(36);
    addVertex(-dx, -dy);
    int angle = 0;
    for (int i = 0; i < 5; i++) {
        addPolarEdge(edge, angle);
        addPolarEdge(edge, angle + 72);
        angle -= 72;
    }
}
}

The program works fine and uses a GStar class constructor to create a star whenever the user clicks the mouse on the canvas. But, there is one problem: "The JCheckBox and JButton never change visually!". When I press the "Clear" JButton the canvas becomes empty but the button does not seem to toggle. Similarly the program draws both filled and empty stars but the "Filled" JCheckBox remains always selected, it doesn't change. The problem becomes even bigger with the JSlider I use in other programs. The slider remains always at the initial position, even if it works in some sense: its value changes. I use Eclipse, 2011 version and the latest JRE library (v.7u6 http://www.oracle.com/technetwork/java/javase/downloads/jre7-downloads-1637588.html). I haven't found sufficient info on the Internet. What is the problem? Thank you for your help!! The acm package can be downloaded from here http://jtf.acm.org/acm.jar

Chader answered 26/8, 2012 at 13:1 Comment(2)
Please provide compilable code (SSCCE)..Madlynmadman
okay I'have added the GStar source code. you can run it. @HarmeetSinghChader
P
5

The ACM Java Task Force framework is designed "to teach Java to first-year computing students without having those students overwhelmed by its complexity." To achieve this, it intercepts all mouse and keyboard events in a way that precludes interferes with normal JApplet interaction. Note that the other examples exhibit this same behavior. This example is an alternative using the Swing API.

Addendum: Compiling under Java 1.5 seems to restore the expected functionality.

enter image description here

import acm.graphics.GMath;
import acm.graphics.GPolygon;
import acm.program.*;
import java.awt.event.*;
import javax.swing.*;

/**
* This program creates a five-pointed star every time the user clicks the mouse
* on the canvas.
*/
public class DrawStarMap extends GraphicsProgram {

    public void init() {
        addMouseListeners();
        add(new JButton("ClearN"), NORTH);
        add(new JButton("ClearW"), WEST);
        add(new JButton("ClearE"), EAST);
        add(new JButton("ClearS"), SOUTH);
        addActionListeners();
    }

    /*
    * Called whenever the user clicks the mouse.
    */
    public void mouseClicked(MouseEvent e) {
        GStar star = new GStar(STAR_SIZE);
        star.setFilled(true);
        add(star, e.getX(), e.getY());
    }

    /*
    * Removes all the graphical objects from the canvas
    */
    public void actionPerformed(ActionEvent e) {
        System.out.println(e.getActionCommand());
        if (e.getActionCommand().startsWith("Clear")) {
            removeAll();
        }
    }

    /*
    * Private constants
    */
    private static final double STAR_SIZE = 20;

    private static class GStar extends GPolygon {
        ...  
    }
}
Personate answered 26/8, 2012 at 14:16 Comment(7)
It is a pity their is no suitable tag for those ACM classes, since you can take a lot of the 'conventional wisdom' used in Swing and AWT & toss it out the window when using the ACM API.Remittance
So, what changes or imports do you suggest for this program? Should I import javax.swing library along with java.awt graphics like the example you gave me? @PersonateChader
Nevertheless, it is supposed to work with the acm package since these examples are in "The Art and Science of Java" academic book and the author uses another example with JTextField, which also doesn't work on my PC (I can't type in the box). I don't think he wants to play with the students.Chader
@KostasKonstantinidis: Java 1.5 seems to work correctly; I don't see an easy fix. I notice that an EAST button updates when the frame is resized, which reminds me of Double buffering enhancements introduced in 1.6.Personate
+1 yes it seems like that, as this acm library is too old, may not be compatible with jdk7 ...Madlynmadman
+1 Yeah, that's right I tried to use Java 1.5 and it worked for all positions. One final question, where did you download Java 1.5 from, since I faced some problems with the installation? @PersonateChader
@KostasKonstantinidis: On Mac OS X, I used Apple's installer; more here.Personate
M
2

I am figuring this out ....

add(fillCheckBox, NORTH); // SOUTH to NORTH
add(new JButton("Clear"), NORTH); // SOUTH to NORTH

how come switching the position from SOUTH to NORTH works great ..

UPDATE :
As well as EAST constraint is not properly working.
May be there is some bug with SOUTH and EAST constraints.

OUTPUT :

Madlynmadman answered 26/8, 2012 at 16:16 Comment(2)
Compiling with Java 1.5 seems to work, as shown here.Personate
Oh my God, it works for both NORTH and WEST but not for SOUTH and EAST. Thank you :) @HarmeetSinghChader

© 2022 - 2024 — McMap. All rights reserved.