Reputation: 1
I haven't looked at the code for this little game I made a while ago, and now all of the sudden the image won't move up when I click it?
I know the clicks are being called because the counter goes up. But the image won't move up. Any help is appreciated.
Batman class below
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;
}
}
}
Where everything is drawn here
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;
}
}
}
Upvotes: 0
Views: 211
Reputation: 285405
Your Batman mouseClicked(MouseEvent e)
method never gets called (if I'm wrong, please show me where). But even if it did, your MOUSE_CLICKED == BUTTON1
boolean check would always be false, since BUTTON1 and MOUSE_CLICKED are both constants and equal to different things, 1 and 500 respectively, and this will make sure that the if block never worked.
Upvotes: 3