Smoother Animation?
Author |
Message |
Token
![](http://server2.uploadit.org/files/token89-avatar1.jpg)
|
Posted: 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! |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
MysticVegeta
![](http://www.geocities.com/ohsoinsane/my_avatar.JPG)
|
Posted: 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. |
|
|
|
|
![](images/spacer.gif) |
[Gandalf]
![](http://compsci.ca/v3/uploads/user_avatars/189297994e4c716fec7f1.png)
|
Posted: 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. |
|
|
|
|
![](images/spacer.gif) |
rizzix
|
|
|
|
![](images/spacer.gif) |
Token
![](http://server2.uploadit.org/files/token89-avatar1.jpg)
|
Posted: 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
|
|
|
|
|
|
![](images/spacer.gif) |
rizzix
|
Posted: 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. |
|
|
|
|
![](images/spacer.gif) |
rizzix
|
|
|
|
![](images/spacer.gif) |
evogre3n
|
Posted: 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. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
|
|