【发布时间】:2015-02-09 05:52:56
【问题描述】:
大家好,我在绘制与用户相交的硬币时遇到了麻烦。我想要这段代码做的是当用户精灵与它在硬币上绘制的十个硬币图像中的任何一个相交时,它不再出现在屏幕上(还需要添加某种计数器,以便当用户收集所有十个硬币都会停止线程,并说“你赢了!”)。我的问题是我将如何执行此操作,因为我已尝试在 if 语句中使用 repaint() ,但它不再正确编译。任何有关如何在硬币上绘画的帮助,甚至可能添加某种计数器(认为 for 循环会派上用场)将不胜感激!代码如下:
public void paint(Graphics g)
{
g.clearRect(0, 0, this.getWidth(), this.getHeight());
one.paintComponent(g);
two.paintComponent(g);
three.paintComponent(g);
four.paintComponent(g);
five.paintComponent(g);
six.paintComponent(g);
seven.paintComponent(g);
eight.paintComponent(g);
nine.paintComponent(g);
ten.paintComponent(g);
monster.setLocation(r.nextInt(10) - 5 + monster.x, r.nextInt(10 - 5 + monster.y));
monster.paintComponent(g);
user.paintComponent(g);
if(user.intersects(one))
{
}
if(user.intersects(two))
{
}
if(user.intersects(three))
{
}
if(user.intersects(four))
{
}
if(user.intersects(five))
{
}
if(user.intersects(six))
{
}
if(user.intersects(seven))
{
}
if(user.intersects(eight))
{
}
if(user.intersects(nine))
{
}
if(user.intersects(ten))
{
}
if(user.intersects(monster))
{
g.setFont(new Font("Serif", Font.BOLD, 35));
g.drawString("YOU HAVE DIED, YOU LOSE!", 100, 100); //Prints this when you lose
thread.interrupt(); //Stopping the thread if you die
}
}
【问题讨论】:
标签: java graphics paint intersection bluej