home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 12,000 to 12,999 / 12000.zip / AOLDLs / Online-Tools / Java-Applets / JAVAAPPS.lzh / JAVAAPPS / STARFIEL / STARFIEL.EXE / StarField.java < prev   
Encoding:
Java Source  |  1996-03-11  |  4.5 KB  |  230 lines

  1.  
  2.  
  3.  
  4.  
  5. import java.awt.*;
  6.  
  7.  
  8.  
  9. class Star {
  10.  
  11.     int    H, V;
  12.  
  13.     int    x, y, z;
  14.  
  15.     int    type;
  16.  
  17.  
  18.  
  19.     Star( int width, int height, int depth, int type )
  20.  
  21.         {
  22.  
  23.         this.type = type;
  24.  
  25.         H = width/2;
  26.  
  27.         V = height/2;
  28.  
  29.         x = (int)(Math.random()*width) - H;
  30.  
  31.         y = (int)(Math.random()*height) - V;
  32.  
  33.         if( (x == 0) && (y == 0 ) ) x = 10;
  34.  
  35.         z = (int)(Math.random()*depth);
  36.  
  37.         }
  38.  
  39.  
  40.  
  41.     public void Draw( Graphics g, double rot )
  42.  
  43.         {
  44.  
  45.         double    X, Y;
  46.  
  47.         int    h, v, hh, vv;
  48.  
  49.         int    d;
  50.  
  51.         z-=2;
  52.  
  53.         if( z < -63 ) z = 100;
  54.  
  55.         hh = (x*64)/(64+z);
  56.  
  57.         vv = (y*64)/(64+z);
  58.  
  59.         X = (hh*Math.cos(rot))-(vv*Math.sin(rot));
  60.  
  61.         Y = (hh*Math.sin(rot))+(vv*Math.cos(rot));
  62.  
  63.         h = (int)X+H;
  64.  
  65.         v = (int)Y+V;
  66.  
  67.         if( (h < 0) || (h > (2*H))) z = 100;
  68.  
  69.         if( (v < 0) || (v > (2*H))) z = 100;
  70.  
  71.         GrayMe(g);
  72.  
  73.         if( type == 0 )
  74.  
  75.             {
  76.  
  77.             d=(100-z)/50;
  78.  
  79.             if( d == 0 ) d = 1;
  80.  
  81.             g.fillRect( h, v, d, d );
  82.  
  83.             }
  84.  
  85.         else
  86.  
  87.             {
  88.  
  89.             d=(100-z)/20;
  90.  
  91.             g.drawLine( h-d, v, h+d, v );
  92.  
  93.             g.drawLine( h, v-d, h, v+d );
  94.  
  95.             if( z < 50 ) {
  96.  
  97.                 d/=2;
  98.  
  99.                 g.drawLine( h-d, v-d, h+d, v+d );
  100.  
  101.                 g.drawLine( h+d, v-d, h-d, v+d );
  102.  
  103.                 }
  104.  
  105.             }
  106.  
  107.         }
  108.  
  109.  
  110.  
  111.     public void GrayMe(Graphics g)
  112.  
  113.         {
  114.  
  115. /*        if( z > 75 )
  116.  
  117.             {
  118.  
  119.             g.setColor( Color.darkGray );
  120.  
  121.             }
  122.  
  123.         else*/ if ( z > 50 )
  124.  
  125.             {
  126.  
  127.             g.setColor( Color.gray );
  128.  
  129.             }
  130.  
  131.         else if ( z > 25 )
  132.  
  133.             {
  134.  
  135.             g.setColor( Color.lightGray );
  136.  
  137.             }
  138.  
  139.         else
  140.  
  141.             {
  142.  
  143.             g.setColor( Color.white );
  144.  
  145.             }
  146.  
  147.         }
  148.  
  149.     }
  150.  
  151.  
  152.  
  153. public class StarField extends java.applet.Applet implements Runnable
  154.  
  155.     {
  156.  
  157.     int        Width, Height;
  158.  
  159.     Thread        me = null;
  160.  
  161.     boolean        suspend = false;
  162.  
  163.     Image        im;
  164.  
  165.     Graphics    offscreen;
  166.  
  167.     double        rot, dx, ddx;
  168.  
  169.  
  170.  
  171.     int        speed, stars, type;
  172.  
  173.     double        defddx, max;
  174.  
  175.     Star        pol[];        /* Points of light */
  176.  
  177.     
  178.  
  179.     public void init()
  180.  
  181.         {
  182.  
  183.         rot = 0;
  184.  
  185.         dx=0;
  186.  
  187.         ddx=0;
  188.  
  189.         Width = size().width;
  190.  
  191.         Height = size().height;
  192.  
  193.  
  194.  
  195.         String    theSpeed = getParameter( "speed" );
  196.  
  197.         Show( "speed", theSpeed );
  198.  
  199.         speed = (theSpeed == null ) ? 50 : Integer.valueOf( theSpeed ).intValue();
  200.  
  201.  
  202.  
  203.         String    theStars = getParameter( "stars" );
  204.  
  205.         Show( "stars", theStars );
  206.  
  207.         stars = (theStars == null ) ? 30 : Integer.valueOf( theStars ).intValue();
  208.  
  209.  
  210.  
  211.         String theType = getParameter( "type" );
  212.  
  213.         Show( "type", theType );
  214.  
  215.         type = (theType == null ) ? 0 : Integer.valueOf( theType ).intValue();
  216.  
  217.  
  218.  
  219.         String theRot = getParameter( "spin" );
  220.  
  221.         Show( "spin", theRot );
  222.  
  223.         rot = (theRot == null) ? 0 : Double.valueOf( theRot ).doubleValue();
  224.  
  225.  
  226.  
  227.         String theMax = getParameter( "maxspin" );
  228.  
  229.         Show( "maxspin", theRot );
  230.  
  231.         max = (theMax == null) ? .1 : Double.valueOf( theMax ).doubleValue();
  232.  
  233.  
  234.  
  235.         String theddx = getParameter( "ddx" );
  236.  
  237.         Show( "ddx", theddx );
  238.  
  239.         defddx = (theddx == null) ? .005 : Double.valueOf( theddx ).doubleValue();
  240.  
  241.  
  242.  
  243.         try
  244.  
  245.             {
  246.  
  247.             im = createImage( Width, Height );
  248.  
  249.             offscreen = im.getGraphics();
  250.  
  251.             }
  252.  
  253.         catch( Exception e)
  254.  
  255.             {
  256.  
  257.             offscreen = null;
  258.  
  259.             }
  260.  
  261.         pol = new Star[stars];
  262.  
  263.         for( int i = 0; i < stars; i++ )
  264.  
  265.             pol[i] = new Star( Width, Height, 100, type );
  266.  
  267.         }
  268.  
  269.  
  270.  
  271.     public void paint( Graphics g )
  272.  
  273.         {
  274.  
  275.         if( offscreen != null )
  276.  
  277.             {
  278.  
  279.             paintMe( offscreen );
  280.  
  281.             g.drawImage( im, 0, 0, this );
  282.  
  283.             }
  284.  
  285.         else
  286.  
  287.             {
  288.  
  289.             paintMe( g );
  290.  
  291.             }
  292.  
  293.         }
  294.  
  295.  
  296.  
  297.  
  298.  
  299.     public void paintMe( Graphics g )
  300.  
  301.         {
  302.  
  303.         g.setColor( Color.black );
  304.  
  305.         g.fillRect( 0, 0, Width, Height );
  306.  
  307.         //g.setColor( Color.gray );
  308.  
  309.         for( int i = 0; i < stars; i++ )
  310.  
  311.             pol[i].Draw( g, rot );
  312.  
  313.         }
  314.  
  315.  
  316.  
  317.     public void start()
  318.  
  319.         {
  320.  
  321.         if( me == null )
  322.  
  323.             {
  324.  
  325.             me = new Thread( this );
  326.  
  327.             me.start();
  328.  
  329.             }
  330.  
  331.         }
  332.  
  333.  
  334.  
  335.     public void stop()
  336.  
  337.         {
  338.  
  339.         if( me != null )
  340.  
  341.             {
  342.  
  343.             me.stop();
  344.  
  345.             me = null;
  346.  
  347.             }
  348.  
  349.         }
  350.  
  351.  
  352.  
  353.     public void run()
  354.  
  355.         {
  356.  
  357.         while( me != null )
  358.  
  359.             {
  360.  
  361.             rot += dx;
  362.  
  363.             dx += ddx;
  364.  
  365.             if( dx > max ) ddx=-defddx;
  366.  
  367.             if( dx < -max) ddx=defddx;
  368.  
  369.             try { Thread.sleep( speed ); }
  370.  
  371.             catch (InterruptedException e){}
  372.  
  373.             repaint();
  374.  
  375.             }
  376.  
  377.         }
  378.  
  379.  
  380.  
  381.     public void update( Graphics g )
  382.  
  383.         {
  384.  
  385.         paint( g );
  386.  
  387.         }
  388.  
  389.  
  390.  
  391.     public boolean mouseDown( java.awt.Event evt, int x, int y )
  392.  
  393.         {
  394.  
  395.         ddx = (ddx == 0) ? defddx : 0;
  396.  
  397.         return true;
  398.  
  399.         }
  400.  
  401.  
  402.  
  403.     public void Toggle( )
  404.  
  405.         {
  406.  
  407.         if( me != null )
  408.  
  409.             {
  410.  
  411.             if( suspend )
  412.  
  413.                 {
  414.  
  415.                 me.resume();
  416.  
  417.                 }
  418.  
  419.             else
  420.  
  421.                 {
  422.  
  423.                 me.suspend();
  424.  
  425.                 }
  426.  
  427.             suspend = !suspend;
  428.  
  429.             }
  430.  
  431.         }
  432.  
  433.  
  434.  
  435.     public void Show( String theString, String theValue )
  436.  
  437.         {
  438.  
  439.         if( theValue == null )
  440.  
  441.             {
  442.  
  443.             System.out.println( theString + " : null");
  444.  
  445.             }
  446.  
  447.         else
  448.  
  449.             {
  450.  
  451.             System.out.println( theString + " : " + theValue );
  452.  
  453.             }
  454.  
  455.         }
  456.  
  457.     }
  458.  
  459.