這段代碼運(yùn)行后,兩小球距離總是恒定,怎樣讓他們兩個是各自獨(dú)立,有各自的軌跡?import java.awt.*;public class Rebound{ public static void main(String args[]) { Frame w=new Frame(); w.setSize(800, 800); MyRebound mp=new MyRebound(); w.add(mp);? ? ? ? ? ? ?Thread t=new Thread(mp); ? ? ? t.start(); ? ? ?? ? ? ?w.show(); ? ? ? ? ? ? ? ? }}class MyRebound extends Panel implements Runnable{ ? ? ?int x=(int)(Math.random()*800);? ? ? ?int y=(int)(Math.random()*800); ? ? ?int i=(int)(Math.random()*800); ? ? ?int j=(int)(Math.random()*800); ? ? ?int att=0; public void paint(Graphics g) { g.fillOval(x, y, 50, 50);? g.setColor(Color.yellow); g.fillOval(i, j, 50, 50); } public void run(){ ? ? ? while(true) { ? ? ? //定義飛行姿態(tài) ? ? ? if(att==0) { ? ? ? x++; ?? ? ? ? y++; ? ? ? i++; ? ? ?j++; ? ? ? } ? ? ? if(att==1) { ? ? ? x--; ? ? ? y++; ? ? ? i--; ? ? ? j++; ? ? ? } ? ? ? if(att==2) { ? ? ? x--; ? ? ? y--; ? ? ? i--; ? ? ? j--; ? ? ? } ? ? ? if(att==3) { ? ? ? x++; ? ? ? y--; ? ? ? i++; ? ? ? j--; ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ?//改變飛行姿態(tài) ? ? ? ? ? ?if(x>730) { ? ? ? ? ? ?if (att==0) { ? ? ? ? ? ? att=1; ? ? ? ? ? ?}else { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? att=2; ? ? ? ?} ?} ? ? ? ? ? ? ? ? ? ? ?if(y>700) { ? ? ? ? ?if(att==1) { ? ? ? ? ?att=2; ? ? ? ? ?}else { ? ? ? ? ?att=3; ? ? ? ? ?} ? ? ? ? ?} ? ? ? ? ?if(x<0) { ? ? ? ? ?if(att==2) { ? ? ? ? ?att=3; ? ? ? ? ?}else { ? ? ? ? ?att=0; ? ? ? ? ?} ? ? ? ? ?} ? ? ? ? ?if(y<0) { ? ? ? ? ?if(att==3) { ? ? ? ? ?att=0; ? ? ? ? ?}else { ? ? ? ? ?att=1; ? ? ? ? ?} ? ? ? ? ?}?//......................................................................................... ? ? ? ? ?if(i>730) { ? ? ? ? ? ?if (att==0) { ? ? ? ? ? ? att=1; ? ? ? ? ? ?}else { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? att=2; ? ? ? ?} ?} ? ? ? ? ? ? ? ? ? ? ?if(j>700) { ? ? ? ? ?if(att==1) { ? ? ? ? ?att=2; ? ? ? ? ?}else { ? ? ? ? ?att=3; ? ? ? ? ?} ? ? ? ? ?} ? ? ? ? ?if(i<0) { ? ? ? ? ?if(att==2) { ? ? ? ? ?att=3; ? ? ? ? ?}else { ? ? ? ? ?att=0; ? ? ? ? ?} ? ? ? ? ?} ? ? ? ? ?if(j<0) { ? ? ? ? ?if(att==3) { ? ? ? ? ?att=0; ? ? ? ? ?}else { ? ? ? ? ?att=1; ? ? ? ? ?} ? ? ? ? ?} ? ? ? ? ?try { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Thread.sleep(5); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //線程休眠5毫秒 ? ? ? }catch(Exception e) {} ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//異常處理 ? ? ? ? repaint(); ? ? ? ? } ?? }}
添加回答
舉報
0/150
提交
取消