2016-05-12 14 views
0

私はバウンスボールを停止させるためにJavaのボタンをどのように操作したらよいのだろうかと思っていました。私はスレッドを開始するコマンドとそれを停止するコマンドでif else文を書くことを試みました。しかし、それはうまくいかなかった。誰でも私を助けることができるだろうか?ここに私のコードは次のとおりです。あなたは、オブジェクトの移動について話している場合は、これを行うことができJavaでバウンスボールを停止して開始する方法

package javaapplication1; 
 

 
import java.awt.*; 
 
import java.applet.*; 
 
import java.applet.Applet; 
 
import java.awt.event.*; 
 

 
public class BouncingBallOriginal extends Applet implements Runnable, ActionListener 
 
{ 
 
    Thread t=new Thread(this); /* declare and initialize a new thread */ 
 
    int x = 0; 
 
    int y = 0; 
 
\t 
 
    int countX = 0; 
 
    int countY = 0; 
 
      
 
    Button button1;   //space bar 
 
    Button button2; 
 

 
    public void init() 
 
    { 
 
     setSize(600,350); 
 
     t.start(); /* starts the thread */ 
 
\t 
 
     setBackground(Color.blue); 
 
     
 
     button1 = new Button("Button 1"); 
 
     add(button1); 
 
     button1.addActionListener(this); 
 

 
     button2 = new Button("Button 2"); 
 
     add(button2); 
 
     button2.addActionListener(this); 
 
    } 
 

 
    private void incrementX() { x += 10; } 
 
    private void decrementX() { x -= 10; } 
 
    private void incrementY() { y += 10; } 
 
    private void decrementY() { y -= 10; } 
 
\t 
 
    public void run() 
 
    { 
 
     try 
 
     { 
 
      for(int i = 1; i > 0; i++) 
 
      { 
 
       Thread.sleep(200); 
 
       repaint(); 
 
      } 
 
     } 
 
     catch(InterruptedException e) 
 
     { 
 
      //do nothing! 
 
     } 
 
    } 
 
    
 
    public void actionPerformed(ActionEvent e) 
 
    { 
 
     if (e.getSource() == button1) 
 
     { 
 
      System.out.println("Button 1 was pressed"); 
 
     } \t 
 
     else 
 
     { 
 
      System.out.println("Button 2 was pressed"); 
 
     } \t \t 
 
    } 
 

 

 
    public void paint(Graphics g) 
 
    { 
 
     g.setColor(Color.yellow); 
 
     g.fillOval(x, y, 20, 20); 
 
     
 
     if(countX< (getSize().width/10) - 1) 
 
     { 
 
      incrementX(); 
 
      countX++;    
 
     } 
 

 
     if(countX >= (getSize().width/10) - 1) 
 
     { 
 
      decrementX(); 
 
      countX++; 
 
     } 
 

 
     if(countX >= (getSize().width/5) - 2) 
 
     { 
 
      countX=0; 
 
     } 
 

 
     if(countY < (getSize().height/10) - 1) 
 
     { 
 
      incrementY(); 
 
      countY++; 
 
     } 
 

 
     if(countY >= (getSize().height/10) - 1) 
 
     { 
 
      decrementY(); 
 
      countY++; 
 
     } 
 

 
     if(countY >= (getSize().height/5) - 2) 
 
     { 
 
      countY=0; 
 
     } 
 
    } 
 
}

+0

重複していますか? http://stackoverflow.com/questions/16758346/how-pause-and-then-resume-a-thread – rleir

答えて

0

方法

import javax.swing.*; 
import java.awt.event.*; 
import java.awt.*; 

public class BallControl extends JPanel { 
    private Ball ball = new Ball(); 
    private JButton jbtSuspend = new JButton("Suspend"); 
    private JButton jbtResume = new JButton("Resume"); 
    private JScrollBar jsbDelay = new JScrollBar(); 

    public BallControl() { 
    // Group buttons in a panel 
    JPanel panel = new JPanel(); 
    panel.add(jbtSuspend); 
    panel.add(jbtResume); 

    // Add ball and buttons to the panel 
    ball.setBorder(new javax.swing.border.LineBorder(Color.red)); 
    jsbDelay.setOrientation(JScrollBar.HORIZONTAL); 
    ball.setDelay(jsbDelay.getMaximum()); 
    setLayout(new BorderLayout()); 
    add(jsbDelay, BorderLayout.NORTH); 
    add(ball, BorderLayout.CENTER); 
    add(panel, BorderLayout.SOUTH); 

    // Register listeners 
    jbtSuspend.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
     ball.suspend(); 
     } 
    }); 
    jbtResume.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
     ball.resume(); 
     } 
    }); 
    jsbDelay.addAdjustmentListener(new AdjustmentListener() { 
     public void adjustmentValueChanged(AdjustmentEvent e) { 
     ball.setDelay(jsbDelay.getMaximum() - e.getValue()); 
     } 
    }); 
    } 
} 
0

は、スレッドは忘れています。特定の値で位置を変えてボールを動かすと、スピードを呼ぶことができます。ボールを止める方法は、スレッドを止める代わりにキーを押したときに速度をゼロに設定することです。

関連する問題