So I'm using JUNG, which is something new for me. I've already implemented a simple GUI, which displays vertexes and edges and through the use of Transformers I can change shapes and all that. But how would I animate an image along an edge?
If the edges were straight lines it'd be easy as I know start and end X and Y coords, but the edges can also be BentLines, CubicCurves or QuadCurves. How would I make, say, a car move along the line that has been drawn?
I've looked at the docs for the PathIterator, but to be honest, I haven't got a clue what it actually does and whether it's apropriate for what I want.
Any pointers in the right direction would be appreciated!
This, in fact, is a bit tricky.
First of all, some contortions are necessary in order to obtain the real edge shape that is painted on the screen. Fortunately, the relevant code is already contained in the
ShapePickSupport.java
class from JUNG.Then, one has to compute a point on this shape (which is therefore implicitly assumed to be a line). This involves playing around with the
PathIterator
for computing the total length, and the current position on the line.I tried to implement this (in a very basic and simple form) and encapsulate this in an
ImageAtEdgePainter
class: It receives theVisualizationViewer
for the edge shape computations, as well as the edge and the image that should be painted. It has asetImageLocation
method that accepts a value between 0.0 and 1.0, where 0.0 means that the image should be at the start of the edge, and 1.0 means that the image is at the end of the edge, respectively.Using a dummy graph and a dummy image, the result looks like this:
where the image oscillates between the end points of the edge. Here is the code, as an MCVE:
When you say that you want to move a car along the line, I can imagine that you also want to align the image of the car with the edge - that is, to rotate the image so that the car always points towards the end of the edge. This would not be sooo difficult. But if this is an issue, you should probably first have a look at other questions (like Java: Rotate image towards mouse position? ) to see whether the answers there can... "inspire" you, or ask it as a separate (not-JUNG-specific) question.