[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.
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();
}
}