hello i have a class that implements the MouseListener
this :
package clickOfGod;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class Control implements MouseListener {
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
if (e.getButton() == e.BUTTON1) {
System.out.println("pressed");
}
}
public void mouseReleased(MouseEvent e) {
}
}
and i want to make it work in my JPanel class
this :
package clickOfGod;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
public class Board extends JPanel implements Runnable {
// declaration
private Ship ship;
private Control mouse;
private Thread loop;
// constructor
public Board() {
setBackground(Color.gray);
setDoubleBuffered(true);
setFocusable(true);
addMouseListener(mouse);
init();
}
// initialisation
private void init() {
ship = new Ship();
loop = new Thread(this);
loop.start();
mouse = new Control();
}
// painting
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.drawRect(ship.getX(), ship.getY(), ship.getW(), ship.getH());
}
public void play() {
System.out.println("test");
}
// game loop
public void run() {
while (true) {
repaint();
play();
try {
loop.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
i don’t know what’s wrong with it,
i created the class,create a new variable of that class, initialize it and then add a mouse listener to the JPanel
what’s wrong ???
thank you