Java poker implementation
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Button.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright (c) Chris 'MD87' Smith, 2007-2008. All rights reserved.
  3. *
  4. * This code may not be redistributed without prior permission from the
  5. * aforementioned copyright holder(s).
  6. */
  7. package com.md87.cardgame.ui;
  8. import com.md87.cardgame.controllers.HumanPlayer;
  9. import java.awt.Color;
  10. import java.awt.Graphics2D;
  11. import java.awt.Point;
  12. import java.awt.Rectangle;
  13. import java.awt.geom.Rectangle2D;
  14. /**
  15. *
  16. * @author chris
  17. */
  18. public class Button {
  19. private final Color activeColour;
  20. private final Color inactiveColour;
  21. public static enum TYPE {
  22. SLOW {
  23. public boolean test(int speed, HumanPlayer waitPlayer,
  24. HumanPlayer player, boolean canRaise, boolean canFold,
  25. HumanPlayer discardPlayer) {
  26. return speed == GameWindow.SPEED_SLOW;
  27. }
  28. },
  29. NORMAL {
  30. public boolean test(int speed, HumanPlayer waitPlayer,
  31. HumanPlayer player, boolean canRaise, boolean canFold,
  32. HumanPlayer discardPlayer) {
  33. return speed == GameWindow.SPEED_NORMAL;
  34. }
  35. },
  36. FAST {
  37. public boolean test(int speed, HumanPlayer waitPlayer,
  38. HumanPlayer player, boolean canRaise, boolean canFold,
  39. HumanPlayer discardPlayer) {
  40. return speed == GameWindow.SPEED_FAST;
  41. }
  42. },
  43. CONTINUE {
  44. public boolean test(int speed, HumanPlayer waitPlayer,
  45. HumanPlayer player, boolean canRaise, boolean canFold,
  46. HumanPlayer discardPlayer) {
  47. return waitPlayer != null;
  48. }
  49. },
  50. CHECK {
  51. public boolean test(int speed, HumanPlayer waitPlayer,
  52. HumanPlayer player, boolean canRaise, boolean canFold,
  53. HumanPlayer discardPlayer) {
  54. return player != null;
  55. }
  56. },
  57. OPEN {
  58. public boolean test(int speed, HumanPlayer waitPlayer,
  59. HumanPlayer player, boolean canRaise, boolean canFold,
  60. HumanPlayer discardPlayer) {
  61. return player != null && canRaise;
  62. }
  63. },
  64. FOLD {
  65. public boolean test(int speed, HumanPlayer waitPlayer,
  66. HumanPlayer player, boolean canRaise, boolean canFold,
  67. HumanPlayer discardPlayer) {
  68. return player != null && canFold;
  69. }
  70. },
  71. DISCARD {
  72. public boolean test(int speed, HumanPlayer waitPlayer,
  73. HumanPlayer player, boolean canRaise, boolean canFold,
  74. HumanPlayer discardPlayer) {
  75. return discardPlayer != null;
  76. }
  77. };
  78. public abstract boolean test(int speed, HumanPlayer waitPlayer,
  79. HumanPlayer player, boolean canRaise, boolean canFold,
  80. HumanPlayer discardPlayer);
  81. }
  82. private String text;
  83. private Rectangle bounds;
  84. public Button(final String text, final Rectangle bounds, final Color base) {
  85. this.text = text;
  86. this.bounds = bounds;
  87. this.inactiveColour = base.brighter();
  88. this.activeColour = this.inactiveColour.brighter();
  89. }
  90. public String getText() {
  91. return text;
  92. }
  93. public Rectangle getBounds() {
  94. return bounds;
  95. }
  96. public boolean contains(final Point p) {
  97. return bounds.contains(p);
  98. }
  99. public void render(final Graphics2D g, final boolean enabled) {
  100. g.setColor(enabled ? activeColour : inactiveColour);
  101. g.drawRoundRect(bounds.x, bounds.y, bounds.width, bounds.height, 5, 5);
  102. renderTextCentered(g);
  103. }
  104. /**
  105. * Renders the specified text horizontally centered within the bounds.
  106. *
  107. * @param g The graphics object to render to
  108. */
  109. protected void renderTextCentered(final Graphics2D g) {
  110. final Rectangle2D actualBounds = g.getFontMetrics().getStringBounds(text, g);
  111. final int yOffset = bounds.y + (int) actualBounds.getHeight()
  112. +(bounds.height - (int) actualBounds.getHeight()) / 2;
  113. final int xOffset = bounds.x + (bounds.width - (int) actualBounds.getWidth()) / 2;
  114. g.drawString(text, xOffset, yOffset - 2);
  115. }
  116. }