Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public final class Panel extends JPanel implements MouseListener {
- private int boardTopLeftX;
- private int boardTopLeftY;
- private Boolean userClicked;
- private int userClickCol;
- public Panel() {
- super();
- this.boardTopLeftX = cellSize;
- this.boardTopLeftY = 2*cellSize;
- this.userClicked = false;
- }
- @Override
- public void paintComponent(Graphics g) {
- g.setColor(COLOR);
- int verticalLineLength = cellSize * ROW_CONSTANT;
- int horizontalLineLength = cellSize * COL_CONSTANT;
- //GUI draw vertical lines
- for (int colNum = 0; colNum <= COL_CONSTANT; colNum++) {
- int xCoord = boardTopLeftX + colNum * cellSize;
- g.drawLine(xCoord, boardTopLeftY, xCoord, boardTopLeftY + verticalLineLength);
- }
- //GUI draw horizontal lines
- for (int rowNum = 0; rowNum <= ROW_CONSTANT; rowNum++) {
- int yCoord = boardTopLeftY + rowNum * cellSize;
- g.drawLine(boardTopLeftX, yCoord, boardTopLeftX + horizontalLineLength, yCoord);
- }
- }
- @Override
- public void mouseClicked(MouseEvent e) {
- synchronized (userClicked) {
- userClickCol = getColOfClick(e.getX(), e.getY());
- userClicked.notifyAll();
- }
- }
- /**
- * Returns column where user clicked above board
- */
- private int getColOfClick(int x, int y) {
- int pieceDiameter = cellSize - BORDER*2;
- for (int col = 0; col < COL_CONSTANT; col++) {
- int distBetweenClickAndCenter =
- euclidDistance(x, y,
- boardTopLeftX + (int) ((col + 0.5) * cellSize),
- boardTopLeftY - (int) (0.5 * cellSize));
- if (distBetweenClickAndCenter < pieceDiameter / 2) {
- return col;
- }
- }
- return -1;
- }
- public int getUserClickCol() {
- return userClickCol;
- }
- @Override
- public void mouseEntered(MouseEvent e) {}
- @Override
- public void mouseExited(MouseEvent e) {}
- @Override
- public void mousePressed(MouseEvent e) {}
- @Override
- public void mouseReleased(MouseEvent e) {}
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement