在我的代码中添加一个计时器

时间:2015-08-15 19:34:01

标签: java timer

我已经搜索并试图自己实现一个计时器,并且无法解决我的生活。所以我想要做的是每五秒我想要随机圆圈改变到一个新的随机位置。它似乎很简单,但我无法让它工作。

public class BounceThread implements Runnable {
  JFrame frame;
  int myX = 500 / 2;
  int myY = 500 / 2;;

  int X;
  int Y;

  Canvas canvas;
  BufferStrategy bufferStrategy;
  BufferStrategy bufferStrategy2;
  boolean running = true;
  Random rand = new Random();
  Random rand2 = new Random();

  int n = rand.nextInt(450) + 1;
  int n2 = rand.nextInt(450) + 1;

  public BounceThread() {
    frame = new JFrame("Basic Game");
    JPanel panel = (JPanel) frame.getContentPane();
    panel.setPreferredSize(new Dimension(500, 500));
    panel.setLayout(null);
    canvas = new Canvas();
    canvas.setBounds(0, 0, 500, 500);
    canvas.setIgnoreRepaint(true);
    panel.add(canvas);
    canvas.addKeyListener(new KeyAdapter() {
      @Override
      public void keyPressed(KeyEvent evt) {
        moveIt(evt);
      }
    });
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setResizable(false);
    frame.setVisible(true);
    canvas.createBufferStrategy(2);
    bufferStrategy = canvas.getBufferStrategy();
    canvas.requestFocus();
  }

  public void run() {
    while (running = true) {
      Paint();
      if ((myX == n || myX == n) && (myY == n2 || myY == n2)) {

        n = rand.nextInt(450) + 1;
        n2 = rand.nextInt(450) + 1;
        n2 = 10 * (Math.round(n / 10));
        n = 10 * (Math.round(n / 10));
        Paint();

      }
    }
  }

  public static void main(String[] args) {
    BounceThread ex = new BounceThread();
    new Thread(ex).start();
  }

  public void Paint() {

    Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics();
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
        RenderingHints.VALUE_ANTIALIAS_ON);
    g.clearRect(0, 0, 500, 500);
    Graphics2D e = (Graphics2D) bufferStrategy.getDrawGraphics();
    e.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
        RenderingHints.VALUE_ANTIALIAS_ON);
    e.clearRect(0, 0, 500, 500);

    Paint(g, e);

    bufferStrategy.show();
  }

  protected void Paint(Graphics2D g, Graphics2D e) {
    n2 = 10 * (Math.round(n / 10));
    n = 10 * (Math.round(n / 10));
    g.fillOval(myX, myY, 30, 30);

    g.setColor(Color.red);

    e.fillOval(n, n2, 30, 30);

    e.setBackground(Color.white);
  }

  public void moveIt(KeyEvent evt) {
    switch (evt.getKeyCode()) {
    case KeyEvent.VK_DOWN:
      if (myY >= 450) {
        break;
      } else {
        myY += 10;
        break;
      }
    case KeyEvent.VK_UP:
      if (myY <= 30) {
        break;
      } else {
        myY -= 10;
        break;
      }
    case KeyEvent.VK_LEFT:
      if (myX <= 30) {
        break;
      } else {
        myX -= 10;
        break;
      }
    case KeyEvent.VK_RIGHT:
      if (myX >= 450) {
        break;
      } else {
        myX += 10;
        break;
      }
    }
  }
}

1 个答案:

答案 0 :(得分:-1)

将以下内容添加到run()方法的开头,在while循环之前的第一行可能正在执行您正在寻找的内容:

    TimerTask task = new TimerTask() {@
        Override
        public void run() {
            n = rand.nextInt(450) + 1;
            n = 10 * (Math.round(n / 10));
        }
    };
    Timer timer = new Timer();
    timer.scheduleAtFixedRate(task, 0, 5 * 1000);

只是为了给你一个想法,我认为总体设计(代码的重用等)必须得到改进。