A nice little star field

I initally wrote a star field program around 5 years ago in c++ and after I learned a bit of java I thought of re-writting it in java.

It's starts the animation as soon as its clicked and stops when it loses focus.

Can anyone tell me if this can be fine tuned to work better?

Initially I kept getting IllegalMonitorStateException and so I decided to make every method synchronized. ;)

Could someone explain why the applet works when the run() method has all its statements in a synchronized block?

There's only one thread in this program, so shouldnt it work properly without the synchronized keyword?

Ok here's the code.....

import java.awt.*;

import java.awt.event.*;

import java.applet.Applet;

/*

<applet code=Stars.class width=300 height=100>

</applet>

*/

publicclass Starsextends Appletimplements Runnable,FocusListener

{

int index=0;

finalint STARS=300;

finalint WIDTH=300;

finalint HEIGHT=100;

int x[]=newint[STARS];

int y[]=newint [STARS];

boolean runOnce=true;//Added this flag so make sure that the star co-ordinates are generated only once.

boolean focussed=false;//Added this flag to suspend the thread.

Thread t;

publicvoid init()

{

index=0;

setBackground(Color.black);

setForeground(Color.white);

if(runOnce)

{

for(int i=0;i<STARS;i++)

{

x[i]=(int)(Math.random()*WIDTH);

y[i]=(int)(Math.random()*HEIGHT);

}

}

runOnce=false;

addFocusListener(this);

}

synchronizedpublicvoid start()

{

if(t==null ||!t.isAlive())

{

t=new Thread(this,"Stars_in_sky");

t.start();

}

}

synchronizedpublicvoid stop()

{

focussed=false;

notify();

}

publicvoid destroy()

{

t=null;

}

synchronizedpublicvoid focusGained(FocusEvent fe)

{

focussed=true;

notify();

}

synchronizedpublicvoid focusLost(FocusEvent fe)

{

focussed=false;

notify();

}

publicvoid run()

{

while(true)

{

synchronized(this)

{

while(!focussed)

{

try

{

wait();

}catch(InterruptedException e){}

}

if(index==WIDTH)index=0;

index++;

repaint();

try

{

t.sleep(50);

}catch(InterruptedException e){}

}

}

}

publicvoid paint(Graphics g)

{

for(int i=0;i<STARS;i++)

{

int xx=x[i]-index;

if(xx>0)//this check is written so that the stars that leave the screen at the left shows up

g.drawString(".",xx,y[i]);//at the right corner and continues to move left and the whole process repeats.

else

g.drawString(".",xx+WIDTH,y[i]);

}

}

}

Message was edited by:

ArcherKing

Sorry had to add one more line

[6735 byte] By [ArcherKinga] at [2007-11-27 4:17:56]
# 1
@Op. You are using wait and notify and they require that you own the lock/monitor, that's why you need to synchronize.Kaj
kajbja at 2007-7-12 9:24:37 > top of Java-index,Java Essentials,New To Java...
# 2
Thanks Kaj for the reply.I also have another doubt. The animation isnt that smooth(it kinda flickers). Is there any method to make it appear smooth?Is there any book that is available that teach methods to improve the animation?ThanksArcher
ArcherKinga at 2007-7-12 9:24:37 > top of Java-index,Java Essentials,New To Java...
# 3
I don't do applets, so my only advice is to switch to swing. AWT is old stuff.
hunter9000a at 2007-7-12 9:24:37 > top of Java-index,Java Essentials,New To Java...
# 4
Even if I switch to swing, its not gonna take care of the flicker problem
ArcherKinga at 2007-7-12 9:24:37 > top of Java-index,Java Essentials,New To Java...
# 5

> Even if I switch to swing, its not gonna take care of

> the flicker problem

You should google on java and double buffering.

Double buffering is what you want to do. You want to draw your stars into a offscreen buffer, and then copy the whole buffer to the screen when you are done.

Kaj

kajbja at 2007-7-12 9:24:37 > top of Java-index,Java Essentials,New To Java...
# 6
Swing double-buffers by default. You could also use javax.swing.Timer, since there is no need to define your own thread. And to avoid graphics problems like shearing, go full-screen: http://java.sun.com/docs/books/tutorial/extra/fullscreen/index.html
Hippolytea at 2007-7-12 9:24:37 > top of Java-index,Java Essentials,New To Java...
# 7
Thanks guys, I'll try what you've suggested Kaj and Hippolyte.@Hippolyte I dont want full-screen cause its supposed to be a small applet.
ArcherKinga at 2007-7-12 9:24:37 > top of Java-index,Java Essentials,New To Java...