need help writing small java program

Tom Scruffy

Member
Oct 31, 2006
52
0
66
Hi guys, I have a question for any of you java experts. I'm writing a small application program for Computer Science 2 that basically has two ellipses on it and has a gif picture that is supposed to circle around within the boundaries of these two ellipses. it does this by repainting the gif in new x and y coordinates each time to animate. It is a modification of a program that made the smiley face ("happyFace.gif") move back and forth on the screen triggered by a timer. and reversed direction when it got to a wall (multiplied moveX and moveY by -1). The teacher has hinted that an ellipse in a standard cartesian system with long axis a and short axis b and the origin in the middle has an x coordinates on this ellipse of x = a cos(angle) and a y coordinates of
y=b sin(angle). I've managed to draw the ellipses but I can't get the gif figure to move in the elliptical pattern.
I've spent literally ten hours on this one, anybody got any suggestions?

Here is the main class followed by the class that creates the program panel.
The last if statement of the panel doesn't do the correct thing, but the panel displays the two ellipses OK.

Code attached
 

Tom Scruffy

Member
Oct 31, 2006
52
0
66
OK, I went back to the Rebound program that this is based on and tried to rewrite this again. I've spent another six hours on this trying all kinds of different ways but still can't get this figure to move. I know the key to this is adapting this formula from the cartesian coordinate system to the java window. I also am not sure how to handle the positive and negative numbers as they were in the cartesion coordinates (if statements).

Any hints at all would help if anybody has done something similar,

Thanks
 

PorscheMaD911

Member
Feb 7, 2005
128
0
71
Hi, I haven't used Java 2D stuff for a while, but I suspect it's may be to do with the fact that you only use the angle in the constructor; it is not referenced in the paintComponent method.

What are you trying to do with the x and y vars? I did a quick trace of them, and they give off some interesting values:
> Executing: C:\Program Files\ConTEXT\ConExec.exe "java" Rebound

465
150
250
-150
250
150
250
150
250
150
250
150
250
... etc.

Using them in the paintIcon method will paint the image at a specific location, not at a particular angle. You also need to adjust that somehow.
 

Tom Scruffy

Member
Oct 31, 2006
52
0
66
The angle variable is only used to figure the new x or y point. The paintComponent just paints the ellipses and puts the image in a particular x and y location. After the timer fires an event the new points are supposed to be assigned (in the listener), the angle incremented and the new points used to increment the figure.
My problem is that I'm not getting the right coordinates somehow with my logic. If I change the program and just have ... say x and y incremented by three pixels each time, it will move, but in a straight line. I need to have it move in an elliptical orbit so as to appear to be moving around the ellipses drawn in the paintComponent method.

One thing I forgot which I had in a previous version is to make the angle return to 0 when it gets to 360 Ill add that but my values still aren't coming out right.
 

Tom Scruffy

Member
Oct 31, 2006
52
0
66
This is my latest attempt, although the figure still stays in the center of the screen and doesn't move. I'm writing this in Visual Studio 2005, does anyone know how to get a list of what my x and y values are doing like the list done above?
 

Tom Scruffy

Member
Oct 31, 2006
52
0
66
Solved!! I finally got the program to work. I was try to cast the cosine formula into an int but i was doing that right at the cosine function which wouldn't give a correct answer. I cast the whole formula after the cosine was multiplied times the size of the orbit into an int and converted the degrees into radians. I then adjusted the window using the size of the orbit as an offset (a and b) and it works good. Thanks to all that replied.

The new ReboundPanel code is attached