G
Guest
Gast
Kann mir jemand sagen wieso in diesem Programm der Thread nicht gestartet wird.
[/code][/list]
Code:
import java.awt.*;
import javax.swing.*;
public class Pendel extends JPanel implements Runnable
{
public void Pendel(){}
private Thread runner = null;
int wid;
int heig;
int x=0;
int y=0;
public void init()
{
}
public void paintComponent(Graphics g)
{
wid=this.getSize().width;
heig=this.getSize().height;
//getxy(wid,heig);
g.setColor(Color.black);
g.fillRect(0,0,wid, heig);
g.setColor(Color.red);
g.drawLine(wid/2,0,x/2,150);
g.fillOval(x/2-8,150,16,16);
}
/* public void getxy(int wid, int heig)
{
x=wid;
y=heig;
}*/
public void start()
{
if(runner == null)
{ runner = new Thread(this);
runner.start();
}
}
public void stop()
{
if(runner!=null)
runner=null;
}
public void destroy()
{
}
public void run()
{
while(runner==Thread.currentThread())
{
x=(x+1);
y=(y+1);
try{ Thread.currentThread().sleep(1000);
}catch(InterruptedException e){}
repaint();
System.out.println(x);
System.out.println(y);
}
}
}