Java Certification, Programming, JavaBean and Object Oriented Reference Books
Java applet simulating flying through space

//File Name: Space.java
//Version of Java: Java 1.1
//Author: Saad Ayub

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

class Star extends Thread{
   private double x;
   private double y;
   private double prevX;
   private double prevY;
   private double headX;
   private DrawArea dA;
   private int napTime;
   private double accel;
   private int d;
   private double m;
   private static Image im;
   private static Graphics buff;

   public Star(DrawArea dA, Image im, Graphics buff){
      napTime = 40;
      this.dA = dA;
      this.im = im;
      this.buff = buff;
   }
   public double getX(){
      do{
         x = (Math.random() * 600);
      }while(x == 300.00);
      return x;
   }

   public double getY(){
      do{
         y = (Math.random() * 380);
      }while(y == 190.00);
      return y;
   }
   public double getSlope(double x, double y){
      return (double)(y - 190.00)/(double)(x - 300.00);
   }

   public void setNapTime(double x, double y){
      d = (int)Math.sqrt(((300.00 - x)*(300.00 - x)) + ((190.00 - y)*(190.00 - y)));
      napTime = (d/20)*2;

      if(d < 100){
         accel = 1;
       }
   }

   public double getHeadX(){
      double headX = 1.00;
      if(x < 300.00){
         if(d < 100 && m < 4 && m > -4)
            headX = 1.00;
         else if(d < 100 && m >= 7)
            headX = .10;
         else{
            headX = (300.00 - x)/30;
            if(headX < 1.00)
               headX = 1.00;
         }
         headX = -headX;
      }
      else if(x > 300.00){
         if(d < 100 && m < 4 && m > -4)
            headX = 1.00;
         else if(d < 100 && m <= -7)
            headX = .10;
         else{
            headX = (x - 300.00)/30;
            if(headX < 1.00)
               headX = 1.00;
         }
   }
      return headX;
   }

   public void run(){
      x = getX();
      y = getY();
      prevX = x;
      prevY = y;
      setNapTime(x, y);
      headX = getHeadX();
      m = getSlope(x, y);

      while(true){
         try{
            Thread.sleep(90);
         }
         catch(Exception e){}

         dA.repaint();

         if(accel == 1)
            headX *= 2.00;

         prevX = x;
         prevY = y;

         x += headX;
         y = 190.00-m*(300.00 - (double)x);

         if(x < 0.00 || x > 600.00 || y < 0.00 || y > 380.00){
            accel = 0;
            x = getX();
            y = getY();
            prevX = x;
            prevY = y;
            setNapTime(x, y);
            headX = getHeadX();
            m = getSlope(x, y);
         }
      }
   }
 

   public void draw(){
      buff.setColor(Color.white);
      buff.drawLine((int)x, (int)y, (int)prevX, (int)prevY);
      buff.setColor(new Color(100, 100, 100));
      if(m <= 2 && m >= -2){
         buff.drawLine((int)x, (int)y+1, (int)prevX, (int)prevY);
         buff.drawLine((int)x, (int)y-1, (int)prevX, (int)prevY);
      }
      if(m > 2 || m < -2){
         buff.drawLine((int)x+1, (int)y, (int)prevX, (int)prevY);
         buff.drawLine((int)x-1, (int)y, (int)prevX, (int)prevY);
      }
   }
   public static void load(Graphics g){
      g.drawImage(im, 0, 0, null);
   }
}

class DrawArea extends Panel{
   private int i;
   private Star sp[];
   private int j;
   private Image im;
   private Graphics buff;
   public DrawArea(Image im){
      j = 90;
      sp = new Star[j];
      this.im = im;
      buff = im.getGraphics();

      for(int i = 0; i < j; i++){
         sp[i] = new Star(this, im, buff);
         sp[i].setPriority(Thread.MAX_PRIORITY);
      }
      for(int i = 0; i < j; i++){
         sp[i].start();
      }
      setBackground(Color.black);
   }
   public void paint(Graphics g){
      buff.setColor(Color.black);
      buff.fillRect(0, 0, 600, 380);
      for(int i = 0; i < j; i++){
         sp[i].draw();
      }
      Star.load(g);
   }
   public void update(Graphics g){
      paint(g);
   }
}

public class Space extends Applet{
   private static DrawArea dA;
   public void init(){
      dA = new DrawArea(createImage(600, 380));
      setLayout(new BorderLayout());
      add(dA, BorderLayout.CENTER);
   }
   public void stop(){
      destroy();
   }
}
 

Return to : Java Programming Hints and Tips