/* * stars.java * * Created on 31 December 2003, 12:06 */ import java.awt.*; import java.awt.image.*; import java.awt.color.*; import java.applet.*; /** * * @author Allen */ public class starsApplet extends Applet { public void init() { initForm(); // TODO: Add any constructor code after initForm call. } private static class myPoint { int x; int y; int z; } private final static int NUM_STARS = 600; // must be final static for use to dimension arrays ! private final static boolean SHOW_FRAME_TIME = false; // show frame time or fps private final static int SCALE = 1; // make the final drawn image bigger (or smaller!) private static myPoint[] stars = new myPoint[NUM_STARS]; private final static int SLEEP_TIME = 20; // period of thread sleep in ms private static int FRAMES_TO_CHECK = 100; // how many frames to average the frame time over private static int width = 320; private static int half_w = width / 2; private static int height = 240; private static int half_h = height / 2; private static long frameTime, timeTemp; private static int frameCount = 0; private static float fps = (float)0.0; private static BufferedImage img; private static Graphics g; /** Creates new form stars */ void initForm() { int loop; // initComponents(); // set the number of frames to do the timing over (aim for 2 second update based on sleep time) FRAMES_TO_CHECK = 2000 / SLEEP_TIME; // setTitle("Stars"); // setSize(width * SCALE, height * SCALE); setVisible(true); setBackground(Color.black); img = new BufferedImage(width, height, ColorSpace.TYPE_RGB); for(loop = 0; loop < NUM_STARS; loop++) { stars [loop] = new myPoint(); // putting the x, y coords in as double width/height may give a better spread of distant stars // but makes them look dimmer ? stars [loop].x = my_random(width); stars [loop].y = my_random(height); stars [loop].z = loop + 1; } g = this.getGraphics(); timeTemp = System.currentTimeMillis(); for(;;) { try { Thread.sleep(SLEEP_TIME); } catch (Exception e) {} repaint(); } } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ /* private void initComponents() {//GEN-BEGIN:initComponents addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent evt) { exitForm(evt); } }); pack(); }//GEN-END:initComponents */ /** Exit the Application */ private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm System.exit(0); }//GEN-LAST:event_exitForm /** * @param args the command line arguments */ public static void main(String args[]) { new stars().show(); } public void paint(Graphics g) { int greyscale_method = 2; // use to try alternate greyscale technique int loop, x, y; double z_div; int g_scale; int star_colour = 0; // bloody compiler, might be used uninitialised z_div = (double)NUM_STARS / 255; // shades of grey (255 max!) g_scale = 0x010101; if(img != null) { // draw stars in black // (translating to x,y screen coords for each, simple z division) for(loop=0; loop < NUM_STARS; loop++) { x = ((stars[loop].x << 7) / stars[loop].z) + half_w; y = ((stars[loop].y << 7) / stars[loop].z) + half_h; if((x > 0) && ( x < width) && ( y > 0) && (y < height)) img.setRGB(x, y, 0x000000); // set to black } // move stars and draw in white for(loop = 0; loop < NUM_STARS; loop++) { stars[loop].z = stars[loop].z - 2; if(stars[loop].z < 1) stars[loop].z = stars[loop].z + NUM_STARS; x = ((stars[loop].x << 7) / stars[loop].z) + half_w; y = ((stars[loop].y << 7) / stars[loop].z) + half_h; if((x > 0) && ( x < width) && ( y > 0) && (y < height)) { //two choices of greyscale technique, if(greyscale_method == 1) { // 1: from the number of stars (and therefore max Z) and divide by the number // of shades of grey star_colour = (int)(256 - (stars[loop].z / z_div)) ; } if(greyscale_method == 2) { // 2: only shade the nearest 256(ish) Z values (so only around 200 visible at // any time, but looks better because there is no distant block of stars) if(stars[loop].z > 254) { star_colour = 1; } else { star_colour = 255 - stars[loop].z; } } star_colour = star_colour * g_scale; img.setRGB(x, y, star_colour); // set to z defined shade of grey } } // draw the scaled image g.drawImage(img, 0, 0, width * SCALE, height * SCALE, 0, 0, width, height, this); g.setColor(Color.white); frameCount++; if(SHOW_FRAME_TIME) { if(frameCount > FRAMES_TO_CHECK) { frameCount = 0; frameTime = System.currentTimeMillis() - timeTemp; timeTemp = System.currentTimeMillis(); } g.drawString("Frame time: "+ (((float)frameTime / FRAMES_TO_CHECK) - SLEEP_TIME) +" ms (+ " + SLEEP_TIME + "ms)", 10, (height * SCALE) - 10); } else { if(System.currentTimeMillis() > timeTemp + 2000) { // check fps every two secs frameTime = System.currentTimeMillis() - timeTemp; fps = (float)frameCount / frameTime * 1000; frameCount = 0; timeTemp = System.currentTimeMillis(); } g.setColor(Color.white); g.drawString("FPS: "+ fps, 10, (height * SCALE) - 10); } } } public void update(Graphics g) { paint(g); } // generate a random number between -(n/2) and (n/2) (but not 0) private int my_random(int n) { int rnumber = 0; while(rnumber == 0) { rnumber = (int)((Math.random() * n) - (n / 2)); } return(rnumber); } // Variables declaration - do not modify//GEN-BEGIN:variables // End of variables declaration//GEN-END:variables }