Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Add listeners to vector drawn shapes

Posted on 2006-06-17
14
419 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
Comment
Question by:waffe
  • 5
  • 5
  • 3
  • +1
14 Comments
 
LVL 4

Expert Comment

by:fffej78
ID: 16928803
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

by:waffe
ID: 16930463
> 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

by:waffe
ID: 16930587
How can I give a shape a set of points?
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 4

Accepted Solution

by:
fffej78 earned 110 total points
ID: 16930595
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

by:fffej78
ID: 16930600
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

by:waffe
ID: 16931813
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

by:fffej78
ID: 16932450
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

by:Mayank S
Mayank S earned 40 total points
ID: 16933595
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

by:fffej78
ID: 16933624
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

by:Mayank S
ID: 16934052
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

by:seet82
ID: 16941276
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

by:Mayank S
ID: 16941325
If its a manager, make it a singleton :)
0
 

Author Comment

by:waffe
ID: 16972022
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

by:waffe
ID: 17035001
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

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

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

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

Join & Ask a Question