augussmile 2016-05-11 17:20 采纳率: 7.7%
浏览 1187

java 多线程问题,新手求教!!!!!!

public class LinesRectsOvalsJPanel extends JPanel implements Runnable
{
private final int sleeptime=5;
private int x=400;
private int y=0;
private double xspeed=1.0,yspeed=1.0;
public LinesRectsOvalsJPanel(int x,int y,double xspeed,double yspeed){
this.x=x;
this.y=y;
this.xspeed=xspeed;
this.yspeed=yspeed;
}
public int getx(){
return x;
}
public int gety(){
return y;
}
public void setballxy(int x,int y){
this.x=x;
this.y=y;
}
public void paint( Graphics g)
{
super.paint(g);
this.setBackground( Color.WHITE );
g.setColor( Color.MAGENTA );
g.fillOval( this.getx(),this.gety(),20, 20);
} // end method paintComponent
public void run()
{

   while(true)
   {
     if(x + xspeed + 2*20 > 800 || x + xspeed < 0){         //当在X轴上碰到墙时,X轴行进方向改变
               xspeed*=-1;
    }else{
               x += xspeed;                 //没碰壁时继续前进
    }
    if(y + yspeed + 2*20 > 800 || y + yspeed < 0){         //当在Y轴上碰到墙时,Y轴行进方向改变
              yspeed*=-1;
    }else{
              y += yspeed;
    }
    this.repaint();
      try{       
             Thread.sleep(sleeptime); 
         }catch(InterruptedException e){
             e.printStackTrace();
         }
   }

}
} // end class LinesRectsOvalsJPanel
public class LinesRectsOvals
{
// execute application
public static void main( String[] args )
{

  // create frame for LinesRectsOvalsJPanel
  JFrame frame = 
     new JFrame( "Drawing lines, rectangles and ovals" );
  frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  LinesRectsOvalsJPanel linesRectsOvalsJPanel = 
     new LinesRectsOvalsJPanel(400,0,1.0,1.0); 
 LinesRectsOvalsJPanel linesRectsOvalsJPane2 = 
             new LinesRectsOvalsJPanel(700,0,1.0,1.0); 
  linesRectsOvalsJPanel.setBackground( Color.WHITE ); 
  linesRectsOvalsJPane2.setBackground( Color.WHITE );
  ExecutorService threadExecutor=Executors.newCachedThreadPool();

  frame.add( linesRectsOvalsJPanel ); // add panel to frame
  frame.add( linesRectsOvalsJPane2 );
  frame.setSize( 800, 800 ); // set frame size
  frame.setVisible( true ); // display frame
  threadExecutor.execute(linesRectsOvalsJPane2);
  threadExecutor.execute(linesRectsOvalsJPanel);

//  linesRectsOvalsJPane2.ballmove();

} // end main
} // end class LinesRectsOvals
程序中明明初始化了两个弹球,为什么最后运行的时候还是一个。。。新手,不懂。。。

  • 写回答

3条回答 默认 最新

  • augussmile 2016-05-11 17:23
    关注

    图片说明
    运行界面是这样。。。。。

    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程