please help: star animation



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
.



Relevant Pages

  • Re: Law of Demeter can be supported without eliminating object coupling
    ... private Point upperLeft; ... public int getLeft() ... public void setLeft ... the client of the Rectangle interface can do something like ...
    (comp.object)
  • Not sure why runs but does not display I think my thread
    ... The program compiles and runs but still not displaying star. ... walls = new Rectangle, getHeight()); ... public void start ... public boolean mouseDown(Event e, int x, int y) ...
    (comp.lang.java.help)
  • Re: No compilation error but not the required output
    ... That's why you see one big rectangle. ... > public class Histogram extends Applet ... > public void paint ... > int x = 20; ...
    (comp.lang.java.gui)
  • Re: please help: star animation
    ... I changed that but still get error with rectangle constructor. ... Polygon star = new Polygon( ... public void start ... public boolean mouseDown(Event e, int x, int y) ...
    (comp.lang.java.help)
  • Re: Help with a JPanel
    ... I also want to be able to change the width/height/x/y of the rectangle ... Do I have to call some kind of redraw command on the JPanel to have it ... public void setRectangle(int x, int y, int w, int h) { ...
    (comp.lang.java.gui)