please help: star animation
- From: andrewtitus@xxxxxxxxxx
- Date: Sun, 26 Feb 2006 20:51:29 GMT
Iam writing a program that displays my star and the star should animate and
bounce off the borders. When I try to create the rectangle to be the borders
of the applet I get an error with rectangle. Can someone please tell me
where I went wrong.
Thank You,
Andy
import java.lang.*;
import java.awt.*;
import java.applet.*;
import java.util.*;
public class StarField extends Applet implements Runnable
{
boolean initializing = true; //Is Applet in init()
boolean done = false; //Terminate loop for
star field
boolean start = false; //Start Animation
float starRadius; //Radius of star
String name ="Andrew Titus"; //Set name to Andrew
Titus
Thread starThread; //Main star thread
double star_dx, star_dy; //Star velocity
Image doublebuffer; //Offscreen buffer
Image bgImage; //Background image
Font creator; //Font to display
name
Rectangle walls; // Self explanitory.
They're all rectangles.
Point starPoint;
Point mouse;
Polygon star = new Polygon(
new int[]{0,6,24,9,15,0,-15,-9,-24,-6},
new int[]{-25,-8,-8,3,20,9,20,3,-8,-8},10);
//******************************** init()
public void init()
{
int x,y,w,h;
mouse = new Point( 0, 0 );
walls = new Rectangle(10,10,getWidth()-20,getHeight()-20);
starThread = new Thread( this );
//Create buffer to draw on
doublebuffer = createImage(getWidth(),getHeight() );
//Font to display creator, 20% of star field height
creator = new Font("Impact",Font.BOLD,(int)(getHeight()*0.20) );
//Walls inside applet bounds
x = (int)(0.01 * getWidth() );
y = (int)(0.01 * getHeight() );
// walls = new Rectangle(x, y, getWidth()-x*2, getHeight()-y*2 );
//Generate random stars
Random generator = new Random();
// walls = new Rectangle(x, y, getWidth(), getHeight() );
starPoint = new Point( getWidth() / 2, getHeight() / 2 );
} // end init()
//******************************** start()
public void start()
{
initializing = false;
starThread = new Thread( this);
starThread.start();
} // end start()
//******************************** stop()
public void stop()
{
done = true;
} // end stop()
// Java applet will call mouseDown() whenever you click.
public boolean mouseDown( Event e, int x, int y )
{
if ( start == false )
startStarField();
return true;
}
//******************************* run()
public void run()
{
while( !done )
{
testWalls();
updateStar();
repaint();
try {
Thread.sleep( 1000/60 ); // 1000ms / 60 fps
}
catch( Exception e ){}
}
} // end run();
void startStarField()
{
//Starting speed of the ball is scaled according to playfield.
double speedX = 0.006;
double speedY = 0.004;
start = true;
// re-center.
starPoint.x = getWidth() / 2;
starPoint.y = getHeight() / 2;
boolean isEven;
double speedScale;
isEven = ( (int)( Math.random() * 10) % 2 == 0 );
star_dx = speedX*getWidth() * ( isEven ? -1 : 1 );
isEven = ( (int)( Math.random() * 10) % 2 == 0 );
star_dy = speedY*getHeight() * ( isEven ? -1 : 1 );
}
void updateStar()
{
starPoint.x += star_dx;
starPoint.y += star_dy;
}
void testWalls()
{
int left = walls.x;
int top = walls.y;
int right = walls.x + walls.width;
int bottom = walls.y + walls.height;
if ( ( starPoint.x + star_dx - 25 ) <= left )
bounceStar();
if ( ( starPoint.x + star_dx + 25) >= right )
bounceStar();
if ( ( starPoint.y + star_dy - 25) <= top )
bounceStar();
if ( ( starPoint.y + star_dy + 25) >= bottom )
bounceStar();
}
void bounceStar()
{
System.out.println("OUT");
}
//Java calls update() whenever there's a change detected on display;
public void update( Graphics g )
{
paint(g);
}//end update()
//Java calls paint() whenever repaint() is called.
public void paint( Graphics g )
{
int x,y,location;
Graphics dbG = doublebuffer.getGraphics();
//Sets background
dbG.setColor( Color.RED ); // Choose color to draw with.
dbG.fillRect(0, 0, getWidth(), getHeight() );
//Draws the rectangle for applet bounds
dbG.setColor(Color.gray);
dbG.fillRect(0,0,getWidth(),getHeight());
//Sets color
dbG.setColor(Color.red);
dbG.setFont( creator );
//Sets for center of screen
x = getWidth()/2;
y = getHeight() /2;
//Draws creator name
dbG.drawString(name,x,y);
//Draws star
dbG.setColor(Color.white);
dbG.fillPolygon(star);
} // end paint()
//****************************** updateStar()
} // end STARFIELD
.
- Follow-Ups:
- Re: please help: star animation
- From: IchBin
- Re: please help: star animation
- From: IchBin
- Re: please help: star animation
- Prev by Date: Re: A simple question about istrue of Ant.
- Next by Date: Re: please help: star animation
- Previous by thread: Some help needed
- Next by thread: Re: please help: star animation
- Index(es):
Relevant Pages
|
|