Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使图形随机出现,并在与其他图形碰撞时消失?_Java_Multithreading_Graphics_Random_Generator - Fatal编程技术网

Java 如何使图形随机出现,并在与其他图形碰撞时消失?

Java 如何使图形随机出现,并在与其他图形碰撞时消失?,java,multithreading,graphics,random,generator,Java,Multithreading,Graphics,Random,Generator,我已经写了一些关于移动图形(被矩形包围)的代码,我正在尝试绘制另一个随机生成的椭圆形(周围有一个矩形)。现在,它的生成速度很快,我不想使用Thread.sleep;因为它会停止听钥匙(据我所知?)。那么,有谁擅长多线程,可以帮助我做到这一点,或者知道如何使图形出现,直到它被可移动的图形触摸 主类中的图形生成器: public void paintComponent(Graphics g){ //System.out.println("X = " + al.x + ", Y = " + a

我已经写了一些关于移动图形(被矩形包围)的代码,我正在尝试绘制另一个随机生成的椭圆形(周围有一个矩形)。现在,它的生成速度很快,我不想使用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();
    }

}
公共组件(图形g){
//System.out.println(“X=“+al.X+”,Y=“+al.Y”);
布尔相交=假;
积分=0;
g、 抽绳(点:+点,5445);
矩形r1=新矩形(al.x,al.y,10,10);
g、 设置颜色(颜色为黑色);
g、 圆形(al.x,al.y,10,10);
Random randX=新的Random();
随机随机数=新随机数();
int xInt=randX.nextInt(590);
int yInt=randY.nextInt(440);
矩形dCoin=新矩形(xInt,yInt,10,10);
g、 setColor(颜色为黄色);
g、 fillOval(xInt,yInt,10,10);
/*
* (???)
* 
*for(intidx=1;idx=1;idx++){
*如果(xInt<590和&yInt<440){
*      }
*  }
*
*检查图形是否与其他图形冲突:
* 
*if(r1.相交(r2)){
*剂量测定法;
* }
*
*/
重新油漆();
}
}
顺便说一句:r1包围可移动图形,r2是包围随机生成图形的矩形。为了得到r1.intersects(r2)方法,我必须在椭圆周围制作不可见的矩形。

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

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

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

为了更快地获得更好的帮助,请发布一个。我不知道你在和谁说话,也不知道“it”应该是什么,但请注意,如果“it”是代码,那么“it”仍然是不可编译的代码片段,而不是SSCCE。请1)点击SSCCE的链接。2) 阅读文件。3) 准备一份SSCCE。4) 发一个SSCCE。对不起,我说的“它”是指原来的帖子。代码只是显示查询所在的paint component方法。“代码只是显示查询所在的paint component方法。”现在距离您提出该问题“仅”2天了。我坚持我最初的意见和建议。