Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

JBox2D: Draw Aim Arrow  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying to create an aim arrow in my game in JBox2D. I want the arrow to be of fixed length and point at the mouse around a fixed point. I have tried drawing a label but I get flickering everytime I repaint and the line does not get deleted, multiple lines get drawn as the mouse is moved. I have also tried to create a shape which follows the mouse but can not get it rotated around the outside. Currently the shape rotates around the center which is not what I want. I have tried searching for answers but I have found nothing, I have tried to implement this for a few days and am starting to get a headache. Any help would be appreciated!

How would I go about implementing an aim arrow without flickering of the screen every time the mouse is moved?

Thank you in advance.
 
Master Rancher
Posts: 3002
105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Domingas,

welcome to the Ranch, and enjoy the stay!

Without seeing your code, it is impossible to suggest some improvements. Question: does your screen also flicker if you leave out the arrow at all? The redrawig might take a lot of time, so flickering is possible even without redrawing when the mouse has moved.

I tried to write a (probably very simplified) version of what you described, using a panel with a GradientPaint as background, an arrow as Shape and a MouseMotionListener. I get no flickering at all, but maybe I made a too simple version.

Can you show us the paintComponent-method of ypur panel (or whatever you use for redrawing)?


 
Rancher
Posts: 786
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Domingas,

As already stated it is impossible to comment on your code when you do not include it.

Here is a general rule of thumb that I use when doing graphics:

Draw your background image to your graphics context, this clears your screen; then draw your objects into the graphics context, that makes it appear that your objects can float over your background; use double buffering to keep the flicker down.
 
Domingas Pleckauskas
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the response. I have currently got a line that follows the mouse, the problem now is that I have a UserView for my game and when I create the line as a JPanel the JPanel appears on top of the UserView and the UserView is not visible. I have the code for my main Game class and the AimLine Class:



 
Piet Souris
Master Rancher
Posts: 3002
105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since some classes are missing, I am unable to test your code. But I can make some general comments:

1) In your class MyGame I see the following code:

Now, frame.add(component) is a convenient way of saying frame.getContentPane.add(component, BorderLayout.Center).
But that Center can only contain one component. Therefore, the userView gets lost. If you want to do it this way, then you simply can
add line1 to userView (if that is a Container), and then add userView to the frame.

2) But creating a transparant panel just to draw some arrow is like firing a canon to shoot a musquito. First, although it is possible, it is not so easy to do, secondly it is much easier to make an arrow shape, set the scaling, rotation and transformtion to the correct values, and draw the arrow as the last thing in the paint method.

3) Normally one overrides only the paintComponent method, but if there are other components in the panel, these components then overdraw your painting.

4) I have a version on my GitHub (click here), in the repository JavaRanch14, source has your name. It first draws an image, and then an arrow as a Shape, following the mouse.

5) Tomorrow I'll add a version with a panel full of components, to show how to get that arrow painted on top of all these components.

Well, see if this example is applicable to your code.
 
Domingas Pleckauskas
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Piet,

The solution is exactly what I was looking for. However I have no idea how to implement it into my game I have uploaded a .zip of the whole game so you or anyone who wants to help can have a good look.

https://www.dropbox.com/s/f6m0tqk32tbyqbc/MyGame.zip?dl=0

Thank you so much for all the help you've provided so far. I should have uploaded my project from the very beginning saves a lot of guess work for anyone trying to help.
 
Piet Souris
Master Rancher
Posts: 3002
105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Domingas,

got your game up and running. Nice game! But what if I run out of grenades?

What I woud like to do is to draw a parabola when the user is aiming. The parabola should have a starting tangent at the point where the grenade starts after a mouseclick, and the top of the parabola must be calculated given the angle and the speed of firing. Only a half parabola will be drawn of course, not the landing place, for that would make it a little bit easy for the user... seems ideal to get a Shape for this, containing a so called quadratic Bezier curve.

Well, that is the idea, but the implementation will not be easy, I'm afraid.

First question is where to define that Shape. Contrary to my examples is this Shape a dynamic one, since the curve will depend on the mouse position. For this, we need to know how a grenade trajectory is calculated. This is done in a library, but  that lib being a .jar, I only have the classes and not the sources. Of course, we could assume a perfect parabola, no wind conditions or such, and then calculate the necessary Bezier curve.

A lot simpler would be to use the arrow that I used, but that does not tell much about the trajectory.

Second question is where to show that half parabola. Should that be in myView? What variables would be needed to avoid drawing after there has been a firing. So we need to adapt the MouseHandler class, to incorporate a status and a mouseMoved implementation, that will take care of the curve.

Questions for now: how to determine the top of the trajectory, what state do we need to maintain, and where does the drawing of the game take place.

All in all a challenge, but I'm sure it can be done. Let mw know what you think.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!