Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package LetterLabels;
- import java.awt.Dimension;
- import java.awt.GridLayout;
- import javax.swing.JPanel;
- import WordTwister.WordTwisterDisplay;
- /**
- * LetterPanel JPanel class Used to store LetterLabels and handle the logic for
- * them, as there are two sets of LetterLabels used in the WordTwister applet
- * game.
- *
- * @author Ben's Mom
- * @author Connor Meade
- */
- @SuppressWarnings("serial")
- public class LetterPanel extends JPanel {
- protected LetterLabel[] theLetters; // Array of LetterLabels the LetterPanel
- // is holding
- private int count; // Number of LetterLabels that actually contain letters
- private int countAns;
- private char[] lettersUsed;
- public int[] xLabelCoord = new int[6];
- public int[] yLabelCoord = new int[6];
- private WordTwisterDisplay e;
- /**
- * Default constructor for the LetterPanel
- */
- public LetterPanel(WordTwisterDisplay e) {
- lettersUsed = WordTwisterDisplay.lettersUsed();
- this.setLayout(new GridLayout(1, 6));
- theLetters = new LetterLabel[6];
- setPreferredSize(new Dimension(300, 50));
- for (int i = 0; i < theLetters.length; i++) {
- theLetters[i] = new LetterLabel(lettersUsed[i]);
- this.add(theLetters[i]);
- }
- count = 6;
- countAns = 0;
- this.e = e;
- shuffleTiles();
- }
- /**
- * Gets the coordinates of the letter panels
- */
- public void getCoordinates () {
- for (int i = 0; i < theLetters.length; i++) {
- xLabelCoord[i] = theLetters[i].getLocationOnScreen().x;
- yLabelCoord[i] = theLetters[i].getLocationOnScreen().y;
- }
- }
- /**
- * Get the word formed by the LetterLabel components held in this
- * LetterPanel
- *
- * @return - String - the word formed
- */
- public String getWord() {
- String temp = "";
- for (int i = 0; i < theLetters.length; i++) {
- temp += theLetters[i].getText();
- }
- return temp.toString().trim();
- }
- /**
- * Method to update the JPanel whenever changes are made to the tiles it
- * contains. It is possible to just use the regular JPanel methods to keep
- * track of the LetterLabels, but it uses an array of JComponents, which
- * means lots of typecasting.
- */
- public void resetTiles() {
- this.removeAll();
- for (int i = 0; i < theLetters.length; i++) {
- this.add(theLetters[i]);
- }
- validate();
- }
- /**
- * Accessor method for the field count
- *
- * @return - int - count of LetterTiles in the LetterPanel
- */
- public int getCount() {
- return count;
- }
- /**
- * Properly increments count from other classes
- *
- * @return
- * -Updated count integer
- */
- public int incrementCount() {
- count = count + 1;
- resetTiles();
- return count;
- }
- /**
- * Method to shuffle the current LetterLabels that contain letters in the
- * LetterPanel
- *
- */
- public void shuffleTiles() {
- for (int i = 0; i < count; i++) {
- int temp = (int) (Math.random() * count);
- LetterLabel swap = theLetters[temp];
- theLetters[temp] = theLetters[i];
- theLetters[i] = swap;
- }
- resetTiles();
- }
- /**
- * Initally sets up the answer tiles
- */
- public void answerTiles () {
- for (int i = 0; i < theLetters.length; i++) {
- theLetters[i] = new LetterLabel(' ');
- }
- resetTiles();
- }
- /**
- * Adds a letter to the answer panel
- *
- * @param panelLetter
- * - Letter to move to the answer panel
- */
- public void addToAnswer (char panelLetter) {
- if (countAns < 6) {
- theLetters[countAns] = new LetterLabel(panelLetter);
- countAns = countAns + 1;
- count = count - 1;
- resetTiles();
- }
- }
- /**
- * When a letter gets moved to the answer letter panel, this method removes that letter from the
- * bottom letter panel
- *
- * @param panelIndex
- * - Index of the letter to remove
- */
- public void removeFromLetter (int panelIndex) {
- countAns = countAns + 1;
- count = count - 1;
- for (;panelIndex < theLetters.length - 1; panelIndex++) {
- theLetters[panelIndex] = theLetters[panelIndex + 1];
- }
- theLetters[5] = new LetterLabel(' ');
- resetTiles();
- }
- /**
- * Clears all answer tiles and moves all letters back to the bottom panel
- */
- public void clearTiles() {
- countAns = 0;
- count = 6;
- e.remove(e.theLetters);
- e.setupLetters();
- e.remove(e.theAnswerTiles);
- e.setupAnswerTiles();
- }
- /**
- * When the submit button is pressed, the "Answer String" gets compared and updates
- * theMessage JLabel depending if the answer is correct
- *
- * @param submittedAnswer
- * - String containing the answer
- */
- public void submitAnswer(String submittedAnswer) {
- if(e.theWords.getText().contains(submittedAnswer)) {
- e.theMessage.setText(submittedAnswer + " exists!");
- clearTiles();
- }
- else
- e.theMessage.setText(submittedAnswer + " does not exist!");
- }
- /**
- * If a backspace is caught in WordTwisterDisplay, the last letter to get added to the answer panel
- * gets moved back to the bottom letter panel
- *
- */
- public void backspaceAnswer () {
- int indexToAdd = 0;
- for (;indexToAdd < theLetters.length; indexToAdd++) {
- if (e.theLetters.theLetters[indexToAdd].getText().charAt(0) == ' ')
- break;
- }
- e.theLetters.theLetters[indexToAdd] = e.theAnswerTiles.theLetters[countAns - 1];
- e.theAnswerTiles.theLetters[countAns - 1] = new LetterLabel(' ');
- countAns = countAns - 1;
- count = count + 1;
- resetTiles();
- e.theLetters.validate();
- }
- /**
- * When an answer Panel is clicked that isn't a space, the letter clicked gets removed from the answer
- * panel then moved to the bottom letter panel
- *
- * @param index
- * - Index of the letter to be moved
- */
- public void removeAnswer (int index) {
- int indexToAdd = 0;
- for (;indexToAdd < theLetters.length; indexToAdd++) {
- if (e.theLetters.theLetters[indexToAdd].getText().charAt(0) == ' ')
- break;
- }
- e.theLetters.theLetters[indexToAdd] = e.theAnswerTiles.theLetters[countAns - 2];
- e.theAnswerTiles.theLetters[index] = new LetterLabel(' ');
- countAns = countAns - 1;
- count = count - 1;
- for (;index < theLetters.length - 1; index++) {
- theLetters[index] = theLetters[index + 1];
- }
- theLetters[5] = new LetterLabel(' ');
- resetTiles();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement