Yoshi
Yoshi

Reputation: 115

Animating a line being drawn from a point to a point

Hi I am having an issue solving a problem that I need to resolve to continue with a project I am doing. I want to animate a line from a corner of a JPanel to an arbitrary point on the border of the panel determined by the user inputted angle.

Alot of posts similar to this recommend the swing timer with incrementing the x2, y2 of a line but my problem is that I don't know the line that is going to be drawn because it is determined by the function I will have to write that takes into account the angle (slope).

Say it happens to be 30 degrees...I wouldn't be able to just increment x2 and y2 because i don't know what the rise and run is in pixels to animate the correct angled line....

Are there any functions available to help with this maybe in Math or anything....I am also looking into javafx.animation

I'm not looking for a solution necessarily just to be pointed in the right direction I guess thanks

Edit
From another post:
Hi I am trying to do a project which essentially shows the path of a pool ball traversing a pool table. I understand the underlying trigonometry but I have no experience with animation in Java.

Right now I have a JPanel that is functioning as the pool table. The ball will always start from a certain point.

I know how to "draw" the line using paint() but how can I animate it easily.

My hunch is to animate the ball's path (basically animate a growing line) by using a loop with a swing timer causing a delay with each pass and drawing the next pixel of the line with each pass of the loop. My problem is how can I reference these pixel values. For example if the line was horizontal or vertical you could just increment the y2 or x2 value and redraw the line with each pass but if the slope of the line is able to change I understand how to get the endpoint of the line but what about each active pixel coordinate on the line. Can I "draw" a line object and somehow reference each pixel in it's path with some type of method. I've looked a little bit into PathIterator() but I'm not convinced I'm on the right track (or I'm more concerned that I'm not on the easiest, more high level track).

Is there an easier way to do this or am I at least on the right track? I am researching on my own but I'm getting worried because it's for a software engineering project in school and the animation aspect is really holding our group back.

Upvotes: 1

Views: 774

Answers (2)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285460

My hunch is to animate the ball's path (basically animate a growing line) by using a loop with a swing timer causing a delay with each pass and drawing the next pixel of the line with each pass of the loop.

Yes, that's about right.

My problem is how can I reference these pixel values.

I wouldn't worry about pixel values. Instead separate your problem into its constituent parts --

  • Give your class an int constant to represent the time slice length. You may wish to experiment with this.
  • Have code for the physics of the ball, that will understand the ball's location for each slice of time
  • Have separate code that draws the ball at it's current location.
  • The timer will update time, and call your physics code to update the location of the ball. The physics part will set class fields that describe this location (ballX and ballY variables perhaps), and then will call repaint() so that the JPanel's paintComponent(...) method can use the ballX and ballY variables to draw the ball at the correct location.
  • Using separate classes for the logic vs the view portion of the program would be best, but perhaps not necessary at an early stage.

Upvotes: 1

Andrew Thompson
Andrew Thompson

Reputation: 168845

Once you have the trigonometry figured out, look to the Math class. The methods include the usual sin, cos, tan methods required to calculate the height of the pixel needed for the top vertex. But beware, those methods take angles in radians, so it will be necessary to convert them from degrees to radians when using them. Math.PI will be helpful for that.

Upvotes: 3

Related Questions