Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8437221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:22:23+00:00 2026-06-10T07:22:23+00:00

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

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T07:22:24+00:00Added an answer on June 10, 2026 at 7:22 am

    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 {
            ...  
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A newbie question. I have the following piece of Java code: import acm.program.*; import
I'm looking for some good ideas for a simple 3d graphics program as my
Hi i've got here a simple program, but it's not working properly. When i
I am trying to make a simple line drawing program in java, I am
Following some well-known OpenGL Haskell tutorial , I've made my first HOpenGL program. Here's
I am trying to replace some graphics code in a set of fortran programs
I want to create simple drawing program; Here I my program's mousePressed and mouseDragged
This should be simple: I'm using a QGLWidget to draw some openGL graphics and
I am working on a simple GUI app that just draws some graphics on
I am trying to learn java and i am practicing with a simple program

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.