Results 1 to 7 of 7
  1. #1
    snssewell is offline Member
    Join Date
    Feb 2009
    Posts
    4
    Rep Power
    0

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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    cross-posted in the Sun Java fora. Please don't do this as it's considered rude on these fora.

  4. #4
    paul pasciak is offline Senior Member
    Join Date
    Jul 2008
    Posts
    125
    Rep Power
    0

    Default 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.

  5. #5
    snssewell is offline Member
    Join Date
    Feb 2009
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    cross-posted in the Sun Java fora. Please don't do this as it's considered rude on these fora.
    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.

  6. #6
    snssewell is offline Member
    Join Date
    Feb 2009
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by paul pasciak View Post
    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.
    You my friend are a Genius! At least to me! :)
    Took me a few minutes but it really worked! My Yoyo is going up and down just as it should! AND I learned something. Thank you so much!!!!! :D

  7. #7
    snssewell is offline Member
    Join Date
    Feb 2009
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    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.
    Thank you for the great advice! Its good to learn of proper ways early.

Similar Threads

  1. Please help output not coming
    By majin_harish in forum New To Java
    Replies: 2
    Last Post: 02-05-2009, 10:45 AM
  2. Saved files are coming back as read-only
    By Manoeuvre in forum Eclipse
    Replies: 0
    Last Post: 11-21-2008, 06:18 PM
  3. Why is the answer not coming out
    By anonymous18 in forum New To Java
    Replies: 4
    Last Post: 11-12-2008, 04:10 AM
  4. How can i know from which jsp request is coming?
    By vishnujava in forum Java Servlet
    Replies: 1
    Last Post: 08-06-2008, 02:13 PM
  5. MySQL Training Class coming to Columbus, OH
    By pegitha in forum Reviews / Advertising
    Replies: 0
    Last Post: 05-07-2007, 06:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •