Okay so I started my starscape again from scratch. This time I created a class of stars and defined two methods, moveStar and resetStar.
Then I created and applet that first checks to see if the stars are out-of-bounds, which they all are from start. It then calls resetStar and moveStar. Both these methods put the data into a public array. Then the applet erases the old star, puts the newPos into the holder for the oldPos, and then draws the star at the newPos. then it loops through the index, and the repeats via a do...while loop until the user closes the window or clicks back.
I messed with it until it compiled with no errors, and now I'm getting runtime dirty regions errors. I then commented old everything from the first fillRect and threw in some printlns, but even with no drawing commands I still got the dirty regions error. This is becoming a little frustrating.
the Star class:
// star.java // by Jon Crawford import java.util.*; public class Star { private int ctrl; private int starSpeed = 4; private int starRadius = 1; private int adjustedSpeed; private int adjustedRadius; private int xPos; private int yPos; private int steps; private double angle; private int starColor; private int ins; private float star[] = new float[8]; Random rand = new Random(); Star(){ starColor = 0; star[7] = 0; for (int ctrl = 0; ctrl < 8; ctrl++) { star[ctrl] = 0; }//end for }//end constructor 1 Star(int color){ starColor = color; for (ctrl = 0; ctrl < 6; ctrl++) { star[ctrl] = 0; }//end for }//end constuctor 2 public void resetStar(float index,float starField[][]) { steps = rand.nextInt(10); starColor = rand.nextInt(3); adjustedRadius = starRadius; if (starColor == 1) { adjustedSpeed = starSpeed / 4; } else if (starColor == 2) { adjustedSpeed = starSpeed / 2; } else if (starColor == 3) { adjustedSpeed = starSpeed; }//end if xPos = rand.nextInt(399) - 199; yPos = rand.nextInt(299) - 149; if (xPos == 0) { xPos = 1; } if (yPos == 0) { yPos = 1; } angle = (Math.sqrt((xPos * xPos)+(yPos * yPos))); star[0] = xPos; star[1] = yPos; star[2] = xPos; star[3] = yPos; star[4] = starColor; star[5] = adjustedRadius; star[6] = adjustedSpeed; star[7] = steps; star[8] = (float)angle; ins = (int)(index); for (ctrl = 0; ctrl < 7; ctrl++) { starField[ins][ctrl] = star[ctrl]; }//end for }//end reset public void moveStar(float index, float starField[][]) { ins = (int)(index); for (ctrl = 0; ctrl < 7; ctrl++) { star[ctrl] = starField[ins][ctrl]; }//end for star[3] = star[1] / star[8] * star[6]; star[4] = star[2] / star[8] * star[6]; star[7]++; if (star[7] / 5 == (int)(star[7] / 5) && star[5] < 10) { star[5]++; star[6]++; }//end if ins = (int)(index); for (ctrl = 0; ctrl < 7; ctrl++) { starField[ins][ctrl] = star[ctrl]; }//end for }//end moveStar }//end class star
StarScape Applet:
// starscape.java // by Jon Crawford import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.awt.geom.*; public class StarScape extends JApplet { public void paint(Graphics g){ String i = ""; int x = 0; int y = 0; int w = 799; int v = 599; int ind = 0; int maxStars = 100; int dX = 0; int dY = 0; Star field[] = new Star[maxStars]; float starField[][] = new float[maxStars][8]; g.setColor(Color.black); g.fillRect(x,y,w,v); do { for (float index = 0; index < maxStars; index++) { ind = (int)(index); if (starField[ind][1] <= 1 || starField[ind][2] <= 1 || starField[ind][1] >= 799 || starField[ind][2] >= 599) { field[ind].resetStar(index, starField); }//end if field[ind].moveStar(index, starField); dX = (int)(starField[ind][1] + 199); dY = (int)(starField[ind][2] + 149); g.setColor(Color.black); x = (int)(dX - starField[ind][5]); y = (int)(dY - starField[ind][5]); w = (int)(dX + starField[ind][5]); v = (int)(dY + starField[ind][5]); //debugger: System.out.println("\t\t " + x + "\t " + y + "\t " + w + "\t " + v); g.fillRect(x,y,w,v); if (starField[ind][4] == 1) { g.setColor(Color.white); } else if (starField[ind][4] == 2) { g.setColor(Color.lightGray); } else if (starField[ind][4] == 3) { g.setColor(Color.darkGray); }//end if starField[ind][1] = starField[ind][3]; starField[ind][2] = starField[ind][4]; dX = (int)(starField[ind][1] + 199); dY = (int)(starField[ind][2] + 149); x = (int)(dX - starField[ind][5]); y = (int)(dY - starField[ind][5]); w = (int)(dX + starField[ind][5]); v = (int)(dY + starField[ind][5]); //debugger: System.out.println("\t\t " + x + "\t " + y + "\t " + w + "\t " + v); g.fillRect(x,y,w,v); }//end for }while (i == "");//end while }//end paint }//end starscape
The runtime console output:
Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\Users\Jon>cd..\ C:\Users>cd.. C:\>cd JSource/Stars C:\JSource\Stars>AppletViewer StarScape.html Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException at StarScape.paint(StarScape.java:28) 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:28) 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\Stars>
Please help!