Home Nach oben Zeichnen Quelltext Ball Quelltext CatchMe Quelltext
| |
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
class Applet1 extends Applet implements Runnable
{
private int x=0;
private int y=0;
private int deltaX=0;
private int deltaY=0;
private int maxX=0;
private int maxY=0;
private int ovalX=20;
private int ovalY=20;
private int pause=0;
Thread counter;
public void start()
{
if (counter==null)
{
counter=new Thread(this);
counter.start();
}
}
public void stop()
{
if (counter!=null)
{
counter.stop();
counter=null;
}
}
public void init()
{
pause=10;
Button b1 = new Button("Stop");
b1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
stop();
}
});
add(b1);
Button b3 = new Button("Schneller");
b3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
pause--;
if (pause < 0) pause = 0;
}
});
add(b3);
Button b4 = new Button("Langsamer");
b4.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
pause++;
if (++pause > 100) pause = 100;
}
});
add(b4);
Button b2 = new Button("Start");
b2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
start();
}
});
add(b2);
}
public synchronized void paint(Graphics g)
{
g.setColor(Color.black);
g.fillOval(x,y,ovalX,ovalY);
}
public void run()
{
try
{
for (;;)
{
maxX=getSize().width-ovalX;
maxY=getSize().height-ovalY;
deltaY=0;
deltaX=0;
y=0;
while (deltaY<maxY/2)
{
for (int i=deltaX ;i<=(maxX-deltaX); i++)
{
x=i;
Thread.sleep(pause);
repaint();
}
for (int i=deltaY ;i<=maxY-deltaY; i++)
{
y=i;
Thread.sleep(pause);
repaint();
}
deltaX+=ovalX;
for (int i=x ;i>=deltaX; i--)
{
x=i;
Thread.sleep(pause);
repaint();
}
deltaY+=ovalY;
for (int i=y ;i>=deltaY; i--)
{
y=i;
Thread.sleep(pause);
repaint();
}
}
}
}
catch(InterruptedException e)
{
System.out.println(e);
}
}
}
|