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
waffeAsked:
Who is Participating?
 
fffej78Commented:
This is called the observer pattern (see http://en.wikipedia.org/wiki/Observer_pattern for an abstract description, and http://www.dofactory.com/patterns/PatternObserver.aspx for some .NET [C#, VB.NET examples]).

Let's say you are drawing inside a Swing frame.  You can add an event listener for the MouseMove event.  The shapes are "observers" of the mouse move event (i.e. they get a chance to react).

You could formulate the code more like this


import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JPanel;

public class JPanel
      implements MouseMotionListener
{
  private List<Shape> _shapes;

  public List<Shape> shapes ()
  {
    return _shapes;
  }

  public void mouseMoved( final MouseEvent mouseEvent )
  {  
    for ( Shape shape : shapes () )
    {
       if ( shape.contains( mouseEvent.getPoint() )                  
       {
          // Process shape information accordingly
          System.out.println( "Shape details: " + shape.getLocation() + shape.getSize() );
       }
    }
   }      
}

abstract class Shape
{
  // Implementations just determine whether the given point is contained within the shape
  public boolean contains( Point point );
 
  // Some other property
  public Point getLocation();
}

Hopefully that makes more sense, and should be easy enough for you to plugin to your implementation.  

BTW, I didn't mean collection of vectors, I meant a collection of shapes :)  I think you knew what I was trying to say though (hopefully!)
0
 
fffej78Commented:
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.
0
 
waffeAuthor Commented:
> 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
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
waffeAuthor Commented:
How can I give a shape a set of points?
0
 
fffej78Commented:
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.

0
 
waffeAuthor Commented:
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
0
 
fffej78Commented:
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!
0
 
Mayank SAssociate Director - Product EngineeringCommented:
http://java.sun.com/j2se/1.5.0/docs/relnotes/features.html

Instead of calling shapes () everytime, why not hold a reference to it:

List <Shape> list = shapes () ;

for ( Shape shape : list )
{
}
0
 
fffej78Commented:
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.

0
 
Mayank SAssociate Director - Product EngineeringCommented:
Actually a method call in a new syntax makes it look more complicated ;-) you have to remember what it returns and gets replaced with.
0
 
seet82Commented:
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
}
0
 
Mayank SAssociate Director - Product EngineeringCommented:
If its a manager, make it a singleton :)
0
 
waffeAuthor Commented:
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
0
 
waffeAuthor Commented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.