Hello,

I'm currently a student in college and to get right to the point - I'm having a problem with figuring out the logic of making the program.

This is how I want my interface to look


TOP - MasterMind : Probably JLabel

CENTER - Where you set each guess to a color you want (Do I make a Panel for each spot? What should I make the spots out of? Buttons? JRadioButtons or just regular JButtons?)

WEST - Start, Retry, Solve buttons - I'll make this out of JButtons

EAST - Tells you what you did right or wrong. (I'm not sure what to use to show whats in the right spot or bad spot.)

SOUTH - Your color Choices (Make this out of JButtons? and change the background to its responding color?)

I'm working on the interface now.

Next problem I have is figuring out the logic of letting the user choose this color, place it over here.. and how the things check if you're right or wrong.

I've been thinking about this for a few days and can't seem to get my hands on what to do. Even the GUI placement confuses me. I'll update with code later on today if I get something going.

For now, I just ask for any direction to what I should do (not asking for code) My main goal here is to understand what I'm doing and not ask for handouts.

Thank you.

My education consist of: Tony Gaddis and GodFrey Muganda - Starting Out With Java Chapters: 1 - 11.


EDIT: Here's a handout by the teacher.

CPS141 –DeGuzman
Final Project
Due December 14, 2010

For your final project, you will program the game “MasterMind” game.

The rules:
The game is played using:

•a decoding board, with a shield at one end covering a row of four large holes, and ten additional rows containing four large holes next to a set of four small holes;

•code pegs of six different colors, which will be placed in the large holes on the board; and


•key pegs, some black and some white, which are flat-headed and smaller than the code pegs; they will be placed in the small holes on the board.
One player becomes the codemaker (the program), the other the codebreaker (the player). The codemaker randomly chooses a pattern of four code pegs. Duplicates are allowed, so the codemaker could even choose four code pegs of the same color. The chosen pattern is placed in the four holes covered by the shield, visible to the codemaker but not to the codebreaker.
The codebreaker tries to guess the pattern, in both order and color, within ten turns. Each guess is made by placing a row of code pegs on the decoding board. Once placed, the codemaker provides feedback by placing from zero to four key pegs in the small holes of the row with the guess. A black key peg is placed for each code peg from the guess which is correct in both color and position. A white peg indicates the existence of a correct color peg placed in the wrong position.
If there are duplicate colors in the guess, they cannot all be awarded a key peg unless they correspond to the same number of duplicate colors in the hidden code. For example, if the hidden code is red-red-blue-blue and the player guesses red-red-red-blue, the codemaker will award two black pegs for the two correct reds, nothing for the third red as there is not a third red in the code, and a black peg for the blue. No indication is given of the fact that the code also includes a second black.
Once feedback is provided, another guess is made; guesses and feedback continue to alternate until either the codebreaker guesses correctly, or ten incorrect guesses are made.

Modified from http://en.wikipedia.org/wiki/Mastermind_(board_game)#Gameplay_and_rules
Picture from http://thinks.com/java/mastermind/mastermind.htm

The Assignment:

You are to write a program to play the game with the computer acting as the codemaker. The color pattern should be randomly generated. You may choose which 6 colors you want to use from the color class. Use them throughout. You may also design your own screen and playing board but the same basic rules should be followed.

Your program should include:
• more than two class that you create (possible structure below)
• your own design for the layout
• a new Class not discussed in the classroom (suggestions below)
• proper use of the various classes and structures discussed throughout the semester
• features a player would expect such as rules, the ability to play again, etc. – You come up with the rest.

Hand in:
• Code to the dropbox including all necessary supporting files. Failure to put your code in the dropbox will result in a failing grade.
• Printouts of all classes created.
• A snapshot of your game.
• Your UML diagrams
• Standard report.

Possible structure:

Color Management Class -- The first class I created was a class that would manage the colors chosen. This class had a field which was a Color array to keep track of the 4 colors the codemaker randomly selected. It had a constructor, a method to select 4 new colors, a method to map a number to a color, a method to compare a given color array passed in as an argument to the class’ field containing the colors and returned the number correct, a method given a color array that matched colors in the arrays that were not in the correct location and a toString method.

Color Choice Panel Class– This is the class used to create the panels for the user to select the color from. Four of these objects will be used in the main GUI class. I used fields for the radio buttons and the button group. Methods included a constructor, a method to set the backgrounds to a neutral color, a method to set all 6 radio button backgrounds to a color that is passed in as a parameter and finally an ActionListener class was implemented to set background colors depending on the radio button selected.

MasterMind Class -- This class is where the game is played. It has all of the GUI objects required to create an interface. I suggest methods to build the different panels to make the code readable. To access what colors that were selected on the Color Choice Panels you can use the getBackground method. You will need to implement action listeners. I had 6 methods to build panels and two different ActionListeners implemented. These will vary greatly depending on your design.

The main class – Creates an instance of your GUI window. A single line of code. You may use an embedded main if you prefer.


New class possibilities (note the Color class is not considered new):
• Add an image when the player wins
• Add sound
• Use a dropdown menu to have players select colors
• Use a new layout manager not discussed in the book
• NOT sure if your idea is a “new” class. Just ask.

Here's some code I've worked on so far, My problem so far is figuring out why its only building one Panel when I want eight panels in the CENTER.

Classes: MasterMind - Launcer
Interface - Screen

MasterMind Code

package masterMind;

public class MasterMind {
	
	public static void main (String[] args)
	{
		Interface run = new Interface();
	}

}

Interface Code

package masterMind;

import java.awt.BorderLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Interface extends JFrame{
	
	final int NUM_CPANELS = 8;
	private JPanel[] choicePanel;
	final int NUM_CBUTTONS = 32;
	private JButton[] choiceButton;
	

	public Interface()
	{
		setTitle("MasterMind");
		setSize(800,400);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//		setLayout(new BorderLayout());
	
		buildPanels();
		
		setVisible(true);
	}
	public void buildPanels()
	{
		for (int i=0; i<8; i++)
		buildChooseSpotPanel();
	}
	public void buildChooseSpotPanel()
	{
		choiceButton = new JButton[NUM_CBUTTONS];
		for(int i=0; i<NUM_CBUTTONS; i++)
		{
			choiceButton[i] = new JButton(""+i);
		}
		
		
		choicePanel = new JPanel[NUM_CPANELS];
		for(int i=0; i<NUM_CPANELS; i++)
		{
			choicePanel[i] = new JPanel();
			add(choicePanel[i]);
			if(i == 0) // 0
			{
				choicePanel[i].add(choiceButton[i]);// 0
				choicePanel[i].add(choiceButton[i+1]);// 1
				choicePanel[i].add(choiceButton[i+2]);// 2
				choicePanel[i].add(choiceButton[i+3]);// 3
				
			}
			else if(i == 1) // 1
			{
				choicePanel[i].add(choiceButton[i+3]);// 4
				choicePanel[i].add(choiceButton[i+4]);// 5
				choicePanel[i].add(choiceButton[i+5]);// 6
				choicePanel[i].add(choiceButton[i+6]);// 7
			}
			else if(i == 2) // 2
			{
				choicePanel[i].add(choiceButton[i+6]);// 8
				choicePanel[i].add(choiceButton[i+7]);// 9
				choicePanel[i].add(choiceButton[i+8]);// 10
				choicePanel[i].add(choiceButton[i+9]);// 11
			}
			else if(i == 3) // 3
			{
				choicePanel[i].add(choiceButton[i+9]); // 12
				choicePanel[i].add(choiceButton[i+10]); // 13
				choicePanel[i].add(choiceButton[i+11]); // 14
				choicePanel[i].add(choiceButton[i+12]); // 15
			}
			else if(i == 4) // 4
			{
				choicePanel[i].add(choiceButton[i+12]); // 16
				choicePanel[i].add(choiceButton[i+13]); // 17
				choicePanel[i].add(choiceButton[i+14]); // 18
				choicePanel[i].add(choiceButton[i+15]); // 19
			}
			else if(i == 5) // 5
			{
				choicePanel[i].add(choiceButton[i+15]); // 20
				choicePanel[i].add(choiceButton[i+16]); // 21
				choicePanel[i].add(choiceButton[i+17]); // 22
				choicePanel[i].add(choiceButton[i+18]); // 23
			}
			else if(i == 6) // 6
			{
				choicePanel[i].add(choiceButton[i+18]); // 24
				choicePanel[i].add(choiceButton[i+19]); // 25
				choicePanel[i].add(choiceButton[i+20]); // 26
				choicePanel[i].add(choiceButton[i+21]); // 27
			}
			else // 7
			{
				choicePanel[i].add(choiceButton[i+21]); // 28
				choicePanel[i].add(choiceButton[i+22]); // 29
				choicePanel[i].add(choiceButton[i+23]); // 30
				choicePanel[i].add(choiceButton[i+24]); // 31
				
			}
		}
		
	}
}

Basically what I'm doing in the interface class is making for loops to create the panels and buttons then add them on.

Thanks in advance.

Any ideas on making the code more simple or efficient are welcomed.

A little confusing for me.

I would create one master panel for the JFrame and add it to frame.

From then on i would add additional panels to the master panel.

I would give the master panel some layout maybe border layout.

I would add one panel to the center of the master panel.

I would give this panel i add to the center an 8 by 4 grid layout, then my 32 buttons would be added to the panel that i put in the center which is a 8 by 4 grid.

centerPanel = new JPanel();
centerPanel.setLayout(new GridLayout(8,4));
// add my 32 buttons
// add centerPanel to center of master panel or maybe you want master panel to be its own little grid of some sort.
// bear in mind grid layouts mess up if you dont add exactly 8 by 4 different things they also size each of the 32 cells identically, like how a calculator looks.

this may not be what you want but hopefully it gives you some ideas.

I did this for my university like 3 years ago. The exact same problem. I still have it, for a price.

commented: Funny. +0

Looking at your original post, i would have the center 8 by 4 grid be for display.

I would make one panel for the center and would add two panels to it that sit left to right. one 8 by 4 ( show their guesses) and next to it an 8 by 1 which you add 8 panels of (2by2) for guess pegs. Input could just be a text box. type R G B B hit enter ( need an action listener or keyboard listener on text box) then color your buttons for guess in ( 8 by 32), and score and color the white and black pegs. net to text box maybe a label giving instructions on how to type guesses. just some ideas.

Mike

you might simply make it 8 by 5 with the 5 collum having a panel that has a 2 by 2 grid layout and 4 guess peg buttons added. this 5th area would be the same size as guesses making the 4 pegs 1/4 the size of a guess button. color then white and black.

Mike

Adam first, I would like to thank you for saying something about how the interface will mess up without proper gridlayout for that was exactly my problem. Secondly, sorry for confusing you Adam, maybe this will be more simple now that I commented on my code.

package masterMind;

import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Interface extends JFrame{
	
	final int NUM_CPANELS = 8;
	private JPanel[] choicePanel;
	final int NUM_CBUTTONS = 32;
	private JButton[] choiceButton;
	
/**
 * Constructor which will include how I want the screen to look
 * then go off into a method down below
 */
	public Interface()
	{
		setTitle("MasterMind");
		setSize(800,400);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(new BorderLayout(10,10));
		setLayout(new GridLayout(8,8));
		int i=1;
	
	/* I made a method to build my panels
	 * so it would be easier on the eyes
	 * for readability.
	 */

		buildPanels();
		
		setVisible(true);
	}
	/** 
	 * This method will build all Panels needed.
	 * Once again for readability.
	 */
	public void buildPanels()
	{
		buildChoicePanel();
	}
	/** In this method I use a for loop
	 * to create my buttons and Panels.
	 * I also use an if, else if statement
	 * to add buttons. Each panel has its
	 * own set of four buttons.
	 */
	public void buildChoicePanel()
	{
		choiceButton = new JButton[NUM_CBUTTONS];
		for(int i=0; i<NUM_CBUTTONS; i++)
		{
			choiceButton[i] = new JButton(""+i);
		}
		
		
		choicePanel = new JPanel[NUM_CPANELS];
		for(int i=0; i<NUM_CPANELS; i++)
		{
			choicePanel[i] = new JPanel();
			/* The comment with numbers are simply a guide
			 * to show which button would fall on this add.
			 */
			if(i == 0) // 0
			{
				choicePanel[i].add(choiceButton[i]);// 0
				choicePanel[i].add(choiceButton[i+1]);// 1
				choicePanel[i].add(choiceButton[i+2]);// 2
				choicePanel[i].add(choiceButton[i+3]);// 3
				add(choicePanel[i], BorderLayout.WEST);
				
			}
			else if(i == 1) // 1
			{
				choicePanel[i].add(choiceButton[i+3]);// 4
				choicePanel[i].add(choiceButton[i+4]);// 5
				choicePanel[i].add(choiceButton[i+5]);// 6
				choicePanel[i].add(choiceButton[i+6]);// 7
			}
			else if(i == 2) // 2
			{
				choicePanel[i].add(choiceButton[i+6]);// 8
				choicePanel[i].add(choiceButton[i+7]);// 9
				choicePanel[i].add(choiceButton[i+8]);// 10
				choicePanel[i].add(choiceButton[i+9]);// 11
			}
			else if(i == 3) // 3
			{
				choicePanel[i].add(choiceButton[i+9]); // 12
				choicePanel[i].add(choiceButton[i+10]); // 13
				choicePanel[i].add(choiceButton[i+11]); // 14
				choicePanel[i].add(choiceButton[i+12]); // 15
			}
			else if(i == 4) // 4
			{
				choicePanel[i].add(choiceButton[i+12]); // 16
				choicePanel[i].add(choiceButton[i+13]); // 17
				choicePanel[i].add(choiceButton[i+14]); // 18
				choicePanel[i].add(choiceButton[i+15]); // 19
			}
			else if(i == 5) // 5
			{
				choicePanel[i].add(choiceButton[i+15]); // 20
				choicePanel[i].add(choiceButton[i+16]); // 21
				choicePanel[i].add(choiceButton[i+17]); // 22
				choicePanel[i].add(choiceButton[i+18]); // 23
			}
			else if(i == 6) // 6
			{
				choicePanel[i].add(choiceButton[i+18]); // 24
				choicePanel[i].add(choiceButton[i+19]); // 25
				choicePanel[i].add(choiceButton[i+20]); // 26
				choicePanel[i].add(choiceButton[i+21]); // 27
			}
			else // 7
			{
				choicePanel[i].add(choiceButton[i+21]); // 28
				choicePanel[i].add(choiceButton[i+22]); // 29
				choicePanel[i].add(choiceButton[i+23]); // 30
				choicePanel[i].add(choiceButton[i+24]); // 31
				
			}
			add(choicePanel[i]);
		}
		
	}
}

So here's my vision...

-NORTH BORDER-
I'm going to add three buttons.

Start - Will commence the game.

Attempt - Will check the colors the user sent in.

Retry - Will restart the game to it's default.

-CENTER-

Will have eight panels, each panel holding its own set of four buttons

[] = panel
o = button

[o o o o]
[o o o o] etc...

-WEST-

I want to try to add a drop box for the user to select his color, since I have to use two new things never done before.

-EAST-

I want to put pegs that you were talking about Adam. To show weather the color is right and if its in a right spot.

-SOUTH-

Empty. (For now.)

-VISION-

NORTH
o-START o-ATTEMPT o-RETRY

WEST Drop box for color choice.

EAST Pegs to show if the color is in the right spot or not.

CENTER

32 Buttons

o o o o
o o o o etc.

_QUESTIONS/PROBLEMS I HAVE_
Q = question P = problem

Q. So is it wrong to build a panel for each set of buttons? Should I just build one panel and set a gridLayout for my 32 Buttons?

P. I'm trying to set my panels that I have created into the CENTER, but doesn't seem to be working... I know this because I tried setting a panel to the WEST and it still stood in the same spot.

choicePanel[i].add(choiceButton[i]);// 0
				choicePanel[i].add(choiceButton[i+1]);// 1
				choicePanel[i].add(choiceButton[i+2]);// 2
				choicePanel[i].add(choiceButton[i+3]);// 3
				add(choicePanel[i], BorderLayout.WEST);
				/* !PROBLEM!
				 * It won't go west when I tell it to. I'm testing it
				 * for WEST because I want to see if it works. I really want
				 * it to be in the center of the BorderLayout. How do I go about
				 * putting it where I want it to?
				 */

Thank You for your help.

I tried making 32 Buttons on one Panel. All buttons just line up next to each other.

Is there a way to put a Gridlayout into a panel?

public void buildChoicePanel()
	{
		choiceButton = new JButton[NUM_CBUTTONS];
		for(int i=0; i<NUM_CBUTTONS; i++)
		{
			choiceButton[i] = new JButton(""+i);
		}
		
		
		choicePanel = new JPanel();
		for(int i=0; i<4; i++)
		{
			choicePanel.add(choiceButton[i]); // 0, 1, 2, 3
			choicePanel.add(choiceButton[i+4]); // 4, 5, 6, 7
			choicePanel.add(choiceButton[i+8]); // 8, 9, 10, 11
			choicePanel.add(choiceButton[i+12]); // 12, 13, 14, 15
			choicePanel.add(choiceButton[i+16]); // 16, 17, 18, 19
			choicePanel.add(choiceButton[i+20]); // 20, 21, 22, 23
			choicePanel.add(choiceButton[i+24]); // 24, 25, 26, 27
			choicePanel.add(choiceButton[i+28]); // 28, 29, 30, 31
		}
		add(choicePanel);

P.s Is there a way to attach photos, to show how my screen looks?

you are setting the layout twice in the interface constructor.

the interface constructor is a frame. I don't even know if a frame can have a layout or how that works.

I always do

class myframe extends JFrame
{
myframe()
{
mypanel panel = new mypanel();
add(mypanel)// the single panel i add to frame. every other panel control is added to this panel.
}

class mypanel extends JPanel
{
// give the panel a layout setLayout etc

and your code with the problem goes right in here, in fact all your code can go in this panel class.

add(choicePanel[i], BorderLayout.WEST);

}// end the panel class


}// end the frame class

// the panel is a nested class. it can use any of the frames variables.

I don't actually use border layout myself. i found this tutorial that you may have allready,
http://download.oracle.com/javase/tutorial/uiswing/layout/border.html

That's actually pretty much what I use to do too. The reason why I'm trying this is because I'm looking over some demo code she did, and I was trying to follow that but I think I shouldn't follow a demo code cause it is what it is... a demo.

Perhaps you could explain what my professor did?

package RadioButtons;

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class RadioButtons extends JFrame{
	private JPanel meter;
	private JLabel meterLabel;
	private JTextField meterText;
	
	private JCheckBox roundBox;
	
	private JRadioButton miles, feet, inches;
	private JPanel choicePanel;
	
	private JLabel result;
	private JPanel space, space1;
	private JLabel emptySpace, emptySpace1;
	
	public RadioButtons(){
		setTitle("Metric Conversions");
		setSize(300,175);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(new BorderLayout(10,10));
		
		buildMeterPanel();
		buildChoicePanel();
		buildResultSpacePanels();

		add(meter, BorderLayout.NORTH);
		add(choicePanel, BorderLayout.CENTER);
		add(result, BorderLayout.SOUTH);
		add(roundBox, BorderLayout.WEST);
		add(space1, BorderLayout.EAST);
		
		setVisible(true);
	}
	
	public void buildPanels(){
		buildMeterPanel();
		buildChoicePanel();
		buildResultSpacePanels();
	}
	
	public void buildMeterPanel(){
		meter = new JPanel();
		meterLabel = new JLabel("Enter meters:  ");
		meterText = new JTextField(15);
		
		meter.add(meterLabel);
		meter.add(meterText);
	}
	
	public void buildChoicePanel(){
		miles = new JRadioButton("    Miles",true);
		feet = new JRadioButton("    Feet");
		inches = new JRadioButton("    Inches");
			
		ButtonGroup gp = new ButtonGroup();
		gp.add(miles);
		gp.add(feet);
		gp.add(inches);
		
		miles.addActionListener(new ButtonListener());
		feet.addActionListener(new ButtonListener());
		inches.addActionListener(new ButtonListener());
	
		choicePanel = new JPanel();
		choicePanel.setLayout(new GridLayout(3,1));
		choicePanel.setBorder(BorderFactory.createTitledBorder("Choices"));
		
		choicePanel.add(miles);
		choicePanel.add(feet);
		choicePanel.add(inches);
	
	}
	
	private class ButtonListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			if(e.getSource()==miles){
				double m = Double.parseDouble(meterText.getText());
				double miles = m*.6214/1000;
				result.setText(m+ "meters is "+miles+" miles.");
			}
			else if (e.getSource()==feet){
				double m = Double.parseDouble(meterText.getText());
				double ft = m*3281.0/1000;
				result.setText(m+ "meters is "+ft+" feet.");
			}
			else if (e.getSource()==inches){
				double m = Double.parseDouble(meterText.getText());
				double in = m*39.37;
				result.setText(m+ "meters is "+in+" inches.");
			}
		}
	}
	
	public void buildResultSpacePanels(){
		result = new JLabel("");
		
		roundBox = new JCheckBox("Check to round answer.",true);
		roundBox.addItemListener(new RoundCheckBox());
		space = new JPanel();
		emptySpace = new JLabel("               ");
		space.add(emptySpace);
		space1 = new JPanel();
		emptySpace1 = new JLabel("               ");
		space1.add(emptySpace1);
		
	}
	
	//This did not work in class because I
	//left the d off of itemStateChanged
	private class RoundCheckBox implements ItemListener{
		public void itemStateChanged(ItemEvent e){
			if(e.getSource()==roundBox)
				System.out.println("ans rounded/ or not");
		}

	
	}
}

If not, it doesn't matter just educational purposes. I'm going to make a separate class now. Actually, I have essays to tend to so check back tomorrow I should have updated something by then.

Anyone else please send me your feed back I don't mind different people helping out.

Thank you.

So, I created a new class for my panels and added it to my JFrame class.. Problem is I can't get the layout to look the way I want it to.

Class extending JFrame

package masterMind;

import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Interface extends JFrame {

	final int NUM_CPANELS = 8;
	private JPanel choicePanel;
	final int NUM_CBUTTONS = 32;
	private JButton[] choiceButton;

	/**
	 * Constructor which will include how I want the screen to look then go off
	 * into a method down below
	 */
	public Interface() {
		setTitle("MasterMind");
		setSize(800, 400);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(new BorderLayout());

		Panels p = new Panels();
		add(p); // !PROBLEM! I try add(p BorderLayout.CENTER); it says some sort
				// of syntax error and won't work
		setVisible(true);
	}

}

Class extending JPanel

package masterMind;

import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JPanel;

public class Panels extends JPanel {

	final int NUM_CPANELS = 8;
	private JPanel choicePanel;

	final int NUM_CBUTTONS = 32;
	private JButton[] choiceButton;

	public Panels() {
		setLayout(new GridLayout(8,8));
		buildPanels();

	}

	/**
	 * This method will build all Panels needed. Once again for readability.
	 */
	public void buildPanels() {
		buildChoicePanel();
	}

	/**
	 * In this method I use a for loop to create my buttons and Panels. I also
	 * use an if, else if statement to add buttons. Each panel has its own set
	 * of four buttons.
	 */
	public void buildChoicePanel() {
		setLayout(new GridLayout(8,8));
		//!PROBLEM! The Gridlayout just doesn't layout the buttons the way I want to
		// the way i want it to look is 8 rows 4 columns 
		choiceButton = new JButton[NUM_CBUTTONS];
		for (int i = 0; i < NUM_CBUTTONS; i++) {
			choiceButton[i] = new JButton("" + i);
		}

		choicePanel = new JPanel();
		for (int i = 0; i < 4; i++) {
			choicePanel.add(choiceButton[i]); // 0, 1, 2, 3
			choicePanel.add(choiceButton[i + 4]); // 4, 5, 6, 7
			choicePanel.add(choiceButton[i + 8]); // 8, 9, 10, 11
			choicePanel.add(choiceButton[i + 12]); // 12, 13, 14, 15
			choicePanel.add(choiceButton[i + 16]); // 16, 17, 18, 19
			choicePanel.add(choiceButton[i + 20]); // 20, 21, 22, 23
			choicePanel.add(choiceButton[i + 24]); // 24, 25, 26, 27
			choicePanel.add(choiceButton[i + 28]); // 28, 29, 30, 31
		}
		add(choicePanel);
}
}

The explanation is within the code with the //!PROBLEM!. I really need help with my layout this is probably my main problem, then I just need an idea of how to code out the logic of the buttons checking a,b,c but i=I'll get to that when its time.

setLayout(new GridLayout(8,8));
		//!PROBLEM! The Gridlayout just doesn't layout the buttons the way I want to
		// the way i want it to look is 8 rows 4 columns

Surely GridLayout(8,8) returns 8 rows, 8 columns? Unless you're skipping some columns, this might be your problem. Maybe this is in your code - if it is, I'm sorry, but I'm not hunting through a lot of layout code.

hopefully that was a typo

add(p BorderLayout.CENTER);

my recollection is there is a comma between p and BorderLayout.CENTER

Mike

Thanks adam that helped.

Surely GridLayout(8,8) returns 8 rows, 8 columns? Unless you're skipping some columns, this might be your problem

Hmm even tho the gridlayout is (8,8) It would just build into that format then leave the rest blank. Though I tested (8,4) it still didn't work. I still have the problem where the program is just leaving all my buttons in the NORTH.LAYOUT or so it seems because I have yet to build into other borders. I wish I could upload a picture to show what I'm talking about. Is there anything that other people do here to show pictures?

Anyway let me make my code shorter, I realize no one really wants to look through a bunch of code for no reason but sometimes I think the problem is somewhere else instead of where I point to but I'll try.

Should I add the Gridlayout in the constructor? Or in the method?

This Is the Constructor

public Panels() {
		setLayout(new GridLayout(8,4));
		buildPanels();

	}

This is the Method that will build the panel *I make a method cause the teacher wants easy reading code and for it to be clean*

public void buildChoicePanel() {
		setLayout(new GridLayout(8,4));
		//!PROBLEM! The Gridlayout just doesn't layout the buttons the way I want to
		// the way i want it to look is 8 rows 4 columns 
		choiceButton = new JButton[NUM_CBUTTONS];
		for (int i = 0; i < NUM_CBUTTONS; i++) {
			choiceButton[i] = new JButton("" + i);
		}

		choicePanel = new JPanel();
		for (int i = 0; i < 4; i++) {
			choicePanel.add(choiceButton[i]); // 0, 1, 2, 3
			choicePanel.add(choiceButton[i + 4]); // 4, 5, 6, 7
			choicePanel.add(choiceButton[i + 8]); // 8, 9, 10, 11
			choicePanel.add(choiceButton[i + 12]); // 12, 13, 14, 15
			choicePanel.add(choiceButton[i + 16]); // 16, 17, 18, 19
			choicePanel.add(choiceButton[i + 20]); // 20, 21, 22, 23
			choicePanel.add(choiceButton[i + 24]); // 24, 25, 26, 27
			choicePanel.add(choiceButton[i + 28]); // 28, 29, 30, 31
		}
		add(choicePanel);

I'm rereading chapter 7 in my book, and trying a bunch of things. I'll post if I find a solution. If anyone has any ideas I would truly appreciate it.

Thank you.

let's just look at this:

public void buildChoicePanel() {

setLayout(new GridLayout(8,4)); // this seems to apply to whatever panel is calling this method.

//!PROBLEM! The Gridlayout just doesn't layout the buttons the way I want to

// the way i want it to look is 8 rows 4 columns

choiceButton = new JButton[NUM_CBUTTONS];

for (int i = 0; i < NUM_CBUTTONS; i++) {

choiceButton[i] = new JButton("" + i);

}

choicePanel = new JPanel(); // choice panel has no layout assigned, you go on to add to it.

#...
commented: adam is patient, and willing to work with through your code to figure out the problem. Although he is willing to go the extent, I must insist on shorting your code, and helping him help you by focusing on a single problem one by one. Thank you Adam. +1

Adam how do i upgrade your reputation here.. because that fixed everything. I have to admit I failed to truly understand chapter 7 due to personal issues but I only blame myself.I simply thought you had to put it out there it will organize it. Ok so am I doing the borderlayout correctly in the JFrame class? *EDIT* I remember you saying you're not sure about the borderlayout but just by looking at it, am I initializing it correctly?

JFrame Class

public Interface() {
		setTitle("MasterMind");
		setSize(800, 400);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(new BorderLayout());

		Panels p = new Panels();
		add(p, BorderLayout.CENTER);
		setVisible(true);
	}

}

Thank you.

that looks right to add an instance of the Panels object to the center of what seems to be the highest level layout, the frame i think. As to reputation, I don't know exactly, its been the same for awhile not sure how people add a point :)

the test is of course how does it look when you run the program.

Mike

The program is coming along great but I'm having code writer block. I have a ComboBox to the west of my program for the user to choose what color they want to try to guess for (If I have time I will change this to pictures, and turn it into a picture guessing game.) The buttons are in the center as wanted. Now I need some sort of.. pegs to show the user what was guessed right and if it was in the right slot or etc. Heres a Url to show what I'm talking about.

http://thinks.com/java/mastermind/mastermind.htm

What you do is, pick a color then you start guessing from the bottom. If you got the color right a White peg will pop into the right box if its in the right spot a black peg will appear.

So Main question, What should I use to show this?

it's another 8 by 4 grid. this time with white and dark buttons. it needs to line up next to the 8by4 color choice grid. maybe instead of 8by 4 choice, make it 8 by 6 with cells 5 and 6 being panels of grids 2 vertical. so choice1 choice2 choice3 choice4 ( choice 5 which is scoring1/scoring2 and choice 6 chich is scoring3/scoring4 were the / denotes adding panels 2,1

mike

maybe instead of 8by 4 choice, make it 8 by 6 with cells 5 and 6 being panels of grids 2 vertical. so choice1 choice2 choice3 choice4 ( choice 5 which is scoring1/scoring2 and choice 6 chich is scoring3/scoring4 were the / denotes adding panels 2,1

mike

Hm, I'm not sure I quite understand. Instead of the current 8 by 4 - I should change to 8 by 6 with the last two for scoring 1/2 and 3/4?

I was thinking a new 8 by 4 lining up next to the current 8 by 4 but I don't know what to use... buttons, radios, labels - not quite sure

that's the idea. xxx xxx xxx xxx || xxx xxx
guesses lenght score lenght

disregard ||, buess you can use buttons for all.

of course the height of the score will be half that of a single guess. but thats sort of how mastermind looks.

Mike

Ok now i'm up to the action listener part.. What happens is when you pick your "Character" from the combo box, that's the character you're going to try to guess at whatever position (Button1 - 2 - 3 - 4)

I got the button to change to whatever character is chosen but I need to set it to the button that the person pressed.

For example

public void buildChoicePanel() 
	{
		snoopy = new ImageIcon("Snoopy.gif");
		charlie = new ImageIcon("Charlie.gif");
		woodstock = new ImageIcon ("Woodstock.gif");
		linus = new ImageIcon ("Linus.gif");		
		
		imgSnoopy = new JLabel(snoopy);
		
		characterBox = new JComboBox(characters);
		characterBox.addActionListener(new ComboBoxListener());
		choicePanel = new JPanel();	
		choicePanel.add(characterBox);
		choicePanel.add(imgSnoopy);
	}
	
	private class ComboBoxListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			 selection = (String) characterBox.getSelectedItem();
		}
	}
	/* This is the actionListener for the buttons - !PROBLEM!
	 * is that I can;t change the button that is pressed instead
	 * I tested it and I keep changing choiceButton[1]. What I need 
	 * to add is some sort of validation to check which button is presed
	 * and change only that button
	 * -- EDIT I fixed the woodstock problem.
	 */
	private class ButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			
			if(selection.equals("Snoopy"))
			{
				
				choiceButton[1].setIcon(snoopy);
			}
			if(selection.equals("Charlie Brown"))
			{
				
				choiceButton[1].setIcon(charlie);
			}
			if(selection.equals("Woodstock"))
			{
				
				choiceButton[1].setIcon(woodstock);
			}
			if(selection.equals("Linus"))
			{
				
				choiceButton[1].setIcon(linus);
			}
		}

Any ideas on checking which button was pressed?

Not sure i'm following.

Button listeners that detect clicks on button are not to bad to write.

for example i have one in some program:

OKButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent event)
				{
			 try
			 	{
				dispose();
		}// end try
			catch(Exception e)
			{}
		}
});

here is me setting up a listener on a jlabel, only its in an array so I add 40 of the listeners in a loop.

for(int tabindex = 0; tabindex < 40; tabindex ++ )
{
final int tabnumber = tabindex;

channelTabs[tabnumber].addMouseListener(new MouseAdapter() {
         public void mousePressed(MouseEvent e) {
			 if (e.getButton() == MouseEvent.BUTTON3)
			 makerightclickhappen(e, tabnumber);
			 else
			 makehappen(tabnumber);}
         public void mouseReleased(MouseEvent e) {}
         public void mouseEntered (MouseEvent me) {}
         public void mouseExited (MouseEvent me) {}
         public void mouseClicked (MouseEvent me) {}  });
}//end for

so that would tell you which button is pressed? like in the loop example makehappen(n) or something is called and you know its button n. or when you add a listen to one button you always know exactly what button it was.

Mike

Yes, what i'm trying to do is the for loop I suppose for an Array of buttons so it can figure out which button was pressed choiceButton.. I'm trying to imply your code on to mine but its saying something about only accepting a final int


for (int i=0; i <NUM_CBUTTONS; i++)
	{
//!PROBLEM! This is where it gives me the problem the red line shows under ButtonListener and says Illegal Modifier for the local class ButtonListener; only abstract of final is permitted.
		private class ButtonListener implements ActionListener
		{
			public void actionPerformed(ActionEvent e)
			{
			
				if(selection.equals("Snoopy"))
					{
				
						choiceButton[1].setIcon(snoopy);
					}
				if(selection.equals("Charlie Brown"))
					{
				
					choiceButton[1].setIcon(charlie);
			}
			if(selection.equals("Woodstock"))
			{
				
				choiceButton[1].setIcon(woodstock);
			}
			if(selection.equals("Linus"))
			{
				
				choiceButton[1].setIcon(linus);
			}
		}
	}
	}

you have to kind of get a feel for what both my examples were doing.

channelTabs[tabnumber].addMouseListener(new MouseAdapter()

you dont have an external class, its all contained between addMouseListener( everything is in here);

this is a different technique for adding listeners.

also note in my loop with an array example, the loop variable i assign to something final

final int tabnumber = tabindex;

so that the internal method within add() can read it.

MIke

After long work of digging into my brain that's not so intelligent... I finally figured out another way to do this for an array of buttons....

here's what i did

private class ButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
		for(int i=0; i<NUM_CBUTTONS; i++)
		{
			if(selection.equals("Snoopy") && choiceButton[i].equals(e.getSource()))
			{
				choiceButton[i].setIcon(snoopy);
			}
			if(selection.equals("Charlie Brown"))
			{
				
				choiceButton[i].setIcon(charlie);
			}
			if(selection.equals("Woodstock"))
			{
				
				choiceButton[i].setIcon(woodstock);
			}
			if(selection.equals("Linus"))
			{
				
				choiceButton[i].setIcon(linus);
			}
		}
		}
		}

Adam, your for loop pointed me in the right direction. I'll be posting my next phase in a few.

Thank you.

Ok so I have a menubar and in that menubar I have a New button. I want that New button to generate a new pattern. Basically when New is clicked I want the game to start.

So when new is clicked it will randomly generate

Charlie Brown - Snoopy - Snoopy - Linus.. and that will be what the user has to guess in order to win

Anyway point is, I don't know how to start I was thinking of using random class to generate this.. Any ideas?

Alright I want to do this one by one.

I have a menubar with a File>New.

When New is clicked I want the program to generate a new guess solution meaning

Red,Blue,Blue,Red For Win.

But with pictures.

I dont really know how to start this listener.. I was thinking of doing Random... I'm not sure I'm really confused on starting this code. Anyone give your ideas so I can some how get started.. it's due this Tuesday and I really want to get the game running by today.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.