Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Smoother Animation?
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Token




PostPosted: Fri Feb 24, 2006 7:11 am   Post subject: Smoother Animation?

Hey, i'm new to Java and i was just wondering if there was some sort of a offscreenonly mode. if not, then how do i make this animation smoother?

code:

// The "Bounce" class.
import java.awt.*;
import hsa.Console;


public class Bounce
{
    static Console c;           // The output console

    public static void main (String [] args)
    {
        c = new Console (25, 90);
        int ballx, bally;
        ballx = c.getWidth () / 2;
        bally = c.getHeight () / 2;
        int [] x = new int [11];
        int [] y = new int [11];
        int [] xdir = new int [11];
        int [] ydir = new int [11];

        for (int i = 1 ; i <= 10 ; i++)
        {
            x [i] = (int) Math.rint (Math.random () * c.getWidth ());
            y [i] = (int) Math.rint (Math.random () * c.getHeight ());
            xdir [i] = (int) Math.rint (Math.random () * 8);
            ydir [i] = (int) Math.rint (Math.random () * 8);
        }


        while (1 == 1)
        {

            for (int i = 1 ; i <= 10 ; i++)
            {

                //  c.setColor (Color.white);
                // c.fillOval (x [i] - 5, y [i] - 5, 10, 10);

                x [i] += xdir [i];
                y [i] += ydir [i];

                if (x [i] >= c.getWidth () - 5 && xdir [i] > 0 || x [i] <= 5 && xdir [i] < 0)
                {
                    xdir [i] *= -1;
                }
                else if (y [i] >= c.getHeight () - 5 && ydir [i] > 0 || y [i] <= 0 + 5 && ydir [i] < 0)
                {
                    ydir [i] *= -1;
                }
                else if (Jordanclass.dist (x [i], y [i], ballx, bally) < 55)
                {
                    xdir [i] *= -1;
                    ydir [i] *= -1;
                }

                if (i == 1)
                {
                    c.clear ();
                }

                c.setColor (Color.black);
                c.fillOval (x [i] - 5, y [i] - 5, 10, 10);




            }
            c.fillOval (ballx - 50, bally - 50, 100, 100);
            Jordanclass.delay (3);



        }





        // Place your program here.  'c' is the output console
    } // main method


    static public void drawball (int x, int y, int rad, Color color)
    {
        c.setColor (color);
        c.fillOval (x - rad, y - rad, rad * 2, rad * 2);
    }


   
        static public void delay (int msec)
        {
            long enterMsec = System.currentTimeMillis ();
            long checkMsec = System.currentTimeMillis ();

            while ((checkMsec - enterMsec) < msec)
            {
                checkMsec = System.currentTimeMillis ();
            }
        }


        static public double dist (int x1, int y1, int x2, int y2)
        {
            return Math.sqrt ((Math.pow (dif (x1, x2), 2)) + (Math.pow (dif (y1, y2), 2)));
        }


        static public int dif (int x1, int x2)
        {
            return Math.max (x1 - x2, x2 - x1);
        }

       
} // Bounce class






Thanks in advance!
Sponsor
Sponsor
Sponsor
sponsor
MysticVegeta




PostPosted: Sat Feb 25, 2006 12:22 pm   Post subject: (No subject)

Why do I get an error: "Jordan Class" is either a misplaced package name or a non-existent entity.

Anyways, you dont need a def method. since when you square it the negative difference will become positive but not vice versa.
[Gandalf]




PostPosted: Sat Feb 25, 2006 2:02 pm   Post subject: (No subject)

You would get that effect using Double Buffering. Don't ask me how to actually do it though.
rizzix




PostPosted: Sat Feb 25, 2006 2:04 pm   Post subject: (No subject)

This was brought up a couple of times.. Please refer to:
http://compsci.ca/v2/viewtopic.php?t=3740
Token




PostPosted: Wed Mar 01, 2006 10:36 am   Post subject: (No subject)

Okay, I got it figured out and completely forgot about this post, but it keeps freezing every once in a while, both on my home computer and school computer. my teacher said that its java's fault, but i think that if i were to change the screen mode somehow it would work better because i read some stuff on diferent screenmodes, does that make any sence at all? and if it does how would i go about doing that?

MysticVegeta: because I created my own class to store my commonly used methods so i wouldent have to recreate them or copy them over again.

heres my new code

code:
// The "Bounce" class.
import java.awt.*;
import java.applet.Applet;
import hsa.Console;


public class Bounce
{
    static Console c;           // The output console

    public static void main (String [] args)
    {

        c = new Console (25, 90);
        int ballx, bally;
        ballx = c.getWidth () / 2;
        bally = c.getHeight () / 2;
        int [] x = new int [11];
        int [] y = new int [11];
        int [] xdir = new int [11];
        int [] ydir = new int [11];

        for (int i = 1 ; i <= 10 ; i++)
        {
            x [i] = (int) Math.rint (Math.random () * c.getWidth ());
            y [i] = (int) Math.rint (Math.random () * c.getHeight ());
            xdir [i] = (int) Math.rint (Math.random () * 8);
            ydir [i] = (int) Math.rint (Math.random () * 8);
        }


        while (1 == 1)
        {

            for (int i = 1 ; i <= 10 ; i++)
            {

                c.setColor (Color.white);
                c.fillOval (x [i] - 5, y [i] - 5, 10, 10);

                x [i] += xdir [i];
                y [i] += ydir [i];

                if (x [i] >= c.getWidth () - 5 && xdir [i] > 0 || x [i] <= 5 && xdir [i] < 0)
                {
                    xdir [i] *= -1;
                }
                else if (y [i] >= c.getHeight () - 5 && ydir [i] > 0 || y [i] <= 0 + 5 && ydir [i] < 0)
                {
                    ydir [i] *= -1;
                }
                else if (dist (x [i], y [i], ballx, bally) < 55)
                {
                    xdir [i] *= -1;
                    ydir [i] *= -1;
                }

                if (i == 1)
                {
                    //c.clear ();
                }

                c.setColor (Color.black);
                c.fillOval (x [i] - 5, y [i] - 5, 10, 10);




            }
            c.fillOval (ballx - 50, bally - 50, 100, 100);
            delay (10);



        }





        // Place your program here.  'c' is the output console
    } // main method


    /////////////////////////////////--Delay--/////////////////////////////////

    static public void delay (int msec)
    {
        long enterMsec = System.currentTimeMillis ();
        long checkMsec = System.currentTimeMillis ();

        while ((checkMsec - enterMsec) < msec)
        {
            checkMsec = System.currentTimeMillis ();
        }
    }


    /////////////////////////////////--DISTANCE--/////////////////////////////////


    static public double dist (int x1, int y1, int x2, int y2)
    {
        return Math.sqrt ((Math.pow (dif (x1, x2), 2)) + (Math.pow (dif (y1, y2), 2)));
    }


    /////////////////////////////////--DIFERENCE--/////////////////////////////////

    static public int dif (int x1, int x2)
    {
        return Math.max (x1 - x2, x2 - x1);
    }


    /////////////////////////////////--DRAWBALL--/////////////////////////////////

    static public void drawball (int x, int y, int rad, Color color)
    {
        c.setColor (color);
        c.fillOval (x - rad, y - rad, rad * 2, rad * 2);
    }



    /////////////////////////////////--RANDCOLOR--/////////////////////////////////

    public static Color randColor ()
    {

        return new Color (
                (int) Math.rint (Math.random () * 255),
                (int) Math.rint (Math.random () * 255),
                (int) Math.rint (Math.random () * 255));
    }


    /////////////////////////////////--PAUSE--/////////////////////////////////

    static public void pause ()
    {
        char dummy;
        dummy = c.getChar ();
    }
} // Bounce class


rizzix




PostPosted: Wed Mar 01, 2006 11:51 am   Post subject: (No subject)

Instead of the delay method you got there, i suggest you use Thread.sleep().

It is a static method that has the same effect.

While loops like that usually lead to 100% CPU consumption. Basically your program freezes.
rizzix




PostPosted: Wed Mar 01, 2006 12:07 pm   Post subject: (No subject)

java.net on smooth animation: http://today.java.net/pub/a/today/2006/02/23/smooth-moves-solutions.html
evogre3n




PostPosted: Wed Mar 01, 2006 6:40 pm   Post subject: (No subject)

rizzix wrote:
Instead of the delay method you got there, i suggest you use Thread.sleep().

It is a static method that has the same effect.

While loops like that usually lead to 100% CPU consumption. Basically your program freezes.


Agreed with rizzix here, but I do find your method quite interesting.
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: