Link to home
Start Free TrialLog in
Avatar of waffe
waffeFlag for United States of America

asked on

Add listeners to vector drawn shapes

Hi,

I am currently working on a project where I draw all kinds of shapes on the screen for specific reasons. The shapes are drawn in vector form so I can get the added benefit of OPENGL. I have a shape class that handles this task; each time I need a new shape I instantiate my shape class to draw what I need. What I would now like is the ability to roll my mouse over any shape I have drawn and retrieve the shapes current color, location, etc...

How could this be done with vector drawn shapes?

Thanks,

waffe
Avatar of fffej78
fffej78

Presumably you have collection of vectors that are on the screen?

On the mouse move event do something along these lines:

// This method give a point returns the shape which contains that point
Shape findShape( Point p )
  for each Shape in my collection of shapes
    if shape.contains( p )
      return shape;
     
  return null; // we aren't over a shape

So your shape class would have an abstract method

abstract boolean contains( Point p );

And you'd have to implement that for each shapes.  Determining whether a point is within a shape is a fairly simple algorithm.
Avatar of waffe

ASKER

> Presumably you have a collection of vectors that are on the screen? – yes, this is true.

I almost get what you are saying (I don't get exactly how to implement the logic), do you have a link or some working code that in some way highlights this concept?

Thanks,

waffe
Avatar of waffe

ASKER

How can I give a shape a set of points?
ASKER CERTIFIED SOLUTION
Avatar of fffej78
fffej78

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
To give a shape a set of points, you'll need to send a message to the shape with a collection of points.  Simple as defining a method like "public void eatPoints ( Set<Point> points )" on the Shape class.  I think you only need deal with one point at a time for this example.

Avatar of waffe

ASKER

Thanks fffej78,

I am going to have to work on this abit to make it happen for I do not fully understand.

Question - what is this line saying:

 "for ( Shape shape : shapes () )"

thanks,

waffe
for ( Shape shape : shapes() ) is the Java5 syntax for going over each element in a collection.

An equivelent way of specifying things in older versions of Java is:

for ( int i = 0 ; i < shapes().size() ; ++i )
{
  Shape shape = shapes().get( i );
     if ( shape.contains( mouseEvent.getPoint() )              
       {
          // Process shape information accordingly
          System.out.println( "Shape details: " + shape.getLocation() + shape.getSize() );
       }
    }
}

Hope that helps!
SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Basically, because I'm too lazy to type it :)  And I think it is conceptually clearer without splattering temporary variables everywhere.  I do accept that if performance is an issue then it is definitely the way to go, but for explaining concepts I think the less lines of code there are the better.

Actually a method call in a new syntax makes it look more complicated ;-) you have to remember what it returns and gets replaced with.
i would recommend some kinda of shape manager
public class ShapeManager
{
    public Shape getShape(int x,int y)
    {
        //loop through the shape collection to find the shape that corresponds to the position x,y
    }

    public void addShape(Shape shape)
    {
        shapeCollection.add(shape);
    }
}

basically, the important thing is the getShape function.

depending on the fidelity of the mouse over, you can either use bounding box to check if x,y collides with the shape(low fidelity)
or calculate based on the vector shape if the x,y is within the vector shape

ideally, the Shape class should have a function that looks something like this

public bool contains(int x, int y) {
    //function returns if shape contains x,y
}
If its a manager, make it a singleton :)
Avatar of waffe

ASKER

Thanks all for your input, I will be working on this tonight with another programming and I will hopefully have a conclusion for you all instead of more questions :) - so keep an eye out.

Thanks,

waffe
Avatar of waffe

ASKER

Thanks people,

I solved the problem in a similar fashion. I already had an array that holds all the locations of each shape, and every shape has a know size; thus, with a mouse listener I can always reference what shape my mouse is over!

Thanks for the ideas and concepts,

waffe