0

As it stands, I have a map with roads on it and vehicles are able to drive back and forth from south to north and east to west (and opposite ways as well). I have used JPanels thus far to represent the vehicles. However now it's becoming a bit difficult to handle because I want to turn the vehicles at junctions and smooth lane changing etc.

So it doesn't seem like JPanel is the optimal choice for this. What I've tried so far is to use the Shape interface to draw polygons and use these as vehicles, however I'm not sure that this is the right choice.

I will eventually want to construct my own vehicle image so the solution will have to either be able to add the image as it's background or something similar and still be able to perform operations such as rotation, transformation etc.

Any guidance on this will be much appreciated.

Force444
  • 3,321
  • 9
  • 39
  • 77

1 Answers1

2

Personally, using JPanel isn't a bad choice, what you need to be able to do is extend it's capabilities to allow you to paint changes in orientation of the vehicle as it turns.

This is going to require some animation - you going to want to know how long it would take a vehicle to complete a turn so you can calculate the angel of the vehicle over time.

For this, I would be using AffineTransformation, check here for some examples.

You will also need to change the size of the component as the vehicle turns, check here for the answer.

Lane changes would be a similar (if easier) concept. The basic idea would be to have a start position and end position of the translation and move to that position over time. Again, you would need to know the amount of time it would take to complete the translation.

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Thank you for the links and the answer. I have been looking at AffineTransformation but just thought I wouldn't be able to use it with JPanels, but I can? – Force444 Jan 27 '13 at 22:08
  • 1
    If you are using custom painting to represent the vehicle then you should have any problems (ie overriding `paintComponent`) - if not, there is a trick, but getting the source code for it is tricky – MadProgrammer Jan 27 '13 at 22:14
  • (I should also mention that you've not provided any kind of code or explanation for how you are rendering your vehicles - so it makes it hard to provide you with an accurate answer ;)) – MadProgrammer Jan 27 '13 at 22:23
  • Thanks for pointing it out. I am aware of it and I generally just wanted to hear opinions/pro-cons/etc. on whether JPanel was a good option. I do see how code would have illuminated some of the issues I could be having but I'll perhaps add a new question being more specific in the future. Nevertheless, your answer did help me, so I am grateful :) – Force444 Jan 27 '13 at 22:29
  • It's certainly not a grip, but I am making a few assumptions ;) – MadProgrammer Jan 27 '13 at 22:37