Change Speed in Java
Results 1 to 5 of 5

Thread: Change Speed in Java

  1. #1
    Member
    Join Date
    Oct 2003
    Posts
    81

    Change Speed in Java

    Ive got the code below and i want to change the speed of the action in the program, (in this case it draws a shape so i want the program to draw the shape faster)


    import SpikeLib.*;

    public class SpikeOctagon
    {
    public static void main (String args [] )
    {
    SpikeWindow window;
    Spike mySpike;

    mySpike = new Spike ();
    window = new SpikeWindow ();
    window.addSpike (mySpike);

    mySpike.setColour (Spike.RED, Spike.PINK);
    mySpike.moveNorth();
    mySpike.moveNorthWest();
    mySpike.moveWest();
    mySpike.moveSouthWest();
    mySpike.moveSouth();
    mySpike.moveSouthEast();
    mySpike.moveEast();
    mySpike.moveNorthEast();


    }

    }



    as im new to java i was thinking of inserting the following code into the program above

    int speed = 0
    for (int 1=0; i<5 ; i++)
    {
    speed = 50 x i ;

    }



    my query is would this little piece of code speed up the actions in the programs ? if so where in the program do i insert it ? or if im wrong what coding should i insert and where.

    cheers

  2. #2
    Senior Member
    Join Date
    May 2002
    Posts
    344
    Code:
    int speed = 0
    for (int 1=0; i<5 ; i++)
    {
    speed = 50 x i ;
    
    }
    ?? i dont get your for loop...did you mean 50*i? right now if you tried to compile that for loop in some code, you would get an error saying undefined variable x. Anyways, you should know that the * (star, astericks (sp?)) is the operator you use to multiply to stuff together. also, could you like give us somehow the SpikeLib package?
    Support your right to arm bears.


    ^^This was the first video game which i played on an old win3.1 box

  3. #3
    Elite Hacker
    Join Date
    Mar 2003
    Posts
    1,407
    Exactly my point White_Eskimo, how are we supposed to help when we don't even have the package he is importing. I have no clue what it is. Also, I don't see any way to speed up a program other than taking out a lot of code so that it is processed faster. So what you suggested about the speed variable would actually slow it down. It will run as fast as it possibly can without any for loops. If you put in extra for loops it will just slow it down.

  4. #4
    Member
    Join Date
    Oct 2001
    Posts
    59
    In Java theres a method called setdelay

    thats what you use.


    int Speed=0 ;
    for (int i =0; i < 5; i++){
    window.setDelay(50 * i);


    so by inserting the above code it changes program speed.

  5. #5
    Elite Hacker
    Join Date
    Mar 2003
    Posts
    1,407
    esi1's example is one of how to slow your program down. Basically the fewer for loops you have the faster the program runs. So you will want to only have the most necessary for loops in all your programs. The only way to speed up your program other than that is to run it on a computer with a faster cpu. I have run some of my programs at school and at home and the speed was very different with the two different cpu speeds. So I guess the answer to how to speed up a program is to get rid of unnecessary for loops and get a faster computer. for loops are good for delays but they can't speed up your program.

Posting Permissions

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