|
CodebreakerUi.java |
|
|
|
|
|
|
|
|
|
|
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!
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();
}
}
}
CodebreakerUi.java Add member variables of type: Color colorSelected; Updated method initComponents() where it is instantiating the...
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 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 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 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 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 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 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 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 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 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...