Results 1 to 5 of 5
- 02-25-2012, 03:10 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 71
- Rep Power
- 0
Generating coordinates for my ball
HI folks
Ok, so I can draw myball on the JPanel no worries. Now I want it to move about the frame. I want to create a method that will give me new x and y coordinates for my ball then I can use these to redraw, but not sure how to generate the numbers. Something like this:
Which I know is wrong, but would be greatful if someone could give a hint on how to change the values of pos within the bounds of the frame dimentions?Java Code:public void move() { pos = new Point(); while(true) { if (pos.x <= getWidth()-DIAMATER) { pos.translate(CHANGE + CHANGE, CHANGE + CHANGE); System.out.println(" going up" +pos); } if (pos.x <= getHeight()-DIAMATER) { pos.translate(CHANGE - CHANGE, CHANGE - CHANGE); System.out.println("going down" + pos); } } }
Thanks in advance.
- 02-25-2012, 05:25 PM #2
Re: Generating coordinates for my ball
Doesn't that depend on how you want to ball to move?not sure how to generate the numbers
If the ball is currently at an x,y position, what are the next values of x,y that you want to be used to show the ball at the new position?
Also you need to test if the new position is within the bounds that you want it to be shown in.
- 02-25-2012, 05:31 PM #3
Member
- Join Date
- Jan 2011
- Posts
- 71
- Rep Power
- 0
Re: Generating coordinates for my ball
HI Norm
I think I got it.
So now the ball bounces around nicely. Need to get is to stop now if it hit's the east boundry.Java Code:private int posX = 200; private int posY = 200; private double offsetX; private double offsetY; private final int DIAMATER = 60; <snip> public void move() if(posX <=0 || (posX + DIAMATER)> getWidth()) { offsetX = -1*offsetX; } if(posY <=0 || (posY + DIAMATER)> getHeight()) { offsetY = -1*offsetY; } posX +=offsetX; posY +=offsetY;
Thanks
- 02-25-2012, 05:43 PM #4
Re: Generating coordinates for my ball
Test if the new x value will be less than 0 and don't use it if it is.
- 02-25-2012, 05:45 PM #5
Member
- Join Date
- Jan 2011
- Posts
- 71
- Rep Power
- 0
Similar Threads
-
Cant create more than one ball
By vettera in forum AWT / SwingReplies: 5Last Post: 09-17-2011, 05:26 PM -
Ball program
By codeStone in forum Advanced JavaReplies: 10Last Post: 02-25-2011, 01:32 PM -
Box Ball Java
By fcgb9115 in forum New To JavaReplies: 1Last Post: 04-19-2010, 02:33 AM -
[SOLVED] Bouncing ball with polar coordinates
By Ypsilon IV in forum New To JavaReplies: 4Last Post: 04-22-2009, 12:19 AM -
Problem deleting ball from bouncing ball app
By adlb1300 in forum New To JavaReplies: 2Last Post: 12-03-2007, 09:08 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks