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 7490761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:45:54+00:00 2026-05-29T15:45:54+00:00

I am making a program where one can click on the window, and a

  • 0

I am making a program where one can click on the window, and a point will be placed there. If the user clicks again, the point will be removed. Programmatically, each click will create a new instance of another class called “Element”, which contains the (X, Y) positions for a single point.

To achieve this, I am extending JPanel and implementing a MouseListener. To draw the points, I’m overriding the paint() method. Every time a user clicks, code the MouseListener’s mouseReleased() either adds to the ArrayList, or removes from it, then calls paint(), where the screen is cleared and the ArrayList is redrawn.

The problem I am having is that the points aren’t going away when clicked on. I don’t know if it’s my lack of understanding of paint(), or something to do with the ArrayList.

Here is my paint():

public void paint(Graphics g)
{
    // Clear screen
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, getWidth(), getHeight());

    // Display what elements will be drawn (for debugging)
    System.out.println("About to draw the following elements:");
    for (Element e : elements)
    {
        System.out.println("\t" + e);
    }

    // Draw Elements
    g.setColor(Color.BLACK);
    for(int i=0; i < elements.size(); i++)
    {
        g.fillOval(elements.get(i).x, elements.get(i).y, 10, 10);
    }

}

And here is the mouse click method:

public void mouseReleased(MouseEvent e)
{
    // Rounds to the nearest grid space (spacing is currently 20px)
    int roundX = (int) ((float)(Math.round(e.getX() / GRID_SPACING)) * GRID_SPACING);
    int roundY = (int) ((float)(Math.round(e.getY() / GRID_SPACING)) * GRID_SPACING);

    System.out.println("Clicked (" + roundX + ", " + roundY + ")");

    // Go through each element...
    for (int i=0; i < elements.size(); i++)
    {
        // if an element exists at the coordinates clicked,
        if (elements.get(i).getX() == roundX && elements.get(i).getY() == roundY)
        {
            // remove it from the elements list
            elements.remove(i);
            i--;
            System.out.println("\tElement exists at (" + roundX + ", " + roundY + "). Removing it.");
        }
    }

    elements.add(new Element(roundX, roundY));

    repaint();
}

The output of this is as follows:

  About to draw the following elements: (None)

Clicked (140, 100)
About to draw the following elements:
    This element's coordinates are (140, 100)

Clicked (160, 100)
About to draw the following elements:
    This element's coordinates are (140, 100)
    This element's coordinates are (160, 100)

Clicked (140, 100)
    Element exists at (140, 100). Removing it.
About to draw the following elements:
    This element's coordinates are (160, 100)
    This element's coordinates are (140, 100)
  • 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-05-29T15:45:55+00:00Added an answer on May 29, 2026 at 3:45 pm

    You should not add(new Element(roundX, roundY)) if the element has been removed in the previous loop

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am making a C++ program. One of my biggest annoyances with C++ is
I'm making a simple program in Objective-C. It has one class with a lot
I'm making a program which the user build directories (not in windows, in my
I'm making a program that fits the wizard concept ideally; the user is walked
How would a go about making a program where the user enters a string,
I am trying to learn python and am making a program that will output
I am currently making a program in which one of its functions is to
I'm making a C program that needs to use two stacks. One needs to
I'm making a relatively simple program which will also be running on a few
I want only one copy of my program in the system. How can I

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.