Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Why does this not work?

  1. #1
    Member
    Join Date
    Aug 2011
    Posts
    86
    My Mood
    Lurking
    Thanks
    16
    Thanked 4 Times in 4 Posts

    Default Why does this not work?

    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!
    Last edited by Spidey1980; August 11th, 2011 at 11:07 AM. Reason: Grammatical


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Why does this not work?

    What line is StarScape:28?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Aug 2011
    Posts
    86
    My Mood
    Lurking
    Thanks
    16
    Thanked 4 Times in 4 Posts

    Default Re: Why does this not work?

    the second g.setColor(Color.black); is at line 28.

    I do get the first black rectangle (the one that sets my background color.)
    Last edited by Spidey1980; August 11th, 2011 at 11:05 AM. Reason: grammatical

  4. #4
    Member
    Join Date
    Aug 2011
    Posts
    86
    My Mood
    Lurking
    Thanks
    16
    Thanked 4 Times in 4 Posts

    Default Re: Why does this not work?

    This thread is dead this code is outdtated already

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Why does this not work?

    ...so did you solve your problem?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. MergeSort won't work
    By joshft91 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 7th, 2011, 09:44 PM
  2. How do get and set methods work??
    By merdzins in forum Object Oriented Programming
    Replies: 2
    Last Post: December 25th, 2010, 03:17 AM
  3. home work ..
    By shane1987 in forum Collections and Generics
    Replies: 8
    Last Post: November 16th, 2010, 09:45 AM
  4. My lines won't work!!!
    By The Mewzytion in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 2nd, 2010, 10:24 AM
  5. Why won't this while loop work?
    By trueblue in forum Loops & Control Statements
    Replies: 2
    Last Post: July 17th, 2009, 09:10 AM