Re: Please help: Exception in thread "Thread-3" java.lang.NullPointerException



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()
.



Relevant Pages

  • Re: Java API sound
    ... I was using mixer but port is better solution; ... private void CreateInterface ... public void actionPerformed{ ... int d = -1; ...
    (comp.lang.java.programmer)
  • Re: mehrfach unterstreichen =?ISO-8859-1?Q?m=F6glich=3F?=
    ... private void init() { ... private void fill(DefaultMutableTreeNode node, int depth) { ... public void changedUpdate{ ... return vGap; ...
    (de.comp.lang.java)
  • JFrame Resize Issues (Redux)
    ... However, when the LookAndFeel is set, Java's Window Manager ... public int OffSetX = 0; ... private Rectangle positRectangle; ... public void componentHidden{ ...
    (comp.lang.java.programmer)
  • Re: mehrfach unterstreichen =?ISO-8859-1?Q?m=F6glich=3F?=
    ... */ public class FlowDOMView extends JPanel {private DefaultTreeModel model = null; ... private void fill(DefaultMutableTreeNode node, int depth) { ... public void changedUpdate{ ... return vGap; ...
    (de.comp.lang.java)
  • Re: custom table cell focus traversal
    ... //the focus to the first textfield. ... public void makeTextBoxes{ ... int column) { ... ConnectionTableModel ctm = table.getModel; ...
    (comp.lang.java.gui)