Question

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

In Java.

CodebreakerUi.java

  1. Add member variables of type:
    1. Color colorSelected;
  2. 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
  3. 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

Current Code

CodebreakerUi.java

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;

public class CodebreakerUi
{
private JPanel codebreakerAttempt;
private JPanel codebreakerColors;
private RoundButton[] buttons;
private RoundButton[][] attempts;
  
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];
  
// 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;
}
}

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

CodebreakerUi.java

package userinterface;

import constants.Constants;
import core.Codebreaker;
import java.awt.Color;
import java.awt.Dimension;

import java.awt.event.*;  
import java.awt.GridLayout;
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;

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

button.addActionListener(new ColorListener());

// add button to JPanel using FlowLayout
codebreakerColors.add(button);
  
// increment the counter
index++;
}  
}


class ColorListener implements ActionListener()

{  

public void actionPerformed(ActionEvent e)

{  

   colorSelected=(RoundButton)e.getBackground();

colorSelected.toString();

    }      

}

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", Integer.valueOf(row));  

attempts[row][col].addActionListener(new AttemptListener());


// 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]);
}
}
}

class AttemptListener implements ActionListener()

{  

    public void actionPerformed(ActionEvent e)

{

Textfield txt=new textField();

   object obj = (RoundButton)e.GetClientProperty(Row);

txt.setText(obj.toString());

    }  

}

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

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

Add a comment
Know the answer?
Add Answer to:
In Java. CodebreakerUi.java Add member variables of type: Color colorSelected; Updated method initComponents() where it is...
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
  • CodebreakerUi.java Add member variables of type: Color colorSelected; Updated method initComponents() where it is instantiating the...

    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 of the...

  • 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 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...

  • Basic button tracking Deliverables Updated files for app6.java myJFrame6.java myJPanel6.java student.java Contents You can start with...

    Basic button tracking Deliverables Updated files for app6.java myJFrame6.java myJPanel6.java student.java Contents You can start with the files available here. public class app6 { public static void main(String args[]) {     myJFrame6 mjf = new myJFrame6(); } } import java.awt.*; import javax.swing.*; import java.awt.event.*; public class myJFrame6 extends JFrame {    myJPanel6 p6;    public myJFrame6 ()    {        super ("My First Frame"); //------------------------------------------------------ // Create components: Jpanel, JLabel and JTextField        p6 = new myJPanel6(); //------------------------------------------------------...

  • 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...

  • 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...

  • Debug this java and post the corrected code. /* Creates a simple JPanel with a single...

    Debug this java and post the corrected code. /* Creates a simple JPanel with a single "quit" JButton * that ends the program when clicked. * There are 3 errors, one won't prevent compile, you have to read comments. */ import javax.swing.*; import java.awt.*; import java.awt.event.*; public class QuitIt extends JFrame {     public QuitIt() {         startIt(); //Create everything and start the listener     }     public final void startIt() {        //Create the JPanel        JPanel myPanel =...

  • 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. //********************************************************************...

  • JAVA problem: Upgrade and extend the previous programs Draw.java and DrawCanvas.java used in the class to...

    JAVA problem: Upgrade and extend the previous programs Draw.java and DrawCanvas.java used in the class to maintain a list of shapes drawn as follows: Replace and upgrade all the AWT components used in the programs to the corresponding Swing components, including Frame, Button, Label, Choice, and Panel. Add a JList to the left of the canvas to record and display the list of shapes that have been drawn on the canvas. Each entry in the list should contain the name...

  • JAVA Hello I am trying to add a menu to my Java code if someone can...

    JAVA Hello I am trying to add a menu to my Java code if someone can help me I would really appreacite it thank you. I found a java menu code but I dont know how to incorporate it to my code this is the java menu code that i found. import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ButtonGroup; import javax.swing.JCheckBoxMenuItem; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JRadioButtonMenuItem; public class MenuExp extends JFrame { public MenuExp() { setTitle("Menu Example");...

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