Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


#ActualPsychotikRabbit

Posted 17 December 2012 - 11:56 AM

I've already worked on something similar, it's just mathematics. MouseEvent can provide you the mouse position of the click (MouseEvent.getX(), MouseEvent.getY()).
We're lucky that you are making a chess game because of chessboard. To get the selected square location you can simply loop through all rows and column and check if the position of the mouse is within the square.

I wrote a little method which would find a square , I didn't test it but I think it can help you out.

	private final int NUMBER_OF_ROWS = 8;
	private final int NUMBER_OF_COLUMNS = 8;
	private final int SQUARE_SIZE = 32;

	public Square getSquareAt(int x, int y) {
		for(int col = 0; col < NUMBER_OF_COLUMNS ; ++col ) {
			for(int row = 0; row < NUMBER_OF_ROWS; ++row ) {
				int squareX = row * SQUARE_SIZE;
				int squareY = col * SQUARE_SIZE;
				Rectangle rectangle = new Rectangle(squareX,squareY, SQUARE_SIZE, SQUARE_SIZE);
			
				if(rectangle.contains(x, y)) {
					return chessBoard[col][row];
				}
			}
		}
		return null;
	}

I let java.awt detect if the coordinate of the mouse is within the bounds of the square.

Edit: That will work if there is one unique mouse listener listening on the chessboard panel. (container of all squares).

#4PsychotikRabbit

Posted 17 December 2012 - 11:56 AM

I've already worked on something similar, it's just mathematics. MouseEvent can provide you the mouse position of the click (MouseEvent.getX(), MouseEvent.getY()).
We're lucky that you are making a chess game because of chessboard. To get the selected square location you can simply loop through all rows and column and check if the position of the mouse is within the square.

I wrote a little method which would find a square , I didn't test it but I think it can help you out.

	private final int NUMBER_OF_ROWS = 8;
	private final int NUMBER_OF_COLUMNS = 8;
	private final int SQUARE_SIZE = 32;

	public Square getSquareAt(int x, int y) {
		for(int col = 0; col < NUMBER_OF_COLUMNS ; ++col ) {
			for(int row = 0; row < NUMBER_OF_ROWS; ++row ) {
				int squareX = row * SQUARE_SIZE;
				int squareY = col * SQUARE_SIZE;
				Rectangle rectangle = new Rectangle(squareX,squareY, SQUARE_SIZE, SQUARE_SIZE);
			
				if(rectangle.contains(x, y)) {
					return chessBoard[col][row];
				}
			}
		}
		return null;
	}

I let java.awt detect if the coordinate of the mouse is within the bounds of the square.

Edit: That will work if the is one unique mouse listener listening on the chessboard panel. (container of all squares).

#3PsychotikRabbit

Posted 17 December 2012 - 11:54 AM

I've already worked on something similar, it's just mathematics. MouseEvent can provide you the mouse position of the click (MouseEvent.getX(), MouseEvent.getY()).
We're lucky that you are making a chess game because of chessboard. To get the selected square location you can simply loop through all rows and column and check if the position of the mouse is within the square.

I wrote a little method which would find a square , I didn't test it but I think it can help you out.

    private final int NUMBER_OF_ROWS = 8;
    private final int NUMBER_OF_COLUMNS = 8;
    private final int SQUARE_SIZE = 32;

    public Square getSquareAt(int x, int y) {
	    for(int col = 0; col < NUMBER_OF_COLUMNS ; ++col ) {
		    for(int row = 0; row < NUMBER_OF_ROWS; ++row ) {
			    int squareX = row * SQUARE_SIZE;
			    int squareY = col * SQUARE_SIZE;
			    Rectangle rectangle = new Rectangle(squareX,squareY, SQUARE_SIZE, SQUARE_SIZE);
			 
			    if(rectangle.contains(x, y)) {
				    return chessBoard[col][row];
			    }
		    }
	    }
	    return null;
    }

I let java.awt detect if the coordinate of the mouse is within the bounds of the square.

#2PsychotikRabbit

Posted 17 December 2012 - 11:53 AM

I've already worked on something similar, it's just mathematics. MouseEvent can provide you the mouse position of the click (MouseEvent.getX(), MouseEvent.getY()).
We're lucky that you are making a chess game because of chessboard. To get the selected square location you can simply loop through all rows and column and check if the position of the mouse is within the square.

I wrote a little method which would find a square , I didn't test it but I think it can help you out.

	private final int NUMBER_OF_ROWS = 8;
	private final int NUMBER_OF_COLUMNS = 8;
    private final int SQUARE_SIZE = 32;

	public Square getSquareAt(int x, int y) {
		for(int col = 0; col < NUMBER_OF_COLUMNS ; ++col ) {
			for(int row = 0; row < NUMBER_OF_ROWS; ++row ) {
				int squareX = row * SQUARE_SIZE;
				int squareY = col * SQUARE_SIZE;
				Rectangle rectangle = new Rectangle(squareX,squareY, SQUARE_SIZE, SQUARE_SIZE);
			  
				if(rectangle.contains(x, y)) {
					return chessBoard[col][row];
				}
			}
		}
		return null;
	}

I let java.awt detect if the coordinate of the mouse is within the bounds of the square.

#1PsychotikRabbit

Posted 17 December 2012 - 11:48 AM

I've already worked on something similar, it's just mathematics. MouseEvent can provide you the mouse position of the click (MouseEvent.getX(), MouseEvent.getY()).
We're lucky that you are making a chess game because of chessboard. To get the selected square location you can simply loop through all rows and column and check if the position of the mouse is within the square.

I wrote a little method which would find a square , I didn't test it but I think it can help you out.

    private final int NUMBER_OF_ROWS = 8;
    private final int NUMBER_OF_COLUMNS = 8;


    public Square getSquareAt(int x, int y) {
	    for(int col = 0; col < NUMBER_OF_COLUMNS ; ++col ) {
		    for(int row = 0; row < NUMBER_OF_ROWS; ++row ) {
			    int squareWidth = chessBoard[col][row].getWidth;
			    int squareX = row * squareWidth;
			    int squareY = col * squareWidth;
			    Rectangle rectangle = new Rectangle(squareX,squareY, squareWidth, squareWidth);
			   
			    if(rectangle.contains(x, y)) {
				    return chessBoard[col][row];
			    }
		    }
	    }
	    return null;
    }

I let java.awt detect if the coordinate of the mouse is within the bounds of the square.

PARTNERS