我没有看过我之前制作的这个小游戏的代码,现在突然间我点击它时图像不会向上移动?
我知道点击是因为计数器上升而被调用的。但图像不会向上移动。任何帮助表示赞赏。
下面的蝙蝠侠课
package Clicky;
import java.awt.Image;
import javax.swing.ImageIcon;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class Batman
{
private String batz = "batbomb.png";
private int x;
private int y;
private Image image;
private boolean visible;
public Batman()
{
ImageIcon ii = new ImageIcon(this.getClass().getResource(batz));
image = ii.getImage();
visible = true;
x = 145;
y = 620;
}
public int getX() { return x; }
public int getY() { return y; }
public Image getImage() { return image; }
public boolean isVisible()
{
return visible;
}
public void setVisible(boolean visible)
{
this.visible = visible;
}
public void mouseClicked(MouseEvent e)
{
int button = MouseEvent.BUTTON1;
if (button == MouseEvent.MOUSE_CLICKED)
{
y -= 1;
}
}
}
这里绘制了所有内容
package Clicky;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JPanel;
import javax.swing.Timer;
import java.awt.Image;
import javax.swing.ImageIcon;
public class Clicky extends JPanel implements ActionListener
{
private Batman bat;
private Timer timer;
private int clicks = 0;
private boolean visible;
public Clicky() {
addMouseListener(new TAdapter());
setFocusable(true);
setBackground(Color.BLACK);
setDoubleBuffered(true);
timer = new Timer(5, this);
timer.start();
bat = new Batman();
}
public int getClickCount()
{
return clicks;
}
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D)g;
g.setColor(Color.WHITE);
g2d.drawString("Clicks: " + getClickCount(), 10, 50);
g2d.drawRect(150, 70, 200, 600);
g2d.drawImage(bat.getImage(), bat.getX(), bat.getY(), this);
Toolkit.getDefaultToolkit().sync();
g.dispose();
}
public void actionPerformed(ActionEvent e) {
repaint();
}
private class TAdapter extends MouseAdapter
{
public void mouseClicked(MouseEvent e)
{
e.getClickCount();
clicks = clicks + 1;
}
}
}
答案 0 :(得分:3)
你的蝙蝠侠mouseClicked(MouseEvent e)
方法永远不会被调用(如果我错了,请告诉我在哪里)。但即使它确实如此,你的MOUSE_CLICKED == BUTTON1
布尔检查也总是假的,因为BUTTON1和MOUSE_CLICKED都是常量并且分别等于不同的东西,1和500,这将确保if块永远不会工作。 / p>