Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.16 KB | None | 0 0
  1. package LetterLabels;
  2.  
  3. import java.awt.Dimension;
  4. import java.awt.GridLayout;
  5.  
  6. import javax.swing.JPanel;
  7.  
  8. import WordTwister.WordTwisterDisplay;
  9.  
  10. /**
  11. * LetterPanel JPanel class Used to store LetterLabels and handle the logic for
  12. * them, as there are two sets of LetterLabels used in the WordTwister applet
  13. * game.
  14. *
  15. * @author Ben's Mom
  16. * @author Connor Meade
  17. */
  18. @SuppressWarnings("serial")
  19. public class LetterPanel extends JPanel {
  20. protected LetterLabel[] theLetters; // Array of LetterLabels the LetterPanel
  21. // is holding
  22. private int count; // Number of LetterLabels that actually contain letters
  23. private int countAns;
  24. private char[] lettersUsed;
  25. public int[] xLabelCoord = new int[6];
  26. public int[] yLabelCoord = new int[6];
  27. private WordTwisterDisplay e;
  28.  
  29. /**
  30. * Default constructor for the LetterPanel
  31. */
  32. public LetterPanel(WordTwisterDisplay e) {
  33. lettersUsed = WordTwisterDisplay.lettersUsed();
  34. this.setLayout(new GridLayout(1, 6));
  35. theLetters = new LetterLabel[6];
  36. setPreferredSize(new Dimension(300, 50));
  37. for (int i = 0; i < theLetters.length; i++) {
  38. theLetters[i] = new LetterLabel(lettersUsed[i]);
  39. this.add(theLetters[i]);
  40. }
  41. count = 6;
  42. countAns = 0;
  43.  
  44. this.e = e;
  45. shuffleTiles();
  46. }
  47.  
  48. /**
  49. * Gets the coordinates of the letter panels
  50. */
  51. public void getCoordinates () {
  52. for (int i = 0; i < theLetters.length; i++) {
  53. xLabelCoord[i] = theLetters[i].getLocationOnScreen().x;
  54. yLabelCoord[i] = theLetters[i].getLocationOnScreen().y;
  55. }
  56. }
  57.  
  58. /**
  59. * Get the word formed by the LetterLabel components held in this
  60. * LetterPanel
  61. *
  62. * @return - String - the word formed
  63. */
  64. public String getWord() {
  65. String temp = "";
  66. for (int i = 0; i < theLetters.length; i++) {
  67. temp += theLetters[i].getText();
  68. }
  69. return temp.toString().trim();
  70. }
  71.  
  72. /**
  73. * Method to update the JPanel whenever changes are made to the tiles it
  74. * contains. It is possible to just use the regular JPanel methods to keep
  75. * track of the LetterLabels, but it uses an array of JComponents, which
  76. * means lots of typecasting.
  77. */
  78. public void resetTiles() {
  79. this.removeAll();
  80. for (int i = 0; i < theLetters.length; i++) {
  81. this.add(theLetters[i]);
  82. }
  83. validate();
  84. }
  85.  
  86. /**
  87. * Accessor method for the field count
  88. *
  89. * @return - int - count of LetterTiles in the LetterPanel
  90. */
  91. public int getCount() {
  92. return count;
  93. }
  94.  
  95. /**
  96. * Properly increments count from other classes
  97. *
  98. * @return
  99. * -Updated count integer
  100. */
  101. public int incrementCount() {
  102. count = count + 1;
  103. resetTiles();
  104. return count;
  105. }
  106.  
  107. /**
  108. * Method to shuffle the current LetterLabels that contain letters in the
  109. * LetterPanel
  110. *
  111. */
  112. public void shuffleTiles() {
  113. for (int i = 0; i < count; i++) {
  114. int temp = (int) (Math.random() * count);
  115. LetterLabel swap = theLetters[temp];
  116. theLetters[temp] = theLetters[i];
  117. theLetters[i] = swap;
  118. }
  119. resetTiles();
  120. }
  121.  
  122. /**
  123. * Initally sets up the answer tiles
  124. */
  125. public void answerTiles () {
  126. for (int i = 0; i < theLetters.length; i++) {
  127. theLetters[i] = new LetterLabel(' ');
  128. }
  129. resetTiles();
  130. }
  131.  
  132. /**
  133. * Adds a letter to the answer panel
  134. *
  135. * @param panelLetter
  136. * - Letter to move to the answer panel
  137. */
  138. public void addToAnswer (char panelLetter) {
  139. if (countAns < 6) {
  140. theLetters[countAns] = new LetterLabel(panelLetter);
  141. countAns = countAns + 1;
  142. count = count - 1;
  143.  
  144. resetTiles();
  145. }
  146. }
  147.  
  148. /**
  149. * When a letter gets moved to the answer letter panel, this method removes that letter from the
  150. * bottom letter panel
  151. *
  152. * @param panelIndex
  153. * - Index of the letter to remove
  154. */
  155. public void removeFromLetter (int panelIndex) {
  156. countAns = countAns + 1;
  157. count = count - 1;
  158.  
  159. for (;panelIndex < theLetters.length - 1; panelIndex++) {
  160. theLetters[panelIndex] = theLetters[panelIndex + 1];
  161. }
  162.  
  163. theLetters[5] = new LetterLabel(' ');
  164. resetTiles();
  165. }
  166.  
  167. /**
  168. * Clears all answer tiles and moves all letters back to the bottom panel
  169. */
  170. public void clearTiles() {
  171. countAns = 0;
  172. count = 6;
  173.  
  174. e.remove(e.theLetters);
  175. e.setupLetters();
  176. e.remove(e.theAnswerTiles);
  177. e.setupAnswerTiles();
  178. }
  179.  
  180. /**
  181. * When the submit button is pressed, the "Answer String" gets compared and updates
  182. * theMessage JLabel depending if the answer is correct
  183. *
  184. * @param submittedAnswer
  185. * - String containing the answer
  186. */
  187. public void submitAnswer(String submittedAnswer) {
  188. if(e.theWords.getText().contains(submittedAnswer)) {
  189. e.theMessage.setText(submittedAnswer + " exists!");
  190. clearTiles();
  191. }
  192. else
  193. e.theMessage.setText(submittedAnswer + " does not exist!");
  194. }
  195.  
  196. /**
  197. * If a backspace is caught in WordTwisterDisplay, the last letter to get added to the answer panel
  198. * gets moved back to the bottom letter panel
  199. *
  200. */
  201. public void backspaceAnswer () {
  202. int indexToAdd = 0;
  203.  
  204. for (;indexToAdd < theLetters.length; indexToAdd++) {
  205. if (e.theLetters.theLetters[indexToAdd].getText().charAt(0) == ' ')
  206. break;
  207. }
  208. e.theLetters.theLetters[indexToAdd] = e.theAnswerTiles.theLetters[countAns - 1];
  209. e.theAnswerTiles.theLetters[countAns - 1] = new LetterLabel(' ');
  210. countAns = countAns - 1;
  211. count = count + 1;
  212. resetTiles();
  213. e.theLetters.validate();
  214. }
  215.  
  216. /**
  217. * When an answer Panel is clicked that isn't a space, the letter clicked gets removed from the answer
  218. * panel then moved to the bottom letter panel
  219. *
  220. * @param index
  221. * - Index of the letter to be moved
  222. */
  223. public void removeAnswer (int index) {
  224. int indexToAdd = 0;
  225.  
  226. for (;indexToAdd < theLetters.length; indexToAdd++) {
  227. if (e.theLetters.theLetters[indexToAdd].getText().charAt(0) == ' ')
  228. break;
  229. }
  230. e.theLetters.theLetters[indexToAdd] = e.theAnswerTiles.theLetters[countAns - 2];
  231. e.theAnswerTiles.theLetters[index] = new LetterLabel(' ');
  232. countAns = countAns - 1;
  233. count = count - 1;
  234.  
  235. for (;index < theLetters.length - 1; index++) {
  236. theLetters[index] = theLetters[index + 1];
  237. }
  238.  
  239. theLetters[5] = new LetterLabel(' ');
  240. resetTiles();
  241. }
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement