NTNUJAVA Virtual Physics Laboratory
Enjoy the fun of physics with simulations!
Backup site http://enjoy.phy.ntnu.edu.tw/ntnujava/

JDK1.0.2 simulations (1996-2001) => Misc => Topic started by: Yves on February 09, 2008, 02:14:03 am



Title: How to tag onto these balls?
Post by: Yves on February 09, 2008, 02:14:03 am
Hi,

I am almost done with my project in Java. Just have a big, tiny problem. My supervisor wants to see tags on those little blue balls. How do I go about it? Any hints? This is a Brownian motion simulation.

Something like this:
http://i26.tinypic.com/2hpsux0.jpg

Cheers.


Title: Re: How to tag onto these balls?
Post by: Fu-Kwun Hwang on February 09, 2008, 10:42:54 am
It is easy with EJS, there is a text string element (you just provide text and coordinate).
However,you are writing your own code. So you need to draw a rectangle box and print a string at the same position.
You can make it as an "onmouse over" action to display those information.


Title: Re: How to tag onto these balls?
Post by: Yves on February 10, 2008, 12:52:44 am
Thanks Professor.

I need to tag on a random ball, without the mouse coming in. Does "onmouse over" work that way?


Title: Re: How to tag onto these balls?
Post by: Fu-Kwun Hwang on February 10, 2008, 09:51:02 am
Becuse you did not say which ball need to be tagged. So I suggested use mouseOver.
If you have decided which ball need to be tagged. Just draw a rectangle and a String near the ball (need to be tagged). And redraw each time you refresh the screen.


Title: Re: How to tag onto these balls?
Post by: Yves on February 10, 2008, 01:03:40 pm
The tag/label needs to follow the chosen ball during the simulation. I gather from your suggestion the tag will be static until refreshed? If my understanding is right then I guess that is not what my supervisor has in mind. Any other alternative?

Thanks for your response. Really appreciate it.


Title: Re: How to tag onto these balls?
Post by: Fu-Kwun Hwang on February 10, 2008, 02:42:20 pm
The screen for java applets usually are refreshed many times per second. Usually, I use 20 frames per second.
You just need to draw the rectangle and string (near the selected particle) to follow the chosen ball at each frame in the simulation. So the tag/label will move with the particle (it is not static).
For example: if the coordinate for the chosen particle is x,y with size dx,dy.
g.fillOval(x,y,dx,dy);
Then you can draw rectangle with
g.drawRect(x+dx/2, y-2*dy, w, h);
where w, h should be larger than  width and hight for the label to be displayed.



Title: Re: How to tag onto these balls?
Post by: Yves on February 11, 2008, 02:39:51 am
Thanks Professor. That is a suggestion I can work on.

Cheers.