如何使图形随机出现并在与另一个图形碰撞时消失?
我已经编写了一些关于移动图形(被矩形包围)的代码,并且我正在尝试绘制另一个随机生成的椭圆形(周围有一个矩形)。现在,它的生成速度非常快,而且我不想使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该使用 Swing
Timer
类定期生成 事件调度线程上的ActionEvent
。这避免了应用程序对击键和其他用户输入无响应的问题。actionPerformed
回调是路由中的挂钩,用于移动和重绘您想要设置动画的对象。在动画例程中,您可以记录自上次调用该方法以来所经过的时间,以保持所需的速度。You should use the Swing
Timer
class to periodically generateActionEvent
s 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.