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

  • Home
  • SEARCH
  • 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 8792485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:59:06+00:00 2026-06-13T22:59:06+00:00

The purpose of the Java applet is as such: A ball is bouncing around

  • 0

The purpose of the Java applet is as such: A ball is bouncing around the screen. The size and speed of this ball can be changed via scrollbars. The user can press and drag the mouse on the screen to draw rectangles. The ball will bounce off of these rectangles as well. The bounds of these rectangles are stored in a vector. When a rectangle is clicked, it (and all other rectangles at that point) are removed from the vector (and the screen).

The problem I’m currently having is that clicking to remove an object doesn’t work. What I’m doing is getting the point of where I clicked, and stepping through each element of the vector and seeing if it contains the point, and if it does, remove it from the vector. Here’s the method.

public void mouseClicked(MouseEvent m)
{
    if (!ball.flag)
    {
      Vector<Rectangle> v;
      v = (Vector<Rectangle>)ball.r;
      Point p;
      p = new Point(m.getPoint());

      boolean done = false;
      int i = 0;

      for (Rectangle rect : v)
      {
        if(rect.contains(p))
        {
          v.removeElement(i);
          System.err.print("Element removed");
          continue;
        }
        i++;
        if(i>=v.size())
          done=true;
      } 
      ball.r = v;
    }

}
  • 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-13T22:59:07+00:00Added an answer on June 13, 2026 at 10:59 pm

    What you need is to use an Iterator. Like so:

    ListIterator<Rectangle> i = v.listIterator();
    
    while(i.hasNext())
    {
        Rectangle r = i.next();
    
        if(r.contains(p))
            i.remove();
    }
    

    This will remove elements from the list in a safe way.

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

Sidebar

Related Questions

Sorry for the awful title. The purpose of the Java applet is as such:
What is the purpose of these java.lang.annotation imports in this code? Why are they
I'm working on a small Java applet, very much new to this programming thing.
I am making a java applet that has basic chat functionality (you can send/recieve
What is the purpose of java.lang.instrument.ClassFileTransformer in real time. Is this used only during
The purpose of this query is to compare one aspect of Java and C++,
What is the purpose of annotations in Java? I have this fuzzy idea of
I am aware that the purpose of volatile variables in Java is that writes
I want to create a purpose built device capable of running a java vm
In Java, how do I convert List<?> to List<T> using a general purpose method

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.