So vector math works in java, but for some reason random does not work inside of methods?!?!
Also it slows down and then freezes after running for about 5 minutes, is there a way to clean out the memory periodically so that this does not happen? The QB version did not have this issue, but then it was running only 200 stars and in java I have 1250 stars going.
Anyways, it works and looks beautiful, a big thanks to Norm, Junky, and Tjstretch. I was asked to upload working code, so here it is:
make a new folder somewhere and name it StarScape.
inside the StarScape folder make another new folder and name it Star.
put StarScape.java and StarScape.html in the Starscape folder.
put Star.java in the Star folder.
open command prompt and navigate to the StarScape folder.
at promt type javac StarScape.java Star/Star.java
click on StarScape.html and enjoy!!!
StarScape.html
<html> <head> <title>StarScape Applet</title> </head> <body> <h1 align=center>StarScape Applet By Jon Crawford</h1> <center> <applet name="StarScape" code="StarScape.class" width=800 height=600> </applet> </center> </body> </html>
Applet StarScape.java, change the maxStars from 1250 to whatever your want. I tweaked it and on my computer and found that 1250 is just enough stars to slow it down to the point that you can actually see the effect. 1,000,000 works too...lol.
// 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.*; public class StarScape extends JApplet { final int maxStars = 1250; String key = ""; String valid; int index; double myX; double myY; int myRadius; double myLayer; int rectX; int rectY; int rectXX; int rectYY; Star stars[] = new Star[maxStars+1];//make 100 star objects public void paint(Graphics g) { g.setColor(Color.black); g.fillRect(0,0,800,600);// make the background black for (index = 0; index < maxStars; index++) { myX = Math.random() * 200 - 99;//star starts in a small square myY = Math.random() * 200 - 99;//in the center myLayer = Math.random() * 3;//random layer stars[index] = new Star();//fill array with stars }//end for do { for (index = 0; index < maxStars; index++) { myX = stars[index].getStarX(); myY = stars[index].getStarY(); myRadius = stars[index].getStarRadius();//get data for star at index number rectX = (int)(myX) - myRadius + 399;//myX and myY are based on a 0,0 in the center grid rectY = (int)(myY) - myRadius + 299;//as well as converting to a 0,0 at top left grid rectXX = myRadius * 2;//these lines setup the boundaries of rectYY = myRadius * 2;//a rectangular star g.setColor(Color.black); g.fillOval(rectX, rectY, rectXX, rectYY);//erase old star stars[index].moveStar();//move the star stars[index].starValid(); valid = stars[index].getValid();//is the star out of bounds? if (valid == "false") {//is star out of bounds? myX = Math.random() * 200 - 99;//set new starting position myY = Math.random() * 200 - 99; myLayer = Math.random() * 3;//new random layer stars[index].resetStar(myX, myY, myLayer); } 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 }while (key == "");//should implement key listener }//end paint }//end starscape
class Star.java
// star.java // by Jon Crawford package Star; import java.util.*; 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]; public void Star(double myX, double myY, double myLayer) { starAngle = (Math.sqrt((myX * myX)+(myY * myY)));//calculate the hypotenuse starField[0] = myX;//set data to star's arrary starField[1] = myY;//this is for current position starField[2] = myX / starAngle * starSpeed;//these set the next position starField[3] = myY / starAngle * starSpeed;//this is reltive current position starField[4] = myLayer;//back, mid, or foreground layer starField[5] = starRadius;//star starting size starField[6] = starSpeed * myLayer;//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(double myX, double myY, double myLayer) {//resetStar method starAngle = (Math.sqrt((myX * myX)+(myY * myY)));//calculate new hypotenuse starField[0] = myX;//set data to star's array starField[1] = myY;//same as default constructor starField[2] = myX / starAngle * starSpeed; starField[3] = myY / starAngle * starSpeed; starField[4] = myLayer; starField[5] = starRadius; starField[6] = starSpeed * myLayer; starField[7] = starSteps; starField[8] = starAngle; }//end resetStar public void 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 }//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 public String getValid() {//get method return isValid; }//end isValid }//end class star
TODO: add a stop button to click on to stop the animation. I have not got that far in my java book....
TODO: make this as the background on my webpage, with buttons and text on top of it.