Java Certification, Programming, JavaBean and Object Oriented Reference Books
Java applet displaying a bunch of numbers moving across the screen

//File Name: Number.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 int red;
   private int green;
   private int blue;
   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 void pickColor(){
      do{
         red = 1 + (int)(Math.random() * 256);
         green = 1 + (int)(Math.random() * 256);
         blue = 1 + (int)(Math.random() * 256);
      } while(red < 25 || green < 25 || blue < 25);
   }
   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;
   }

   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);
      pickColor();
      while(true){
         try{
            Thread.sleep(90);
         }
         catch(Exception e){}

         dA.repaint();

         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){
            x = getX();
            y = getY();
            prevX = x;
            prevY = y;
            setNapTime(x, y);
            headX = getHeadX();
            m = getSlope(x, y);
            pickColor();
         }
      }
   }
 

   public void draw(){
      buff.setColor(new Color(red, green, blue));
      buff.drawString("(" + (int)x + ", " + (int)y + ")", (int)x, (int) y);
      if(red + 5 <= 256)
         red += 5;
      if(green + 5 <= 256)
         green += 5;
      if(blue + 5 <= 256)
         blue += 5;
   }
   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 Number 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