Re: Please help: Exception in thread "Thread-3" java.lang.NullPointerException
- From: andrewtitus@xxxxxxxxxx
- Date: Mon, 27 Feb 2006 10:21:46 GMT
I got rid of that error message but still no animation. Not sure what Iam
doing wrong. Please point me in the right direction.
Thank You,
Andy
import javax.swing.JApplet;
import java.awt.Color;
import java.awt.Event;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.util.Random;
public class StarField extends JApplet 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;
mouse = new Point(0,0);
//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.08) );
//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 );
} // end init()
//******************************** start()
public void start()
{ starThread = new Thread(this);
bgImage = getImage(getCodeBase(),"background");
initializing = false;
//I added this and got rid of null exception error
if (starThread == null) {
starThread = new Thread(this, "Clock");
starThread.start();
}
} // end start()
//******************************** stop()
public void stop()
{
done = true;
// starThread.stop();
} // 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()
//******************************** testStarField()
void startStarField()
{
start = true;
//Starting speed of the ball is scaled according to playfield.
double speedX = 0.006;
double speedY = 0.004;
// 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 );
}//end testStarField()
void updateStar()
{
starPoint.x += star_dx;
starPoint.y += star_dy;
}
//******************************** testWalls()
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();
}//end testWalls()
void bounceStar()
{
System.out.println("OUT");
}
//******************************** update()
public void update(Graphics g)
{
paint(g);
}
public void paint(Graphics g)
{
int x,y;
if(initializing)
{
g.drawString("Inatialzing...",getWidth()-15,getHeight()/2);
return;
}
Graphics dbG = doublebuffer.getGraphics();
//Sets background
dbG.setColor( Color.RED ); // Choose color to draw with.
dbG.fillRect(0, 0, getWidth(), getHeight() );
//Draw background image centered
dbG.drawImage(bgImage,0,0,getWidth(),getHeight(),
0,0,bgImage.getWidth(this),
bgImage.getHeight(this),this);
//Draws the rectangle for applet bounds two gray and one white
dbG.setColor( Color.BLACK );
dbG.drawRect( walls.x-1, walls.y-1, walls.width+2, walls.height+2 );
dbG.drawRect( walls.x+1, walls.y+1, walls.width-2, walls.height-2 );
dbG.setColor( Color.WHITE );
dbG.drawRect( walls.x, walls.y, walls.width, walls.height );
//Sets color and font
dbG.setColor(Color.black);
dbG.setFont( creator );
//Sets for center of screen
x = getWidth()/2;
y = getHeight() /2;
//Draws creator name
dbG.drawString(name,x,y);
//Sets for center of screen
x = getWidth()/2;
y = getHeight() /2;
//Draws star
dbG.setColor(Color.white);
dbG.fillPolygon(star);
// Copy doublebuffer to screen.
g.drawImage(doublebuffer, 0, 0, this );
dbG.dispose();
}// end paint()
} //end starfield()
.
- Follow-Ups:
- References:
- Please help: Exception in thread "Thread-3" java.lang.NullPointerException
- From: andrewtitus
- Re: Please help: Exception in thread "Thread-3" java.lang.NullPointerException
- From: Bart Cremers
- Please help: Exception in thread "Thread-3" java.lang.NullPointerException
- Prev by Date: Automate authentication of a web-site through HTTPUrlConnection
- Next by Date: Re: Not sure why runs but does not display I think my thread
- Previous by thread: Re: Please help: Exception in thread "Thread-3" java.lang.NullPointerException
- Next by thread: Re: Please help: Exception in thread "Thread-3" java.lang.NullPointerException
- Index(es):
Relevant Pages
|
|