I' m writing a class in java that extends JPanel for a chess game. Its principal goal is to give to another class one point with the function getXY. When the method getXY is called it starts a new Thread and then it calls wait(). The other thread adds to the panel one mouse listener and when the user clicks on the panel it calculates the coordinates and the notify() the panel, but it gives one error.
This is the class:
int spacesX;
int spacesY;
int panelWidth;
int panelHeigth;
private int x;
private int y;
public PannelloInputGriglia(int x, int y,int heigth,ChessModel model)
{
this.setBounds(x, y, (heigth/model.getHeight()*model.getWidth()), heigth);
spacesX = model.getWidth();
spacesY = model.getHeight();
panelWidth = this.getWidth();
panelHeigth = this.getHeight();
}
public void setX(int x)
{
this.x = x;
}
public void setY(int y)
{
this.y = y;
}
public Point getXY()
{
InputThread th = new InputThread(this);
try {
this.wait();
} catch (InterruptedException ex) {
Logger.getLogger(PannelloInputGriglia.class.getName()).log(Level.SEVERE, null, ex);
}
return new Point(x, y);
}
private class InputThread extends Thread
{
PannelloInputGriglia pannello;
public InputThread(PannelloInputGriglia p)
{
pannello = p;
start();
}
@Override
public void run() {
pannello.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int sectorsX = panelWidth/spacesX;
int sectorsY = panelHeigth/spacesY;
setX(e.getX() / sectorsX);
setY(e.getY() / sectorsY);
super.mouseClicked(e);
pannello.notify();
}
});
}
}
What can I do?