Hi Leute,
Das folgende Programm lässt eine Fliege per Zufall über den Bildschirm fliegen. Leider flackert das Bild sehr stark.
Ich habe nun gehört, dass sich das verhindern liese, wenn man einen Buffer einbaut.
Wisst ihr mehr darüber?
Das folgende Programm lässt eine Fliege per Zufall über den Bildschirm fliegen. Leider flackert das Bild sehr stark.
Ich habe nun gehört, dass sich das verhindern liese, wenn man einen Buffer einbaut.
Wisst ihr mehr darüber?
Code:
import java.awt.*;
import java.applet.*;
public class Fliege extends java.applet.Applet implements Runnable
{
int posy=1,posx=1,zufall=0,zufall2=0,zufall3=0;
Thread Flieg = null;
public void init()
{
setLayout(null);
setBackground(java.awt.Color.white);
setSize(800,600);
}
public void stop()
{
Flieg.stop();
Thread Flieg = null;
}
public void run()
{
while(true)
{
zufall2= (int)(Math.random()*2+1);
zufall3= (int)(Math.random()*2+1);
if(zufall2==1)
posx++;
if(zufall2==2)
posx--;
if(zufall3==1)
posy++;
if(zufall3==2)
posy--;
zufall = (int)(Math.random()*20);
try
{
Flieg.sleep(zufall);
}
catch(InterruptedException e)
{
}
repaint();
}
}
public void paint(Graphics g)
{
Image fly = getImage(getCodeBase(),"Fliege.jpg");
if(posx<=0)
posx=1;
if(posy<=0)
posy=1;
if(posy>=800)
posy=799;
if(posy>=600)
posy=599;
g.drawImage(fly,posx,posy,this);
}
public void start()
{
Flieg = new Thread(this);
Flieg.start();
}
}