Advertisement
Guest User

Untitled

a guest
May 21st, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. public final class Panel extends JPanel implements MouseListener {
  2.  
  3. private int boardTopLeftX;
  4. private int boardTopLeftY;
  5.  
  6. private Boolean userClicked;
  7. private int userClickCol;
  8.  
  9. public Panel() {
  10. super();
  11. this.boardTopLeftX = cellSize;
  12. this.boardTopLeftY = 2*cellSize;
  13. this.userClicked = false;
  14. }
  15.  
  16. @Override
  17. public void paintComponent(Graphics g) {
  18. g.setColor(COLOR);
  19.  
  20. int verticalLineLength = cellSize * ROW_CONSTANT;
  21. int horizontalLineLength = cellSize * COL_CONSTANT;
  22.  
  23. //GUI draw vertical lines
  24. for (int colNum = 0; colNum <= COL_CONSTANT; colNum++) {
  25. int xCoord = boardTopLeftX + colNum * cellSize;
  26. g.drawLine(xCoord, boardTopLeftY, xCoord, boardTopLeftY + verticalLineLength);
  27. }
  28.  
  29. //GUI draw horizontal lines
  30. for (int rowNum = 0; rowNum <= ROW_CONSTANT; rowNum++) {
  31. int yCoord = boardTopLeftY + rowNum * cellSize;
  32. g.drawLine(boardTopLeftX, yCoord, boardTopLeftX + horizontalLineLength, yCoord);
  33. }
  34. }
  35.  
  36. @Override
  37. public void mouseClicked(MouseEvent e) {
  38. synchronized (userClicked) {
  39. userClickCol = getColOfClick(e.getX(), e.getY());
  40. userClicked.notifyAll();
  41. }
  42. }
  43.  
  44. /**
  45. * Returns column where user clicked above board
  46. */
  47. private int getColOfClick(int x, int y) {
  48. int pieceDiameter = cellSize - BORDER*2;
  49.  
  50. for (int col = 0; col < COL_CONSTANT; col++) {
  51. int distBetweenClickAndCenter =
  52. euclidDistance(x, y,
  53. boardTopLeftX + (int) ((col + 0.5) * cellSize),
  54. boardTopLeftY - (int) (0.5 * cellSize));
  55. if (distBetweenClickAndCenter < pieceDiameter / 2) {
  56. return col;
  57. }
  58. }
  59. return -1;
  60. }
  61.  
  62. public int getUserClickCol() {
  63. return userClickCol;
  64. }
  65.  
  66. @Override
  67. public void mouseEntered(MouseEvent e) {}
  68.  
  69. @Override
  70. public void mouseExited(MouseEvent e) {}
  71.  
  72. @Override
  73. public void mousePressed(MouseEvent e) {}
  74.  
  75. @Override
  76. public void mouseReleased(MouseEvent e) {}
  77. }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement