import java.applet.*;
import java.awt.*;
public class Strichbegewegen extends Applet implements Runnable { // Initialisierung der Variablen
double x1_posa=2;
double y1_posa=2;
double x1_pos=50;
double y1_pos=30;
double y2_pos=0;
double x2_pos=0;
double x3_pos=0;
double y3_pos=0;
double winkelbeta=((Math.PI*-40)/180);
int appletsize_x=300;
int appletsize_y=200;
private Image dbImage;
private Graphics dbg;
public void init()
{setBackground(Color.BLUE);
}
public void start()
{ Thread th= new Thread(this);
th.start();
}
public void stop() {
}
public void destroy() {
}
public void run()
{ Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
double xsteig=x1_posa+Math.cos(winkelbeta)*1;
double ysteig=y1_posa+Math.sin(winkelbeta)*1;
x2_pos=x1_pos-(Math.sin(60)*15);
y2_pos=y1_pos-(Math.cos(60)*15);
x3_pos=x1_pos-(Math.cos(-60)*15);
y3_pos=y1_pos-(Math.sin(-60)*15);
while(true) {
if ((x1_pos < 0) | (x1_pos > appletsize_x)|(x2_pos < 0)|(x3_pos < 0)| (x2_pos > appletsize_x)| (x3_pos > appletsize_x)) {
xsteig=xsteig*-1;
} // if
// oberer oder unterer Rand
if ((y1_pos < 0) | (y2_pos < 0) | (y3_pos < 0)| (y1_pos > appletsize_y) | (y2_pos > appletsize_y)|(y3_pos > appletsize_y)) {
ysteig =ysteig*-1;
}
x1_pos+=xsteig;
y1_pos+=ysteig;
x2_pos+=xsteig;
y2_pos+=ysteig;
x3_pos+=xsteig;
y3_pos+=ysteig;
repaint();
try {
// Stoppen des Threads für in Klammern angegebene Millisekunden
Thread.sleep(50);
}
catch (InterruptedException ex) {
// do nothing
}
// Zurücksetzen der ThreadPriority auf Maximalwert
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
}
}
/** Update - Methode, Realisierung der Doppelpufferung zur Reduzierung des Bildschirmflackerns */
public void update(Graphics g) {
// Initialisierung des DoubleBuffers
if (dbImage == null) {
dbImage = createImage(this.getSize().width, this.getSize().height);
dbg = dbImage.getGraphics();
}
// Bildschirm im Hintergrund löschen
dbg.setColor(getBackground());
dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);
// Auf gelöschten Hintergrund Vordergrund zeichnen
dbg.setColor(getForeground());
paint(dbg);
// Nun fertig gezeichnetes Bild Offscreen auf dem richtigen Bildschirm anzeigen
g.drawImage(dbImage, 0, 0, this);
}
public void paint(Graphics g) {
g.setColor (Color.red);
//g.fillOval((int)x1_pos,(int)y1_pos, 10,10);
g.drawRect(0,0,300,200);
int[]a={(int)x1_pos,(int)x2_pos,(int)x3_pos};
int[]b={(int)y1_pos,(int)y2_pos,(int)y3_pos};
g.fillPolygon(a,b,3);
/**
* g.drawLine((int)x1_pos,(int)y1_pos,(int)x2_pos,(int)y2_pos);
* g.drawLine((int)x2_pos,(int)y2_pos,(int)x3_pos,(int)y3_pos);
* g.drawLine((int)x3_pos,(int)y3_pos,(int)x1_pos,(int)y1_pos);
*/
repaint();
}
}