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 7 of 7

Thread: New working theaded non-system-crippling Starscape

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

    Default New working theaded non-system-crippling Starscape

    Thank you everyone for all your help. Here is the final polished version:

    StarScape.java
    // starscape.java
    // by Jon Crawford
    import Star.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.geom.*;
    import java.io.*;
    import java.math.*;
    import java.lang.*;
     
    public class StarScape extends JApplet implements Runnable
    {
      final int maxStars = 500;
      String key = "";
      String valid;
      int index;
      double myX;
      double myY;
      int myRadius;
      int myLayer;
      int rectX;
      int rectY;
      int rectXX;
      int rectYY;
      private long myStarDelay = 20;
      Star stars[] = new Star[maxStars+1];//make 1000 star objects
     
      public void init()
      {
        for (index = 0; index < maxStars; index++)
        {      
          stars[index] = new Star();//fill array with stars
        }//end for 
      }//end init
     
      public void start() 
      {
        Thread t = new Thread(this);
        t.start();
      }
     
      public void run()
      {
        do 
        {
          repaint();
          try 
          {
            Thread.sleep(40);
          } 
          catch (InterruptedException e)
          {
          }
        }while (key == "");
      }
     
      public void paint(Graphics g)
      {
        g.setColor(Color.black);
        g.fillRect(0,0,800,600);// make the background black, erase all previous stars
        for (index = 0; index < maxStars; index++) 
        {
          stars[index].moveStar();//move the star
          valid = stars[index].starValid();
          if (valid == "false")
          {//is star out of bounds?
            stars[index].resetStar();
          }  
          myX = stars[index].getStarX();
          myY = stars[index].getStarY();
          myRadius = stars[index].getStarRadius();//get data agian because star was moved
          myLayer = stars[index].getStarLayer();//layer just denotes shade of star. 
          if (myLayer == 3) 
          {
            g.setColor(Color.white);
          }else if (myLayer == 2) 
          {
            g.setColor(Color.lightGray);
          }else if (myLayer == 1) 
          {
            g.setColor(Color.darkGray);
          }else 
          {
            g.setColor(Color.white);//these set color based on layer
          }
          rectX = (int)(myX) - myRadius + 399;
          rectY = (int)(myY) - myRadius + 299;
          rectXX = myRadius * 2;
          rectYY = myRadius * 2;//set up the new star I mean rectangle
          g.fillOval(rectX, rectY, rectXX, rectYY);//draw star
        }//end for
      }//end paint
     
    }//end starscape

    Star.java
    // star.java
    // by Jon Crawford
    package Star;
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.geom.*;
    import java.io.*;
    import java.math.*;
     
    public class Star {
     
      private final int starSpeed = 1;
      private final int starRadius = 1;
      private final int starSteps = 0;
      private final float[] invalidValues = new float[]{ -399f, -299f, 399f, 299f, 0f, 0f};
      private String isValid = "false";
      private double starAngle;
      private double starField[] = new double[9];
      private double starX;
      private double starY;
      private int starLayer;
     
      public Star()
      {
        starX = Math.random() * 200 - 99;//star starts in a small square
        starY = Math.random() * 200 - 99;//in the center
        starLayer = (int)(Math.random() * 3 + 1);//random layer
        starAngle = (Math.sqrt((starX * starX)+(starY * starY)));//calculate the hypotenuse
        starField[0] = starX;//set data to star's arrary
        starField[1] = starY;//this is for current position
        starField[2] = starX / starAngle * starSpeed;//these set the next position
        starField[3] = starY / starAngle * starSpeed;//this is reltive current position
        starField[4] = starLayer;//back, mid, or foreground layer
        starField[5] = starRadius;//star starting size
        starField[6] = starSpeed * starLayer;//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 constructor
     
      public void moveStar() 
      {//mutator method
        System.out.println("") ;    
        starField[0] = starField[0] + starField[2];//next position becomes the curent position 
        starField[1] = 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] = starField[7] + 1;//increment steps
        if (starField[7] / 5 == (int)(starField[7] / 5) && starField[5] < 20)
        {//if star has taken 5 steps
          starField[5] = starField[5] + 1;//increment size
          starField[6] = starField[6] + 1;//increment speed
        }//end if      
      }//end moveStar
     
      public void resetStar()
      {//resetStar method
        starX = Math.random() * 200 - 99;//set new starting position
        starY = Math.random() * 200 - 99;
        starLayer = (int)(Math.random() * 3 + 1);//new random layer
        starAngle = (Math.sqrt((starX * starX)+(starY * starY)));//calculate new hypotenuse
        starField[0] = starX;//set data to star's array
        starField[1] = starY;//same as default constructor
        starField[2] = starX / starAngle * starSpeed;
        starField[3] = starY / starAngle * starSpeed;
        starField[4] = starLayer;
        starField[5] = starRadius;
        starField[6] = starSpeed * starLayer;
        starField[7] = starSteps;
        starField[8] = starAngle;
      }//end resetStar
     
      public String starValid()
      {//isValid method
        if(starField[0] > invalidValues[0] && starField[1] > invalidValues[1] && starField[0] < 
     
    invalidValues[2] && starField[1] < invalidValues[3])
        {//in bounds?
          isValid = "true";
        }else
        {//out of bounds
          isValid = "false";
        }//end if
        return isValid;
      }//end is valid
     
      public int getStarX() 
      {//get method
        return (int)(Math.round(starField[0]));
      }//end getStarX
     
      public int getStarY() 
      {//get method
        return (int)(Math.round(starField[1]));
      }//end getStarY
     
      public int getStarRadius() 
      {//get method
        return (int)(Math.round(starField[5]));
      }//end getStarRadius
     
      public int getStarLayer() 
      {//get method
        return (int)(Math.round(starField[4]));
      }//end getStarLayer
     
    }//end class star


    edit: It has now been running for an hour with no problems.
    Last edited by Spidey1980; August 20th, 2011 at 10:08 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: New working theaded non-system-crippling Starscape

    Small comments.
    Several of your variables are defined at the class level but are only used at in one method. They could be defined local to that method.
     if (valid == "false")
    Use the equals() method when comparing Strings.
    However this variable should be a boolean, not a String.


    Should the starting shape be a square or a circle?

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

    Default Re: New working theaded non-system-crippling Starscape

    LOL I don't think you can make a circle out of a 2x2 square of pixels. If you Talking about the fillRect in my code, that's the background. Easier than erasing each star, and visually there is no difference to painting the whole canvas black each time, or erasing each star individually. On older systems that ran QB, It did look better to erase each star individually.

    Sure, I could polish it some more but I'm done with that. I just thought, If I can convert that from QB to java learning java on my own (not paying some college my hard earned cash), then I can make anything with java.

    Java is good, it makes me learn good technique and style. Have self-taught myself programming, I had no style whatsoever in my QB programs.
    Last edited by Spidey1980; August 20th, 2011 at 10:52 PM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: New working theaded non-system-crippling Starscape

    LOL I don't think you can make a circle out of a 2x2 square
    This is the square I was talking about:
        starX = Math.random() * 200 - 99;//star starts in a small square
        starY = Math.random() * 200 - 99;//in the center

    Change this logic to chose the x,y combinations that are in a circle vs a small square.

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

    Default Re: New working theaded non-system-crippling Starscape

    What I'm talking about there is that I do not want a star to start close to the edge to immediately move out of range. So that the Stars actually have a little more time on screen, I start them near the center. The above line of code starts the star randomly somewhere inside a box with a size -99,-99 to 99,99. However the full grid is -399,-299 to 399,299. When drawing a star this is converted to a 0,0 to 800,600 grid.

    The screen and virtual grid are both rectangular in shape. The Star it self is round(oval).

    __________
    |         |
    |   __    |
    |  |__|   |
    |         | 
    |_________|

    As the above awful text art shows, the square in the center is where I start my stars. The stars move out from there toward the edge of the big square, then they start again at a new random location inside the center square. I would love the center starting area to be a circle, but the math for that would be more complicated then the vector math I use to define the direction each star moves.
    Last edited by Spidey1980; August 21st, 2011 at 09:44 PM.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: New working theaded non-system-crippling Starscape

    I would love the center starting area to be a circle
    There is a very simple way to filter out the random numbers that are not in a circle.
    Try this:
              do {    // get in a circle <<<<<<<<<<<<<<<<<
                myX = Math.random() * 200 - 99;//set new starting position 
                myY = Math.random() * 200 - 99;
              } while(Math.sqrt(myX*myX + myY*myY) > 99); // reject if outside

  7. The Following User Says Thank You to Norm For This Useful Post:

    Spidey1980 (August 22nd, 2011)

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

    Default Re: New working theaded non-system-crippling Starscape

    Thats great! I love it. You took the vector math even further. WOW.

    So basically use the logarithm to find the distance x,y is from 0,0 and reject anything that is further than 99 pixels away from 0,0.
    Last edited by Spidey1980; August 22nd, 2011 at 09:54 AM.

Similar Threads

  1. using if statement on string System.getProperty("os.name") not working
    By sauyon in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 29th, 2011, 12:09 PM
  2. what is System,out,println in System.out.println()?
    By koteshk in forum Java Theory & Questions
    Replies: 2
    Last Post: April 18th, 2011, 12:28 AM
  3. System.CurrentTimeMilliseconds
    By Dave in forum Java SE APIs
    Replies: 1
    Last Post: August 26th, 2009, 11:02 AM
  4. Replies: 4
    Last Post: January 27th, 2009, 12:03 AM