3D Starafield
Java Source:
import java.awt.*;
import java.applet.Applet;
public class StarField extends Applet implements Runnable
{
Dimension d;
Font font = new Font("Helvetica", Font.BOLD, 36);
FontMetrics fm;
Graphics goff;
Image ii;
Thread thethread;
String s;
final int numstars=64;
int[] starx, stary, starz;
final int c=8;
public String getAppletInfo()
{
return("Starfield - Door Brian Postma");
}
public void init()
{
Graphics g;
int i;
d = size();
g=getGraphics();
g.setFont(font);
fm = g.getFontMetrics();
stary=new int[numstars];
starx=new int[numstars];
starz=new int[numstars];
for (i=0; i0 && d.height>0)
{
ii = createImage(d.width, d.height);
goff = ii.getGraphics();
}
if (goff==null || ii==null)
return;
goff.setFont(font);
goff.setColor(Color.black);
goff.fillRect(0, 0, d.width, d.height);
DrawStars();
goff.setColor(new Color(128, 192, 255));
goff.drawString(s,(d.width-fm.stringWidth(s)) / 2, d.height/2 );
g.drawImage(ii, 0, 0, this);
}
public void DrawStars()
{
int i,x1,y1,x2,y2;
goff.setColor(Color.white);
for (i=0; id.width || y2<0 || y2>d.height || starz[i]<2)
{
starx[i]=c*(int)(Math.random()*d.width-d.width/2);
stary[i]=c*(int)(Math.random()*d.height-d.height/2);
starz[i]=66;
}
else
{
goff.setColor(new Color(255-3*(int)starz[i],255-3*(int)starz[i],255-3*(int)starz[i]));
goff.drawLine(x1,y1,x2,y2);
}
}
}
public void run()
{
long starttime;
Graphics g;
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
g=getGraphics();
while(true)
{
starttime=System.currentTimeMillis();
try
{
paint(g);
starttime += 30;
Thread.sleep(Math.max(0, starttime-System.currentTimeMillis()));
// Thread.sleep(20);
}
catch (InterruptedException e)
{
break;
}
}
}
public void start()
{
if (thethread == null) {
thethread = new Thread(this);
thethread.start();
}
}
public void stop()
{
if (thethread != null) {
thethread.stop();
thethread = null;
}
}
}
|