Results 1 to 7 of 7
- 02-13-2009, 10:26 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 4
- Rep Power
- 0
[SOLVED] Yoyo Applet. Got the yoyo coming down but can't get it to go back up.
I have to write a yoyo application for college and I got the yoyo coming down but now I need to get it to move back up and then just keep going up and down. I thought it would be easy to just reverse the progress of the coming down yoyo, but then I got stuck. I thought if I increase the index instead of decreasing it that it would go backwards but it don't. I am missing something and I am not sure what it is. Here is code, maybe someone can help.
Java Code:import javax.swing.*; import java.awt.*; import java.awt.event.*; public class YoYo extends JApplet implements Runnable { // variables for the index and sleep timer private int index = 0; private int index2 = 0; // The arrays that store the movement for the line and ball int[] verticalLine = {50,80,110,140,170,200,230,260,290}; int[] verticalBall = {40,70,100,130,160,190,220,250,280}; // variables for the array int moveLine = verticalLine[0]; int moveBall = verticalBall[0]; // variable for the starting point of the line int xLine = 50; int xBall = 40; private Thread ball; public void run() { // The yoyo sleeps for 100 miliseconds and then starts try { Thread.sleep(150); } catch(InterruptedException e) { showStatus(e.toString()); } repaint(); } public void init() { // if there is no thread if (ball == null) { // creates a new thread and calls the start method ball = new Thread(this); ball.start(); } } public void start() { // the start method for the thread ball = new Thread(this); ball.start(); } public void update(Graphics gr) { // calls the paint method to update the screen paint(gr); } public void paint(Graphics gr) { // gets the background color to draw over the line and ball gr.setColor(getBackground()); // draws over the old line gr.drawLine(xLine,50,xLine, moveLine); // fills the Oval with the background color gr.fillOval(xBall,moveLine, 20, 20); // set the color to red and draw a new line gr.setColor(Color.BLUE); gr.drawLine(xLine,40,xLine,verticalLine[index]); // set the color to orange and fill the ball gr.setColor(Color.GREEN); gr.fillOval(xBall,verticalLine[index], 20, 20); // increases the index moveLine = verticalLine[index]; ++index; // if the index is 8 reverse if(index == 8) { // index = 8; // } // gets the background color to draw over the line and ball gr.setColor(getBackground()); // draws over the old line gr.drawLine(xLine,290,xLine, moveLine); // fills the Oval with the background color gr.fillOval(xBall,moveLine, 20, 20); // set the color to red and draw a new line gr.setColor(Color.BLUE); gr.drawLine(xLine,280,xLine,verticalLine[index]); // set the color to orange and fill the ball gr.setColor(Color.GREEN); gr.fillOval(xBall,verticalLine[index], 20, 20); // decrease the index moveLine = verticalLine[index]; index--; } try { // try to have the thread sleep otherwise catch the exception Thread.sleep(150); } catch(InterruptedException e) { showStatus(e.toString()); } repaint(); } }
-
A few suggestions:
1) Don't draw directly in a JApplet, draw instead in a JPanel. Then place this JPanel into the JApplet, perhaps into its contentPane.
2) In your drawing JPanel, don't override the paint method, override the paintComponent method. The Swing graphics tutorials will explain why, and I strongly suggest that you read them.
3) Get your logic out of your paint/paintComponent method. You only have partial control on when paint / paintComponent will be called (you can suggest that it be called via repaint(), but it may ignore the suggestion, or even the app my call in extra paints if need be), but instead have the logic outside of these methods which should be soley for painting.
4) Look up and use a Swing Timer instead of a background thread. It's easy to use and is a bit cleaner here.
5) Walk through your logic on paper. Imagine you are the program and think what will happen to your moveLine variable with each iteration of the program. If you do this, you will see your logic's faults.
Best of luck.
-
cross-posted in the Sun Java fora. Please don't do this as it's considered rude on these fora.
- 02-13-2009, 11:53 PM #4
Senior Member
- Join Date
- Jul 2008
- Posts
- 125
- Rep Power
- 0
I see no way up
I haven't run your code, but I think I've
spotted a common mistake that made
when reversing an animation.
It's when you decide you can reverse
simply with the obvious test:
// if the index is 8 reverse
if(index == 8)
This will reverse the animation for 1
increment (or decrement), then the
index will not be 8, and the animation
will proceed forword again.
A nice fix:
DO NOT INCREMENT THE INDEX!
Wha..?
Set up an incrementValue.
int incrementValue = 1 ; // You can change this to effect the speed
Now you can get rid of all that reverse
animation stuff that follows the
if(index==8)
Change it to:
if(index==8) incrementValue = -incrementValue;
Then, instead of index++; or index--;
(wherever that is in your code) use:
index+=incrementValue;
The yo-yo should go down, then up, but then
it will stay up.
This would fix that problem:
Change that change I just offered to:
if(index==8 || index==0) incrementValue = -incrementValue;
Now the yo-yo will oscillate up and down.
Experimenting with the compared values
will vary the range in which the yo-yo travels.
- 02-14-2009, 02:02 AM #5
Member
- Join Date
- Feb 2009
- Posts
- 4
- Rep Power
- 0
I was not aware that it was rude to ask the same question in 2 forums on the internet. I had asked a question over there earlier and did not get a response, then I asked this question and did not get a response either. So I decided to come over here. I don't see why that is rude. They could not help me so I came here. Sorry if I offended anyone I was just looking for help with my project.
- 02-14-2009, 02:04 AM #6
Member
- Join Date
- Feb 2009
- Posts
- 4
- Rep Power
- 0
- 02-14-2009, 02:06 AM #7
Member
- Join Date
- Feb 2009
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
Please help output not coming
By majin_harish in forum New To JavaReplies: 2Last Post: 02-05-2009, 09:45 AM -
Saved files are coming back as read-only
By Manoeuvre in forum EclipseReplies: 0Last Post: 11-21-2008, 05:18 PM -
Why is the answer not coming out
By anonymous18 in forum New To JavaReplies: 4Last Post: 11-12-2008, 03:10 AM -
How can i know from which jsp request is coming?
By vishnujava in forum Java ServletReplies: 1Last Post: 08-06-2008, 01:13 PM -
MySQL Training Class coming to Columbus, OH
By pegitha in forum Reviews / AdvertisingReplies: 0Last Post: 05-07-2007, 05:07 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks