问关于线程画图的
class pc1{ //画一个矩形public pc1(Color c,int x,int y){
this.c=c;this.x=x;this.y=y;
}
Color c;int x,y;
public void draw(Graphics g){
g.setColor(c);
g.fillRect(x, y, 50, 50);
}
}
class Producer extends Thread{ //线程
public Producer(Graphics2D g1){
this.g=g1;
}
pc1 pct=new pc1(Color.BLUE,50,50);
Graphics2D g;
public void run(){
while(true){
try {
pct.draw(g); //调用pctr的draw()方法
Thread.sleep(5000);
}
catch (Exception ex) {}
}
}
}
class contentpanel extends JPanel{
Graphics2D g2;
public void paintComponent(Graphics g){
super.paintComponent(g);
g2=(Graphics2D)g;
pt.draw(g);
c1.start();p1.start();
}
pc1 pt=new pc1(Color.BLUE,50,50);
Producer p1=new Producer(g2);
Cus c1=new Cus(g2);
}
---------------------------------------------
以上是一部分代码。为啥我直接调用方法以就行了..但是用个线程转了下就不行了..麻烦高手指教下
顺便问下怎么用 图形界面模拟生产消费者的问题