2013-12-15 112 views
1

我遇到了線程問題。我正在嘗試重新制作乒乓球,其中有2名球員的動作聽衆。這兩個槳以我希望它們移動的速度移動。然而,「球」(橢圓形的矩形對象)正在移動得太快。我試過用Ball類中的第二個線程放慢速度,但這似乎不起作用。任何幫助,將不勝感激。 這裏是我的代碼:需要緩慢的矩形運動

public class BattleBallz extends JFrame implements Runnable { 
AL keyListen1 = new AL(); 
AL2 keyListen2 = new AL2(); 
Image dbi; 
Graphics dbg; 

int x1,x2; 
int ballX, ballY, direction; 
Ball b1; 

public BattleBallz(){ 
    setTitle("Battle Ballz"); 
    setSize(350, 400); 
    setResizable(false); 
    setVisible(true); 
    setLocationRelativeTo(null); 
    setDefaultCloseOperation(EXIT_ON_CLOSE); 
    addKeyListener(keyListen1); 
    addKeyListener(keyListen2); 
    x1 = 150; 
    x2 = 150; 
    int direction = 0 + (int)(Math.random() * ((2 - 0) + 1)); 
    b1 = new Ball(direction); 
    Thread b = new Thread(b1); 
    b.start(); 
} 

public void paint(Graphics g){ 
    dbi = createImage(getWidth(), getHeight()); 
    dbg = dbi.getGraphics(); 
    paintComponent(dbg); 
    g.drawImage(dbi, 0, 0, this); 
} 

public void paintComponent(Graphics g){ 
    Rectangle p1 = new Rectangle(x1,375,60,10); 
    g.setColor(Color.blue); 
    g.fillRect(x1, 375, 60, 10); 
    Rectangle p2 = new Rectangle(x2,45,60,10); 
    g.setColor(Color.red); 
    g.fillRect(x2,45,60,10); 

    b1.paintComponent(g, p1, p2); 

    repaint(); 
} 


@Override 
public void run() { 
    try { 
     while(true){ 
      move(); 
      Thread.sleep(5); 
     } 
    } catch (Exception e) { 
     // TODO: handle exception 
    } 

} 

public void move(){ 
    x1 += keyListen1.getXdirection(); 
    x2 += keyListen2.getXdirection(); 

    if (x1<=0){ 
     x1 =0; 
    } 
    if (x1>=305){ 
     x1 = 305; 
    } 
    if (x2<=0){ 
     x2=0; 
    } 
    if (x2>=305){ 
     x2=305; 
    } 
} 
} 

public class Ball extends JPanel implements Runnable { 

boolean up,down,right,left; 
int x = 150, y = 150 ; 

public Ball(int direction){ 
    if(direction ==1){ 
     down = true; 
    } 
    if (direction ==0){ 
     up = true; 
    } 
} 

public void paintComponent(Graphics g, Rectangle p1, Rectangle p2){ 
    super.paintComponents(g); 
    if(down){ 
     y++; 
     Rectangle ball = new Rectangle(x,y,49,49); 
     g.fillOval(x, y, 50, 50); 
     if (y>=385 || ball.intersects(p1)){ 
      down = false; 
      up = true; 
     } 
    } 
    if (up){ 
     y--; 
     Rectangle ball = new Rectangle(x,y,49,49); 
     g.fillOval(x, y, 50, 50); 
     if (y<=10 || ball.intersects(p2)){ 
      up = false; 
      down = true; 
     } 
    } 
} 

@Override 
public void run() { 
    // TODO Auto-generated method stub 
    try { 
     while (true){ 
      Thread.sleep(30); 
     } 
    } catch (Exception e) { 
     // TODO: handle exception 
    } 
} 
} 
+0

從來沒有問過之前在這裏的一個問題。試圖找出如何發佈我的球類。 – user3105381

+0

其他代碼是相當不相關的,我們可能只需要Ball類中的run方法和任何調用的方法。 – Sinkingpoint

+0

已發佈。謝謝。 – user3105381

回答

3
  1. 使用Swing Timer而非純Thread,你有一個問題與Concurency in Swing,代碼發佈到阻止Event Dispatch Thread,通過Thread.sleep鎖定,無盡塊

  2. 使用paintComponent但放有JPanel,1st。碼線內應super.paintComponent,否則油漆被累積,覆蓋getPreferredSizeJPanel

  3. 使用KeyBindings代替KeyListener

+0

我會給它一個鏡頭。謝謝! – user3105381

+0

在由paintComponent標記的文章中搜索,其中一部分包含(在答案中)有關我的積分的非常好的代碼 – mKorbel