Question

CodebreakerUi.java Add member variables of type: Color colorSelected; Updated method initComponents() where it is instantiating the...

CodebreakerUi.java

  1. Add member variables of type:
    1. Color colorSelected;
  1. Updated method initComponents() where it is instantiating the RoundButtons for the 1D array on the JPanel displaying the codebreaker’s colors; it should add the following
    1. Add action listener ColorListener to each round button
  1. Updated method initComponents() where it is instantiating the RoundButtons for the 2D array on the JPanel for the codebreaker’s attempt; it should add the following
    1. Add client property “row” to each RoundButton set to the current iteration of the outer loop
    2. Add action listener AttemptListener to each round button
  1. Write an inner class to create an ActionListener that is registered to each of the colored buttons on the Codebreaker’s JPanel that displays the eight available colors; it should
    1. Explicitly type cast the ActionEvent object to an instance of RoundButton; all method getSource() on the ActionEvent object
    2. Set member variable colorSelected to the color stored in the client property “color” of the clicked RoundButton instance; it will need to be explicitly type cast to class Color
  1. Write an inner class to create an ActionListener that is registered to each of the RoundButtons in the 2D array on the Codebreaker’s JPanel that displays the codebreaker’s attempt; it should
    1. Explicitly type cast the ActionEvent object to an instance of RoundButton
    2. Check if the member variable codebreakerAttempt in class Codebreaker already contains the selected color; if false
      1. Set the background color of the RoundButton instance to the value stored in member variable colorSelected
      2. Add to member variable codebreakerAttempt in class Codebreaker’s the value stored in member variable colorSelected
    3. Check if the size of member variable codebreakerAttempt in class Codebreaker is equal to the maximum number of pegs (i.e. 4); if true
      1. Create a variable of type int set equal to the client property “row” of the clicked RoundButton instance; it will need to be explicitly type cast to primitive data type int
      2. Call method enableDisableButtons() passing the int variable from step i. above as an argument
  1. Write private method enableDisableButtons() to disable the last row of the codebreaker’s attempt and enable the next row of the codebreaker’s attempt; it should
    1. Have a return type of void
    2. Receive one parameter of type int representing the current row
    3. Loop through the passed in row value, for each column
      1. Disable the current row and associated columns
      2. If the current row isn’t zero (0), then enable the next row and associated columns

Code so far:

package userinterface;

import constants.Constants;
import core.Codebreaker;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JButton;

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author kwhiting
*/
public class CodebreakerUi
{
private JPanel codebreakerAttempt;
private JPanel codebreakerColors;
private RoundButton[] buttons;
private RoundButton[][] attempts;
private Color colorSelected;
  
private Codebreaker codebreaker;
  
public CodebreakerUi(Codebreaker codebreaker)
{
this.codebreaker = codebreaker;
initComponents();
}
  
private void initComponents()
{
initCodebreakerColors();
initCodebreakerAttempt();
}
  
  
private void initCodebreakerColors()
{
codebreakerColors = new JPanel();
codebreakerColors.setBorder(BorderFactory.createTitledBorder("Codebreaker Colors"));
codebreakerColors.setMinimumSize(new Dimension(200, 65));
codebreakerColors.setPreferredSize(new Dimension(200,65));
  
// instantiate the Array with the size
buttons = new RoundButton[Constants.COLORS];
  
// counter for enhanced for loop
int index = 0;
  
// put client properties on the buttons so we
// know which one it is
for (RoundButton button : buttons)
{          
// create the buttons
button = new RoundButton();
Color color = Constants.codeColors.get(index);
button.setBackground(color);
button.putClientProperty("color", color);
  
// set the tooltip
if(color == Color.BLUE)
button.setToolTipText("BLUE");
else if(color == Color.BLACK)
button.setToolTipText("BLACK");
else if(color == Color.GREEN)
button.setToolTipText("GREEN");
else if(color == Color.ORANGE)
button.setToolTipText("ORANGE");
else if(color == Color.PINK)
button.setToolTipText("PINK");
else if(color == Color.RED)
button.setToolTipText("RED");
else if(color == Color.YELLOW)
button.setToolTipText("YELLOW");
else if(color == Color.WHITE)
button.setToolTipText("WHITE");
  
// add an ActionListener

  
// add button to JPanel using FlowLayout
codebreakerColors.add(button);
  
// increment the counter
index++;
}  
}
  
private void initCodebreakerAttempt()
{
codebreakerAttempt = new JPanel();
codebreakerAttempt.setBorder(BorderFactory.createTitledBorder("Codebreaker Attempt"));
codebreakerAttempt.setMinimumSize(new Dimension(100, 100));
codebreakerAttempt.setPreferredSize(new Dimension(100, 100));
  
// set the layout manager to use GridLayout
codebreakerAttempt.setLayout(new GridLayout(Constants.MAX_ATTEMPTS, Constants.MAX_PEGS));
  
// instantiate the Array with the size
attempts = new RoundButton[Constants.MAX_ATTEMPTS][Constants.MAX_PEGS];
addActionListener(ColorListener);
  
// create the array of JButtons for the code breaker's attempts
for (int row = 0; row < Constants.MAX_ATTEMPTS; row ++)
{          
for(int col = 0; col < Constants.MAX_PEGS; col++)
{
// create the buttons
attempts[row][col] = new RoundButton();
  
// if it isn't the last row then disable the button
if(row != (Constants.MAX_ATTEMPTS - 1))
attempts[row][col].setEnabled(false);
  
// add the button to the UI
codebreakerAttempt.add(attempts[row][col]);
}
}
}

/**
* @return the codebreakerAttempt
*/
public JPanel getCodebreakerAttempt()
{
return codebreakerAttempt;
}

/**
* @return the codebreakerColors
*/
public JPanel getCodebreakerColors()
{
return codebreakerColors;
}

No worries. Just by Sunday would be awesome.

Thank you!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

package userinterface;

import constants.Constants;
import static constants.Constants.MAX_PEGS;
import core.Codebreaker;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.JPanel;


public class CodebreakerUi
{
    private JPanel codebreakerAttempt;
    private JPanel codebreakerColors;
    private RoundButton[] buttons;
    private RoundButton[][] attempts;
  
    Color colorSelected;
  
    private Codebreaker codebreaker;
  
    public CodebreakerUi(Codebreaker codebreaker){
        this.codebreaker = codebreaker;
        initComponents();
    }
  
    private void initComponents(){
        initCodebreakerColors();
        initCodebreakerAttempt();
    }
  
    private void initCodebreakerColors(){
        codebreakerColors = new JPanel();
        codebreakerColors.setBorder(BorderFactory.createTitledBorder("Codebreaker Colors"));
        codebreakerColors.setMinimumSize(new Dimension(200, 65));
        codebreakerColors.setPreferredSize(new Dimension(200,65));

        buttons = new RoundButton[Constants.COLORS];

        int counter = 0;

        for (RoundButton button : buttons) {          

            button = new RoundButton();
            Color color = Constants.codeColors.get(counter);
            button.setBackground(color);
            button.putClientProperty("color", color);

            if(color == Color.BLUE)
                button.setToolTipText("BLUE");
            else if(color == Color.BLACK)
                button.setToolTipText("BLACK");
            else if(color == Color.GREEN)
                button.setToolTipText("GREEN");
            else if(color == Color.ORANGE)
                button.setToolTipText("ORANGE");
            else if(color == Color.PINK)
                button.setToolTipText("PINK");
            else if(color == Color.RED)
                button.setToolTipText("RED");          
            else if(color == Color.YELLOW)
                button.setToolTipText("YELLOW");
            else if(color == Color.WHITE)
                button.setToolTipText("WHITE");

         button.addActionListener(new ColoredButtons());

            codebreakerColors.add(button);

            counter++;
          
        }  

    }
  
    private void initCodebreakerAttempt(){
        codebreakerAttempt = new JPanel();
        codebreakerAttempt.setBorder(BorderFactory.createTitledBorder("Codebreaker Attempt"));
        codebreakerAttempt.setMinimumSize(new Dimension(100, 100));
        codebreakerAttempt.setPreferredSize(new Dimension(100, 100));
      
        // set the layout manager to use GridLayout
        codebreakerAttempt.setLayout(new GridLayout(Constants.MAX_ATTEMPTS, Constants.MAX_PEGS));
      
        // instantiate the Array with the size
        attempts = new RoundButton[Constants.MAX_ATTEMPTS][Constants.MAX_PEGS];
      
        // create the array of JButtons for the code breaker's attempts
        for (int row = 0; row < Constants.MAX_ATTEMPTS; row ++) {          
            for(int col = 0; col < Constants.MAX_PEGS; col++){
                // create the buttons
                attempts[row][col] = new RoundButton();
                attempts[row][col].putClientProperty("row", row);
                attempts[row][col].addActionListener(new RoundButtons());
              
             
                if(row != (Constants.MAX_ATTEMPTS - 1))
                    attempts[row][col].setEnabled(false);
              
          
                codebreakerAttempt.add(attempts[row][col]);
              
            }
           

        }
          
    }

    /**
     * @return the codebreakerAttempt
     */
    public JPanel getCodebreakerAttempt()
    {
      
        return codebreakerAttempt;
    }

    /**
     * @return the codebreakerColors
     */
    public JPanel getCodebreakerColors()
    {
      
        return codebreakerColors;
    }
  
    private class ColoredButtons implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent ae) {
            RoundButton sourceButton = (RoundButton)ae.getSource();
          
            colorSelected = (Color)sourceButton.getClientProperty("color");
        }
    }
    private class RoundButtons implements ActionListener{
      
        @Override
      
        public void actionPerformed(ActionEvent be) {
            RoundButton placementButton = (RoundButton)be.getSource();
            if(codebreaker.getCodebreakerAttempt().contains(colorSelected) == false){
                placementButton.setBackground(colorSelected);
                codebreaker.getCodebreakerAttempt().add(colorSelected);
            }
            if(codebreaker.getCodebreakerAttempt().size() == 4){
             
             
                int row = (int) placementButton.getClientProperty("row");
                 enableDisableButtons(row);
            }
          
          
        }
      
    }
    public void clearBoard() {
         for(int i=0;i<10;i++){
            for(int j=0; j<4;j++){
            attempts[i][j].setBackground(null);
          
                if (i == 9)
                    attempts[i][j].setEnabled(true);
                else
                    attempts[i][j].setEnabled(false);
        }
    }
       codebreaker.removeAll();
      
    }
    private void enableDisableButtons(int row){
     
      for (int col = 0; col < Constants.MAX_PEGS; col++){
          attempts[row][col].setEnabled(false);
          if(row != 0) {
              attempts[row - 1][col].setEnabled(true);
            
          }
          codebreaker.removeAll();
        
      }

    }
  
}

Add a comment
Know the answer?
Add Answer to:
CodebreakerUi.java Add member variables of type: Color colorSelected; Updated method initComponents() where it is instantiating the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Java. CodebreakerUi.java Add member variables of type: Color colorSelected; Updated method initComponents() where it is...

    In Java. CodebreakerUi.java Add member variables of type: Color colorSelected; Updated method initComponents() where it is instantiating the RoundButtons for the 1D array on the JPanel displaying the codebreaker’s colors; it should add the following Add action listener ColorListener to each round button Updated method initComponents() where it is instantiating the RoundButtons for the 2D array on the JPanel for the codebreaker’s attempt; it should add the following Add client property “row” to each RoundButton set to the current iteration...

  • I have been messing around with java lately and I have made this calculator. Is there...

    I have been messing around with java lately and I have made this calculator. Is there any way that I would be able to get a different sound to play on each different button 1 - 9 like a nokia phone? Thank you. *SOURCE CODE* import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; class Calculator extends JFrame { private final Font BIGGER_FONT = new Font("monspaced",Font.PLAIN, 20); private JTextField textfield; private boolean number = true; private String equalOp = "="; private...

  • Create a class called Flower. Add one member variables color. Add only one getter function for the color. The get function returns color. Implement a constructor that expects color value and assigns it to the member variable. Create a subclass of Flower

    Create a class called Flower. Add one member variables color. Add only one getter function for the color. The get function returns color. Implement a constructor that expects color value and assigns it to the member variable. Create a subclass of Flower named Rose. The Rose class has one member variable name.  Add a constructor which expects color and  name. Pass color to the base constructor and set name to it's member variable.Write a program that has an array of ...

  • I have currently a functional Java progam with a gui. Its a simple table of contacts...

    I have currently a functional Java progam with a gui. Its a simple table of contacts with 3 buttons: add, remove, and edit. Right now the buttons are in the program but they do not work yet. I need the buttons to actually be able to add, remove, or edit things on the table. Thanks so much. Here is the working code so far: //PersonTableModel.java import java.util.List; import javax.swing.table.AbstractTableModel; public class PersonTableModel extends AbstractTableModel {     private static final int...

  • I got this programming its used to highlight text, but i couldn't remove highlight again from...

    I got this programming its used to highlight text, but i couldn't remove highlight again from the text. i need someone to write me code that removes text highlight here is my code import java.awt.*; import java.awt.event.*; import java.util.Map; import java.util.HashMap; import javax.swing.*; import javax.swing.text.*; public class TextHighlight {    private JTextArea textarea;    private JComboBox colorbox;    private JTextField top;    private String[] colorOptions = { "GRAY", "YELLOW", "RED", "CYAN", "ORANGE", "PINK" };    private Highlighter.HighlightPainter grayPainter;    private...

  • Add a timer to the lightbulb ON/OFF program such that the bulb stays on for 10...

    Add a timer to the lightbulb ON/OFF program such that the bulb stays on for 10 seconds after the ON button is pushed and then goes off.   Put a new label on the lightbulb panel that counts the number of seconds the bulb is on and displays the number of seconds as it counts up from 0 to 10. THIS IS A JAVA PROGRAM. The image above is what it should look like. // Demonstrates mnemonics and tool tips. //********************************************************************...

  • Please fix and test my code so that all the buttons and function are working in...

    Please fix and test my code so that all the buttons and function are working in the Sudoku. CODE: SudokuLayout.java: import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BorderFactory; import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JTextField; public class SudokuLayout extends JFrame{ JTextArea txtArea;//for output JPanel gridPanel,butPanel;//panels for sudoku bord and buttons JButton hint,reset,solve,newPuzzel;//declare buttons JComboBox difficultyBox; SudokuLayout() { setName("Sudoku Bord");//set name of frame // setTitle("Sudoku Bord");//set...

  • I tried to add exception handling to my program so that when the user puts in...

    I tried to add exception handling to my program so that when the user puts in an amount of change that is not the integer data type, the code will catch it and tell them to try again, but my try/catch isnt working. I'm not sure how to fix this problem. JAVA code pls package Chapter9; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class GUIlab extends JFrame implements ActionListener {    private static final int WIDTH = 300;    private...

  • It's not showing anything. I've tried many things but only a small window pops up. Below...

    It's not showing anything. I've tried many things but only a small window pops up. Below is my code so far. It's creating a Mortgage Calculator with GUI. Having user input the principle, term, and rate. ************************************************************************************************************************** import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Mortgage extends JFrame implements ActionListener { //declaring components private JTextField principle, rate, terms, result; private JButton calculate, reset, exit;       //constructor    public Mortgage() { setLayout(new GridLayout(0,2)); //adding labels and textfields add(new JLabel("Principle:")); principle...

  • Create a class called Flower. Add one member variables color. Add only one getter function for the color. The get function returns color. Implement a constructor that expects color value and assigns it to the member variable. Create a subclass of Flower

    Create a class called Flower. Add one member variables Color. Add only one getter function for the color. The get function returns color. Implement a constructor that expects color value and assigns it to the member variable. Create a subclass of Flower named Rose. The Rose class has one member variable name. Add a constructor which expects color and name. Pass color to the base constructor and set name to it's member variable.Write a program that has an array of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT