0

I'm trying to make an section of code in mouseClicked remove any rectangles clicked on from a Vector. All the rectangles are properly stored in the vector and the point I'm checking for is valid when I run the program. Looking at the documentation, there is indeed a rectangle.contains(point) method, so i'm not sure why the following snippet is invalid. Thanks!

public void mouseClicked(MouseEvent m)
    {

      Point p = new Point(m.getPoint());
      Vector v = ball.r; //ball.r is where they are put into in another object's method
      boolean done = false;
      int i = 0;

      while (!done)
      {
        if(v.elementAt(i).contains(p))
        {
          v.removeElement(i);
          i--; //prevent i from incrementing
        }
        i++;
      }
    }

Also I did not do this in a for loop because, as far as I know, when an element is removed, the vector will "repack" and I will jump over an element of the vector. Not sure if I am right or wrong in saying/doing this.

Alex Kibler
  • 4,674
  • 9
  • 44
  • 74
  • Which `Vector`? Verify import of `java.awt.Rectangle`, which implements `java.awt.Shape`. – trashgod Nov 04 '12 at 22:28
  • What is the return type of `v.elementAt(i)`? – Code-Apprentice Nov 04 '12 at 22:28
  • Just added it at the top. Same problem. – Alex Kibler Nov 04 '12 at 22:29
  • Please post the complete error message. I believe you have left out some important information by paraphrasing. – Code-Apprentice Nov 04 '12 at 22:29
  • Make sure you set done to true at some point as it's an infinite loop as it stands. – Brian Dishaw Nov 04 '12 at 22:29
  • @Code-Guru C:\Users\Alex\Downloads\Bounce2\Bounce2.java:217: cannot find symbol symbol : method contains(java.awt.Point) location: class java.lang.Object if(v.elementAt(i).contains(p)) ^ – Alex Kibler Nov 04 '12 at 22:30
  • Unsolicited advice: use an else clause to increment, rather than decrementing to "prevent" incrementing. The former will be more understandable when you return to this code in the future. – Code-Apprentice Nov 04 '12 at 22:31
  • Are you **sure** `v.elementAt(i)` returns a Rectangle? The compiler error indicates otherwise. Note that the compiler is trying to look for the `contains()` method in `java.lang.Object`. This means that `v.elementAt(i)` is returning an Object. – Code-Apprentice Nov 04 '12 at 22:32
  • Just checked (Using a System.err.println(v.elementAt(i))), and it returns a Rectangle – Alex Kibler Nov 04 '12 at 22:35
  • 1
    Yes, the **concrete** type is a `Rectangle`, which can be determined at runtime, with a `println()` for example. However, the compiler only sees an `Object` because this is the **declared** type. – Code-Apprentice Nov 04 '12 at 22:36
  • In other words, it doesn't matter what you see at runtime. At this point, the only thing that matters is the compiler error which explicitly states that the compiler is looking for the `contains()` method in the `Object` class. – Code-Apprentice Nov 04 '12 at 22:39

3 Answers3

3

Since you have defined Vector v as raw type vector, it doesn't know the type of element inside it i.e. v.elementAt(i) and hence not able to find match the contains() method. This is the reason for the issue.

You have two options. Either use Generics while defining the Vector or type cast the element before calling contains() method.

e.g.

 Vector<Rectangle> v  = (Vector<Rectangle>)ball.r;

or

 if(((Ractangle)v.elementAt(i)).contains(p))
Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
  • 1
    I'd like to point out that using generics is the recommended solution for Java 1.5 and later. – Code-Apprentice Nov 04 '12 at 22:34
  • Thanks for the answer! It now compiles without errors, which is a major improvement. Still have other issues to work out, but I think they're unrelated. Thanks! – Alex Kibler Nov 04 '12 at 22:38
3

Java Vector can be used as generic or non-generic. And you have used as non-generic. So elementAt(int index) method returns an Object, NOT a Rectangle. The easiest way to solve this problem is to cast the returned Object to a Rectangle object. Like this,

import java.awt.Rectangle;  // Don't forget the to import

public void mouseClicked(MouseEvent m)
{

  Point p = new Point(m.getPoint());
  Vector v = ball.r;
  boolean done = false;
  int i = 0;

  while (!done)
  {
    if( ((Rectangle)(v.elementAt(i)).contains(p))   // casting
    {
      v.removeElement(i);
      i--;
    }
    i++;
  }
}

A better solution will be using generics.

import java.awt.Rectangle;

public void mouseClicked(MouseEvent m)
{

  Point p = new Point(m.getPoint());
  Vector<Rectangle> v = ball.r;   // remember, bar.r Vector must be a Vector<Rectagle> 
  boolean done = false;
  int i = 0;

  while (!done)
  {
    if( (v.elementAt(i).contains(p))   // No casting
    {
      v.removeElement(i);
      i--;
    }
    i++;
  }
}

Remember you can assign generic collections to non-generic references, but you can't assign non-generic objects to generic references. You can learn about generics here.

SajithA
  • 437
  • 1
  • 9
  • 23
2
C:\Users\Alex\Downloads\Bounce2\Bounce2.java:217: cannot find symbol symbol : method contains(java.awt.Point) location: class java.lang.Object if(v.elementAt(i).contains(p)) 

This error message indicates that the compiler is looking for the contains() method in the Object class. In otherwords, v.elementAt(i) returns an Object, not a Rectangle. To fix this, you need to change your declaration for v as follows:

Vector<Rectangle> v = ball.r;

Hope this helps!

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268