I have a project for class. It compiles but does not run and I just cannot understand why.
It is a not so simple starscape.
my folder set up is StarScape/Star/
I have my main applet in StarScape, and a package called star in star.
This is the command I use to compile it:
C:\JSource\StarScape>javac StarScape.java Star/Star.java
code for StarScape.java in the starscape folder:
// starscape.java // by Jon Crawford import Star.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.awt.geom.*; public class StarScape extends JApplet { final int maxStars = 100; String key = ""; int index; int myX; int myY; int myRadius; int myLayer; int rectX; int rectY; int rectXX; int rectYY; public void paint(Graphics g) { Star stars[] = new Star[maxStars];//make 100 star objects g.setColor(Color.black); g.fillRect(0,0,600,600);// make the background black do { for (index = 0; index < maxStars; index++) { stars[index].getStarX(myX); stars[index].getStarY(myY); stars[index].getStarRadius(myRadius);//get data for star at index number rectX = myX - myRadius + 399;//myX and myY are based on a 0,0 in the center grid rectY = myY - myRadius + 299;//as well as converting to a 0,0 at top left grid rectXX = myX + myRadius + 399;//these lines setup the boundaries of rectYY = myY + myRadius + 299;//a rectangular star g.setColor(Color.black); g.fillRect(rectX, rectY, rectXX, rectYY);//erase old star stars[index].moveStar();//move the star stars[index].getStarX(myX); stars[index].getStarY(myY); stars[index].getStarRadius(myRadius);//get data agian because star was moved stars[index].getStarLayer(myLayer);//layer just denotes shade of star. if (myLayer == 1) { g.setColor(Color.white); }else if (myLayer == 2) { g.setColor(Color.lightGray); }else if (myLayer == 3) { g.setColor(Color.darkGray); }else { g.setColor(Color.white);//these set color based on layer } rectX = myX - myRadius + 399; rectY = myY - myRadius + 299; rectXX = myX + myRadius + 399; rectYY = myY + myRadius + 299;//set up the new star I mean rectangle g.fillRect(rectX, rectY, rectXX, rectYY);//draw star }//end for }while (key == "");//should implement key listener }//end paint }//end starscape
Code for star.java in the star folder:
// star.java // by Jon Crawford package Star; import java.util.*; public class Star { private final int starSpeed = 4; private final int starRadius = 1; private final int starSteps = 0; private final int[] invalidValues = new int[]{ -399, -299, 399, 299 }; private int xPos; private int yPos; private int starAngle; private int starLayer; private int starField[] = new int[8]; Random rand = new Random(); Star(){ xPos = rand.nextInt(399) - 199;//star starts in a small square yPos = rand.nextInt(299) - 149;//in the center starAngle = (int)(Math.sqrt(xPos * xPos)+(yPos * yPos));// calculate the hypotenuse starLayer = rand.nextInt(3);//random layer starField[0] = xPos;//set data to star's arrary starField[1] = yPos;//this is for current position starField[2] = xPos / starAngle * starSpeed;//these set the next position starField[3] = yPos / starAngle * starSpeed; starField[4] = starLayer;//back, mid, or foreground layer starField[5] = starRadius;//star starting size starField[6] = starSpeed;//the starting speed starField[7] = starSteps;//star has taken 0 steps, every 5 steps star grows and speeds up starField[8] = starAngle;//store the hypotenuse }//end default constructor public void moveStar() {//set or mutator method if(starField[2] > invalidValues[0] && starField[3] > invalidValues[1] && starField[2] < invalidValues[1] && starField[3] < invalidValues[3]) {//if star is not to close to the edge starField[0] = starField[2];//next position becomes the curent position starField[1] = starField[3]; starField[2] = starField[0] / starField[8] * starField[6];//new next position starField[3] = starField[1] / starField[8] * starField[6]; starField[7]++;//increment steps if (starField[7] / 5 == (int)(starField[7] / 5) && starField[5] < 15) {//if star has taken 5 steps and is not larger than 30x30 starField[5]++;//increment size starField[6]++;//increment speed }//end if }else {//if star is too close to the edge xPos = rand.nextInt(399) - 199;//set new starting position yPos = rand.nextInt(299) - 149; starAngle = (int)(Math.sqrt(xPos * xPos)+(yPos * yPos));//calculate new hypotenuse starLayer = rand.nextInt(3);//new random layer starField[0] = xPos;//set data to star's array starField[1] = yPos;//same as default constructor starField[2] = xPos / starAngle * starSpeed; starField[3] = yPos / starAngle * starSpeed; starField[4] = starLayer; starField[5] = starRadius; starField[6] = starSpeed; starField[7] = starSteps; starField[8] = starAngle; }//end if }//end moveStar public int getStarX(int myX) {//get method return starField[0]; }//end getStarX public int getStarY(int myY) {//get method return starField[1]; }//end getStarY public int getStarRadius(int myRadius) {//get method return starField[5]; }//end getStarRadius public int getStarLayer(int myLayer) {//get method return starField[4]; }//end getStarLayer }//end class star
the runtime errors i get:
C:\JSource\StarScape>javac StarScape.java Star/Star.java C:\JSource\StarScape>appletviewer starscape.html Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException at StarScape.paint(StarScape.java:32) at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:796) at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:713) at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:6 93) at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(System EventQueueUtilities.java:125) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:641) at java.awt.EventQueue.access$000(EventQueue.java:84) at java.awt.EventQueue$1.run(EventQueue.java:602) at java.awt.EventQueue$1.run(EventQueue.java:600) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessCo ntrolContext.java:87) at java.awt.EventQueue.dispatchEvent(EventQueue.java:611) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre ad.java:269) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread. java:184) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre ad.java:174) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161) at java.awt.EventDispatchThread.run(EventDispatchThread.java:122) Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException at StarScape.paint(StarScape.java:32) at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:796) at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:713) at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:6 93) at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(System EventQueueUtilities.java:125) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:641) at java.awt.EventQueue.access$000(EventQueue.java:84) at java.awt.EventQueue$1.run(EventQueue.java:602) at java.awt.EventQueue$1.run(EventQueue.java:600) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessCo ntrolContext.java:87) at java.awt.EventQueue.dispatchEvent(EventQueue.java:611) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre ad.java:269) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread. java:184) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre ad.java:174) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161) at java.awt.EventDispatchThread.run(EventDispatchThread.java:122) C:\JSource\StarScape>
I know that there may be better ways to do this, but it is for class. The rules for the project:
1)create a package or class for stars. Stars should have properties like location, size, speed, angle, color.
2)at least 3 colors, basically three starfields/layers on top of one another. Should run as one starfield not three, just look like 3.
2)must have a moveStar setter method; stars must grow and accelerate toward edge of screen as part of the moveStar Method.
3)must have get methods for x and y positions, color/layer, and size
4)main applet must call the methods, and draw the starfield (painting must be done in the main applet. Must use rectangle, other shapes have not been taught yet.
5)keyListener to be learned later, this must run until user exits the window. Even if I know keyListener it is NOT to be used, Teach does not want anyone to be far ahead of anyone else.
6)can use vector math if known (I do, can only use math.sqrt since math.hypot has not been gone over in class) else can move star on the four diagonals (i.e. x-1,y-1 AND x-1,y+1 AND x+1,y+1 AND x+1,y-1)
Please help. Code as been commented for easy read.