2013-04-08 85 views
0

從左側和底部的兩個球在相交的某個座標上相互碰撞。我已經做了我在互聯網上搜索的內容,並且工作完美,但我需要一個開始,暫停和恢復按鈕。看看我完成:暫停/恢復Java小程序

import java.applet.Applet; 
import java.awt.Button; 
import java.awt.Graphics; 
import java.awt.Image; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 


public class train extends Applet implements Runnable,ActionListener { 
    private volatile boolean runs = true; 
    private Image i; 
    private Graphics doubleG; 
    Ball b, b2; 
    Button x,y,z; 
    Thread thread = new Thread(this); 
    @Override 
     public void init(){ 
     setSize(800, 600); 
     x = new Button("Action!"); 
     y = new Button("Stop"); 
     z = new Button("Resume!"); 
     add(x); 
     add(y); 
     add(z); 


     y.addActionListener(new ActionListener() { 

      @SuppressWarnings("deprecation") 
      public void actionPerformed(ActionEvent e) 
      { 

       runs = false; 
       repaint(); 
      } 
     }); 


     z.addActionListener(new ActionListener() { 

      @SuppressWarnings("deprecation") 
      public void actionPerformed(ActionEvent e) 
      { 

       try { 

        runs = true; 
        b.update(this); 
        repaint(); 
       } catch (InterruptedException e1) { 
        // TODO Auto-generated catch block 
        e1.printStackTrace(); 

       } 

        b2.update2(this); 
        repaint(); 
      } 
     }); 

    } 





    @Override 
    public void start(){ 
     x.addActionListener(this); 
     b = new Ball(100, 100); 
     b2 = new Ball(500, 500); 
     } 
    @Override 

    public void run(){ 

     while(runs){ 
      b.update(this); 
      b2.update2(this); 

      repaint(); 
      try { 
       Thread.sleep(17); 
      } catch (InterruptedException e) { 
       //TODO Auto-generated catch block 
      // e.printStackTrace(); 
      } 
     } 
    } 



    @Override 
    public void stop(){ 


    } 
    @Override 
    public void destroy(){ 

    } 
    @Override 
    public void update(Graphics g) { 
     // TODO Auto-generated method stub 
     if(i == null){ 
      i = createImage(this.getSize().width, this.getSize().height); 
     doubleG = i.getGraphics(); 
     } 
     doubleG.setColor(getBackground()); 
     doubleG.fillRect(0, 0, this.getSize().width, this.getSize().height); 

     doubleG.setColor(getForeground()); 
     paint(doubleG); 

     g.drawImage(i, 0, 0, this); 

     } 

    @Override 
    public void paint(Graphics g){ 
     b.paint(g); 
     b2.paint(g); 
    } 

    public void actionPerformed(ActionEvent e) { 
     thread.start(); 

    } 


} 

主train.class和:

import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.Rectangle; 
import java.awt.event.ActionListener; 


public class Ball { 


    private int x; 
    private int y; 
    private double dx = 7.9; 
    private double dy = 7; 
    private int radius = 20; 


    public Ball() { 
     // TODO Auto-generated constructor stub 
    } 


    public Ball(int i, int j) { 
     // TODO Auto-generated constructor stub 
     x = i; 
     y = j; 

    } 
    public void update(train sp){ 
       x += dx; 

       // if(x + dx > sp.getSize().width - 300){ 
     //  dx=0; 
     // } 
       } 


    public void paint(Graphics g){ 
     g.setColor(Color.GREEN); 


     g.fillOval(x-radius, y-radius, radius * 2, radius * 2); 

    } 
    public void update2(train sp){ 
     y -= dy; 
     if(y - dy < sp.getSize().height - 470){ 

      x += dx; 
      y -= dy; 



     // if(y < sp.getSize().height - 470){ 
     //  y = sp.getSize().height -470; 
     //  dy *= energyloss; 
     //  dy = -dy; 
     // }else{ 

     //  dy += gravity * dt; 
     //  y += dy*dt + .5 * gravity * dt * dt; 
      } 
     //} 
} 
    public void update(ActionListener actionListener) throws InterruptedException { 

     x += dx; 
       } 

    public void update2(ActionListener actionListener) { 

    train tr = new train(); 


    if(y - dy < tr.getSize().height - 470){ 
     x += dx; 
     y -= dy; 
     }else{ 
     y-=dy; 

     } 





    } 
} 

我想要做的就是我想要做一個恢復按鈕。我已經完成了開始和暫停,但是當我點擊恢復按鈕時,它一次只移動1個座標。我需要它就像開始,暫停和正常播放。請幫忙。 T_T

回答

1

一個簡單的解決辦法是沒有「運行」控制循環,而只是確定是否調用更新方法。這樣你就不會打破循環,而必須重新啓動。