Solved

Add listeners to vector drawn shapes

Posted on 2006-06-17
415 Views
Last Modified: 2010-03-31
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
0
Question by:waffe
• 5
• 5
• 3
• +1
14 Comments

LVL 4

Expert Comment

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

Author Comment

> 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

Author Comment

How can I give a shape a set of points?
0

LVL 4

Accepted Solution

fffej78 earned 110 total points
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

LVL 4

Expert Comment

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

Author Comment

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

LVL 4

Expert Comment

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

LVL 30

Assisted Solution

mayankeagle earned 40 total points
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

LVL 4

Expert Comment

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

LVL 30

Expert Comment

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

LVL 2

Expert Comment

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

LVL 30

Expert Comment

If its a manager, make it a singleton :)
0

Author Comment

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

Author Comment

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

Join & Write a Comment Already a member? Login.

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!