简体   繁体   English

如何在Java中停止并开始弹跳球

[英]how to stop and start bouncing ball in java

I was just wondering how I could manipulate buttons in java to make a bouncing ball stop, and then start. 我只是想知道如何操作Java中的按钮以使弹跳停止并随后启动。 I tried writing an if else statement with a command to start the thread, and a command to stop it. 我尝试编写一条if else语句,其中包含用于启动线程的命令和用于停止线程的命令。 However, that did not work. 但是,那没有用。 Could anyone possibly help me? 有人可以帮我吗? Here is my code: 这是我的代码:

 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; int countX = 0; int countY = 0; Button button1; //space bar Button button2; public void init() { setSize(600,350); t.start(); /* starts the thread */ 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; } 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"); } else { System.out.println("Button 2 was pressed"); } } 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; } } } 

a way that you can do this is 一种可以做到这一点的方法是

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());
      }
    });
  }
}

Forget about thread if you are talking about moving your objects. 如果您在谈论移动对象,请不要使用线程。 You moving your ball by changing its positions by a certain value lets call it speed. 通过将球的位置改变一定值来移动球,可以称之为速度。 The way you should stop your ball is setting its speed to zero when a key is pressed instead of stopping the thread. 停止球的方法是在按下某个键时将其速度设置为零,而不是停止螺纹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM