如何使图形随机出现并在与另一个图形碰撞时消失?

发布于 2024-12-11 07:05:57 字数 1251 浏览 0 评论 0原文

我已经编写了一些关于移动图形(被矩形包围)的代码,并且我正在尝试绘制另一个随机生成的椭圆形(周围有一个矩形)。现在,它的生成速度非常快,而且我不想使用 Thread.sleep;因为它会停止监听按键(据我所知?)。那么,任何擅长多线程的人都可以帮助我做到这一点,或者知道如何使图形出现,直到它被可移动图形触摸。

主类中的图形生成器:

public void paintComponent(Graphics g){
    //System.out.println("X = " + al.x + ", Y = " + al.y);
    boolean intersect = false;
    int points = 0;

    g.drawString("Points: " + points, 5, 445);

    Rectangle r1 = new Rectangle(al.x, al.y, 10, 10);
    g.setColor(Color.BLACK);
    g.fillOval(al.x, al.y, 10, 10);

    Random randX = new Random();
    Random randY = new Random();
    int xInt = randX.nextInt(590);
    int yInt = randY.nextInt(440);

    Rectangle dCoin = new Rectangle(xInt, yInt, 10, 10);
    g.setColor(Color.YELLOW);
    g.fillOval(xInt, yInt, 10, 10);


        /*
         * (???)
         * 
         * for(int idx = 1; idx == 1; idx++){
         *      if(xInt < 590 && yInt < 440){
         *      }
         *  }
         *
         * Check if graphic collides with another:
         * 
         * if(r1.intersects(r2)){
         *      doSomething;
         * }
         *
         */
        repaint();
    }

}

顺便说一句:r1 包围可移动图形,r2 是包围随机生成图形的矩形。我必须在椭圆周围制作不可见的矩形才能获得 r1.intersects(r2) 方法。

I have written some code on moving a graphics (that is surrounded by a rectangle), and I am trying to draw another oval (with a rectangle around it) that will generate at random. Right now, it's generating WAY fast, and I don't want to use Thread.sleep; because it will stop listening for the keys (to my knowledge?). So is anyone good with multi-threading that could help me do this or know how to make a graphic appear until it is touched by the movable graphic.

The Graphics Generator in main class:

public void paintComponent(Graphics g){
    //System.out.println("X = " + al.x + ", Y = " + al.y);
    boolean intersect = false;
    int points = 0;

    g.drawString("Points: " + points, 5, 445);

    Rectangle r1 = new Rectangle(al.x, al.y, 10, 10);
    g.setColor(Color.BLACK);
    g.fillOval(al.x, al.y, 10, 10);

    Random randX = new Random();
    Random randY = new Random();
    int xInt = randX.nextInt(590);
    int yInt = randY.nextInt(440);

    Rectangle dCoin = new Rectangle(xInt, yInt, 10, 10);
    g.setColor(Color.YELLOW);
    g.fillOval(xInt, yInt, 10, 10);


        /*
         * (???)
         * 
         * for(int idx = 1; idx == 1; idx++){
         *      if(xInt < 590 && yInt < 440){
         *      }
         *  }
         *
         * Check if graphic collides with another:
         * 
         * if(r1.intersects(r2)){
         *      doSomething;
         * }
         *
         */
        repaint();
    }

}

BTW: r1 surrounds the movable graphic, and r2 is the rectangle surrounding the randomly generated graphic. I had to make invisible rectangles around the ovals to get the r1.intersects(r2) method.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

身边 2024-12-18 07:05:57

您应该使用 Swing Timer 类定期生成 事件调度线程上的ActionEvent。这避免了应用程序对击键和其他用户输入无响应的问题。

actionPerformed 回调是路由中的挂钩,用于移动和重绘您想要设置动画的对象。在动画例程中,您可以记录自上次调用该方法以来所经过的时间,以保持所需的速度。

Timer timer = new Timer(1000, new ActionListener() {
  long lastTime = System.currentTimeMillis();

  public void actionPerformed(ActionEvent evt) {
    long timeNow = System.currentTimeMillis();
    long timeEllapsed = timeNow - lastTime;
    lastTime = timeNow;

    if (timeEllapsed > 0L) {
      for (Moveable mv : moveables) {
        mv.updatePosition(timeEllapsed);
      }

      for (Drawable d : drawables) {
        d.repaint();
      }
    }
  }
});

You should use the Swing Timer class to periodically generate ActionEvents on the Event Dispatch Thread. This avoids the problem of the application becoming unresponsive to keystrokes and other user input.

The actionPerformed callback is the hook into your routing to move and repaint the object(s) you wish to animate. Within the animation routine you can record the time ellapsed since the last time the method was called in order to maintain the desired velocity.

Timer timer = new Timer(1000, new ActionListener() {
  long lastTime = System.currentTimeMillis();

  public void actionPerformed(ActionEvent evt) {
    long timeNow = System.currentTimeMillis();
    long timeEllapsed = timeNow - lastTime;
    lastTime = timeNow;

    if (timeEllapsed > 0L) {
      for (Moveable mv : moveables) {
        mv.updatePosition(timeEllapsed);
      }

      for (Drawable d : drawables) {
        d.repaint();
      }
    }
  }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文