如何从B类中获取A类窗体的画布并调用paint()在A类的窗体背景画图?

外键KEY 2012-12-25 08:51:38
源代码如下所示:

package Test6;
import java.awt.*;
import java.awt.event.*;


public class BridgeThroughOut {
BridgePanel winframe;
public BridgeThroughOut(){}
public BridgeThroughOut(String wintitle){
winframe=new BridgePanel(wintitle);
}//过桥类,其中包括桥面板类,过桥人的类

public static class BridgePanel extends Frame{
Graphics g;
public BridgePanel(){}
public BridgePanel(String wintitle){
super(wintitle);
setSize(500,500);
setLayout(new FlowLayout(FlowLayout.CENTER,10,10));
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
}
});
this.setVisible(true);
}

public class Person extends Thread{
int x,y;
Color color;
int sleeptime;//人过桥的速度
String direction;//方向
BridgePanel BP;

public Person(){}
public Person(BridgePanel BP,int x,int y,int v,String direct,Color color){
this.BP=BP;
this.x=x;
this.y=y;
this.color=color;
sleeptime=v;
direction=direct;
}//创建人的构造函数
public void run(){
if(direction=="North")
while(y>50){
y--;
try{
Thread.sleep(sleeptime);
}catch(Exception e){
System.out.println(e);
}
repaint();
}
else if(direction=="South")
while(y<450){
y++;
try{
Thread.sleep(sleeptime);
}catch(Exception e){
System.out.println(e);
}
repaint();
}
}

public void setGraphics(BridgePanel BP){
}
public void paint(Graphics g){
Graphics g1=BP.getGraphics();
g1.setColor(color);
g1.fillOval(x, y, 20, 20);
}
}//过桥人的类,把人当做质点,以颜色的不同区分南北桥上的人

public void addPerson(BridgePanel BP,int x,int y,int v,String direct,Color color){
new Person(BP,x,y, v,direct,color).start();
//new Thread(new Person(x,y, v,direct,color).start();
}//添加过桥人的方法
}// 过桥面板的创建

public static void main(String[] args){
//添加人的参数分别是所选面板,横坐标、纵坐标、方向(South或者North)、描绘颜色
BridgePanel BP=new BridgePanel("过桥问题");
BP.addPerson(BP,250,450,10,"North",Color.red);
BP.addPerson(BP,250,50,100,"South",Color.green);
}
}


我想问的是怎么能够在BridgePanel中的addPerson中创建线程的时候能够在调用Person中的paint()方法,然后在BridgePanel中画出来?
...全文
35 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧