我们制定了一种方法来确定我们的图像在我们的框架中的位置。如果图像在框架的某个部分,它将显示一个消息对话框,我们将返回我们的主JPanel
(我们正在制作迷你高尔夫游戏;))
问题是当消息对话框出现时,它在他切换到我们的主面板后不断重新出现。这是代码:
package Project;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
public class Blackhole
{
Image blackhole;
int xblack;
int yblack;
public final int breedte=75;
public final int hoogte=75;
public String afbeelding2;
public Atoom atoom;
public Frame venster;
public SpelPaneel x;
public Blackhole(int xblack, int yblack, String afbeelding2)
{
this.xblack = xblack;
this.yblack = yblack;
blackhole = new ImageIcon(getClass().getResource(afbeelding2)).getImage();
}
public void tekenblackhole(Graphics g)
{
g.drawImage(blackhole,xblack,yblack,75,75,null);
}
public boolean gameover(Atoom atoom)
{
return ((atoom.xpositie +atoom.BREEDTE > this.xblack) && (atoom.xpositie < this.xblack + this.breedte) && (atoom.ypositie + atoom.HOOGTE>this.yblack && atoom.ypositie<this.yblack+this.hoogte )) ;
}
这就是我们在播放发生的类中调用的方法。
if (blackhole.gameover(atoom))
{
JOptionPane.showMessageDialog(null, "You're Goneee in The Black Hole !!");
venster.switchPanel();
}