Draw pointable / pickable lines, etc, how to?

K

Kevin

(I asked in comp.lang.java.gui, but with little response after 3 days,
so I repost it here. :)

I am wondering what is the best way to this common GUI task:

Draw some rectangles, lines, pies, etc (say, in a JPanel). Make sure
these things are "pointable" and "pickable": when mouse is moved over
it, we can press mouse key to "pick up" it and drag it around, etc.


Previously in some early school homework projects, I just stored the
locations for each of them in an array. For example, for a line, I
stored its starting and ending (x, y) points. For a rectangle, I stored

its 4 corner points (x,y). In listener for mouse move (or click, drag)
in JPanel, I got the current mouse's position, and checked each item in

the above array to see if the mouse was in any of them. This worked ok
with not too many lines or rectangles. And it did not use too much
memory also since only several integers needed to be saved for each
line or rectangle, etc.


Any idea of better method? (in terms of efficiency, for example, we may

have 10000 rectangels and 50000 lines. And of course we need it for
real time when user moves the mouse around).

By the way, I think one way is to make each line / rectangle / pie etc
an object. But that sounds like not efficient if we have many of them.
Right? And also I am not sure how to do it.

Or, is there any existing toolkit / framework etc that can do this job
easily?

Thanks. :)
 
L

Larry Barowski

Kevin said:
...
have 10000 rectangels and 50000 lines. And of course we need it for
real time when user moves the mouse around).

If searching is too slow due to the large number of
objects, then the usual solution is a
pixel-to-object-index map. Each rendering operation
is duplicated to the map, using the object index as a
"color". You could perhaps do this in Java using
a BufferedImage. Technically you can't guarantee
that there will be no antialiasing (you can only
suggest it) or changes in your "colors", but in
practice this may not be a problem. If your objects
are simple enough you could do your own rendering
with reasonable speed.
By the way, I think one way is to make each line / rectangle / pie etc
an object. But that sounds like not efficient if we have many of them.
Right? And also I am not sure how to do it.

How you store the objects is a separate issue.
 
D

Damian Driscoll

Kevin said:
Previously in some early school homework projects, I just stored the
locations for each of them in an array. For example, for a line, I
stored its starting and ending (x, y) points. For a rectangle, I stored

its 4 corner points (x,y). In listener for mouse move (or click, drag)
in JPanel, I got the current mouse's position, and checked each item in

the above array to see if the mouse was in any of them. This worked ok
with not too many lines or rectangles. And it did not use too much
memory also since only several integers needed to be saved for each
line or rectangle, etc.


Any idea of better method? (in terms of efficiency, for example, we may

have 10000 rectangels and 50000 lines. And of course we need it for
real time when user moves the mouse around).

By the way, I think one way is to make each line / rectangle / pie etc
an object. But that sounds like not efficient if we have many of them.
Right? And also I am not sure how to do it.

Or, is there any existing toolkit / framework etc that can do this job
easily?

Thanks. :)

Your methods should be fine, even for large datasets. Doing a search for
"clipping algorithms" should give you plenty of information on how to
search the data efficiently. Another option would be to use something like
the spatial datatypes in MySQL and get all the hard work done for free,
leaving you with just the database handling code.
 
?

=?ISO-8859-15?Q?R=E9mi?= Bastide

See:
http://mindprod.com/jgloss/graph.html
(I asked in comp.lang.java.gui, but with little response after 3 days,
so I repost it here. :)

I am wondering what is the best way to this common GUI task:

Draw some rectangles, lines, pies, etc (say, in a JPanel). Make sure
these things are "pointable" and "pickable": when mouse is moved over
it, we can press mouse key to "pick up" it and drag it around, etc.


Previously in some early school homework projects, I just stored the
locations for each of them in an array. For example, for a line, I
stored its starting and ending (x, y) points. For a rectangle, I stored

its 4 corner points (x,y). In listener for mouse move (or click, drag)
in JPanel, I got the current mouse's position, and checked each item in

the above array to see if the mouse was in any of them. This worked ok
with not too many lines or rectangles. And it did not use too much
memory also since only several integers needed to be saved for each
line or rectangle, etc.


Any idea of better method? (in terms of efficiency, for example, we may

have 10000 rectangels and 50000 lines. And of course we need it for
real time when user moves the mouse around).

By the way, I think one way is to make each line / rectangle / pie etc
an object. But that sounds like not efficient if we have many of them.
Right? And also I am not sure how to do it.

Or, is there any existing toolkit / framework etc that can do this job
easily?

Thanks. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top