-1

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?

Greps
  • 1
  • 2

1 Answers1

0

There's no call to getXY () in your code.

But from the code you posted I think you should:

  • Read a bit of documentation on Threads/Runnable (Jenkov tutorial). Think about the impact of the call to the start () method in your InputThread constructor.
  • Read how the Swing GUI works with thread processing (Swing multithreading)
user392486
  • 195
  • 4