SRL Archive - Help and Support

JAVA GUI help needed!


[XoL]

2013-02-26 19:46:09

Hi guys back again need some more assistance… Like always please do not move out of the SRL members section.

So my assignment this time is to make the game FOURSIGHT, no AI just two player.

GOAL: Write a GUI-based program to provide an electronic game board for the game Foursight. This is a strategy board game sold by Lakeside games (and has recently shown up in several mobile apps). Your program should allow two people to play and make sure their moves are legal. The assignment does not include the ability of the program to play against a human.
TEAMS: You may work in teams of two persons or by yourself on this assignment.

THE GAME: The game is played by two players on a 4 by 4 game board. Each player has 8 green markers, 4 white markers, and 4 black markers. A turn consists of putting one of your markers on the game board. It is only legal to place a green marker in an empty space and only legal to place a white or black marker on a space which is currently green (i.e. "on top of" a green marker).

Although each player has both white and black markers, one player is designated the "white" player and the other the "black" player. At the end of the game (when all markers have been played) the winner is the player who has the most "three in a row"s of their color on the board.

METHOD: We'll talk about several of the details in class. In particular, you'll probably want an array of 16 JButtons arranged as the board. How these buttons react to being pressed will depend on what color the button is and whether or not the current player is holding a colored piece. Each player should have a set of three buttons (green, white, and black) that allow them to select a piece to place on the board. The program will have to keep track of how many pieces of each color the player has.

The real heart of the program lies in the ActionListeners. For each type of button, you need to figure out how it should react when pressed at various times and then code those cases into the actionPerformed method for the corresponding listener. In the case of the board buttons, you'll often want to change the background color of the button to indicate a new piece has been placed there. In order to do this, you need to call the setBackground() method on that button. So, remember that if e is an ActionEvent, e.getSource() will return the actual Java object which generated the event. In this case that will be a JButton and you'll need to cast the result of getSource to be a JButton object:

JButton b = (JButton)(e.getSource());
The game should indicate whose turn it is and only allow piece selections from that player's buttons. If a button is pressed at the wrong time, the program should play a sound and go back to waiting for button presses. For instance, clicking on a board button without selecting a piece to move, or selecting a piece when you are out of that color should produce error beeps.


Right so I created a half working gui, I would like some help creating a panel in the middle and a 16 button grid in that panel..
Here is what mine looks like so far
Here is the code I have written so far, any help would be highly appreciated, I am just lost beyond lost at this point I tried everything…

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

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.FlowLayout;
import java.awt.Color;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Grid extends JFrame implements ActionListener
{
private JButton panel;
public static final int WIDTH = 500;
public static final int HEIGHT = 500;
public static void main (String [] args)
{
Grid gui = new Grid( );
gui.setVisible(true);
}

public Grid( )
{
super("FourSight");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//------------------------------------
setLayout(new BorderLayout( ));
JPanel fourBoard = new JPanel( );
//fourBoard = new JPanel();
GridLayout gl = new GridLayout(4, 4);
fourBoard.setLayout(gl);
//-------------------------------------
add(fourBoard, BorderLayout.CENTER);
JButton buttonPanel = new JButton( );
buttonPanel.setBackground(Color.LIGHT_GRAY);
JButton buttonPanel2 = new JButton( );
buttonPanel2.setBackground(Color.LIGHT_GRAY);
//-------------------------------------
buttonPanel.setLayout(new BorderLayout( ));
JButton blackButton = new JButton("Black");
blackButton.setBackground(Color.BLACK);
blackButton.addActionListener(this);
buttonPanel.add(blackButton, BorderLayout.NORTH);
//-------------------------------------
JButton whiteButton = new JButton("White");
whiteButton.setBackground(Color.WHITE);
whiteButton.addActionListener(this);
buttonPanel.add(whiteButton, BorderLayout.CENTER);
//-------------------------------------
JButton greenButton = new JButton("Green");
greenButton.setBackground(Color.GREEN);
greenButton.addActionListener(this);
buttonPanel.add(greenButton, BorderLayout.SOUTH);
//-------------------------------------
//Player Two
//-------------------------------------
buttonPanel2.setLayout(new BorderLayout( ));
JButton blackButton2 = new JButton("Black");
blackButton2.setBackground(Color.BLACK);
blackButton2.addActionListener(this);
buttonPanel2.add(blackButton2, BorderLayout.NORTH);
//-------------------------------------
JButton whiteButton2 = new JButton("White");
whiteButton2.setBackground(Color.WHITE);
whiteButton2.addActionListener(this);
buttonPanel2.add(whiteButton2, BorderLayout.CENTER);
//-------------------------------------
JButton greenButton2 = new JButton("Green");
greenButton2.setBackground(Color.GREEN);
greenButton2.addActionListener(this);
buttonPanel2.add(greenButton2,BorderLayout.SOUTH);
//-------------------------------------
add(buttonPanel, BorderLayout.WEST);
add(buttonPanel2, BorderLayout.EAST);
//-------------------------------------
int i = 0;

// for (int a = 0; a > 4; a++)
// {
// for(int b = 0; b<4; b++, i++)
// {
// fourBoard.add(new JButton(), (a,b));
// }
// }
}

public void actionPerformed(ActionEvent e)
{
String buttonString = e.getActionCommand( );
if (buttonString.equals("White"))
button.setBackground(Color.WHITE);
else if (buttonString.equals("Black"))
panel.setBackground(Color.BLACK);
else if (buttonString.equals("Green"))
panel.setBackground(Color.GREEN);
else
System.out.println("We have experienced a problem");
}
}

NKN

2013-02-26 20:40:51

JFrame Designer

Cheats. :3

[XoL]

2013-02-27 11:58:54

What is that?

NKN

2013-02-27 16:26:40

From: [XoL]
What is that?


It's a thing you can make Java GUI's with.
You select the component, and drag it to where you want it.
Just like our form designer.

J J

2013-02-27 16:29:27

for (int a = 0; a > 4; a++)
// {
// for(int b = 0; b<4; b++, i++)
// {
// fourBoard.add(new JButton(), (a,b));
// }
// }
Not sure what you are doing there.. You also need to add ActionListeners so you really want to define your stuff as private variables on top and initialize in the constructor.

Then make some private classes for ActionListeners to handle events.

I would really work with multiple classes for stuff like this, I'll make one with a panel containing the buttons now.

EDIT: First version done

package org.obduro.java;

import javax.swing.JFrame;

public class FourSightGUI extends JFrame {

private ButtonPanel buttonPanel;

public FourSightGUI(){
super("FourSight");
setSize(500, 500);

buttonPanel = new ButtonPanel();
add(buttonPanel);

setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}

public static void main(String[] args){
System.out.println("Created game!");
FourSightGUI game = new FourSightGUI();

}

}

package org.obduro.java;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

public class ButtonPanel extends JPanel {

private final static int WIDTH = 4, HEIGHT = 4;

private ClickListener clickListener;
private JButton[][] buttons;

public ButtonPanel(){
setLayout(new GridLayout(WIDTH, HEIGHT));
buttons = new JButton[WIDTH][HEIGHT];
clickListener = new ClickListener();

for(int x = 0; x < WIDTH; x++){
for(int y = 0; y < HEIGHT; y++){
buttons[x][y] = new JButton("[" + x + "][" + y + "]");
buttons[x][y].addActionListener(clickListener);
add(buttons[x][y]);
}
}
}

private class ClickListener implements ActionListener{

@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Button has been pressed");

JButton button = (JButton) e.getSource();
for(int x = 0; x < WIDTH; x++){
for(int y = 0; y < HEIGHT; y++){
if(button.equals(buttons[x][y])){
System.out.println("Button[" + x + "][" + y + "] has been clicked!");
}
}
}


}

}


}

[XoL]

2013-02-27 18:33:15

Alright so I started using the GUI thing but I realized it would make things really messy with 16 buttons right up in the middle, I was told to use an array and make them all react the same way? Here is the code I have now


import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.GridLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import java.awt.CardLayout;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import javax.swing.BoxLayout;
import java.awt.FlowLayout;
import javax.swing.JLabel;


public class FourSight extends JFrame {

private JPanel contentPane;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FourSight frame = new FourSight();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public FourSight() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 500, 378);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);

JPanel LeftPanel = new JPanel();
contentPane.add(LeftPanel, BorderLayout.WEST);
GridBagLayout gbl_LeftPanel = new GridBagLayout();
gbl_LeftPanel.columnWidths = new int[]{61, 0};
gbl_LeftPanel.rowHeights = new int[]{99, 23, 0, 0, 0, 0, 0, 0};
gbl_LeftPanel.columnWeights = new double[]{0.0, Double.MIN_VALUE};
gbl_LeftPanel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
LeftPanel.setLayout(gbl_LeftPanel);

JLabel lblPlayerOne = new JLabel("Player One");
GridBagConstraints gbc_lblPlayerOne = new GridBagConstraints();
gbc_lblPlayerOne.insets = new Insets(0, 0, 5, 0);
gbc_lblPlayerOne.gridx = 0;
gbc_lblPlayerOne.gridy = 0;
LeftPanel.add(lblPlayerOne, gbc_lblPlayerOne);

JButton greenP1 = new JButton("Green");
GridBagConstraints gbc_greenP1 = new GridBagConstraints();
gbc_greenP1.anchor = GridBagConstraints.WEST;
gbc_greenP1.insets = new Insets(0, 0, 5, 0);
gbc_greenP1.gridx = 0;
gbc_greenP1.gridy = 1;
LeftPanel.add(greenP1, gbc_greenP1);

JButton whiteP1 = new JButton("White");
GridBagConstraints gbc_whiteP1 = new GridBagConstraints();
gbc_whiteP1.anchor = GridBagConstraints.WEST;
gbc_whiteP1.insets = new Insets(0, 0, 5, 0);
gbc_whiteP1.gridx = 0;
gbc_whiteP1.gridy = 2;
LeftPanel.add(whiteP1, gbc_whiteP1);

JButton blackP1 = new JButton("Black ");
GridBagConstraints gbc_blackP1 = new GridBagConstraints();
gbc_blackP1.insets = new Insets(0, 0, 5, 0);
gbc_blackP1.anchor = GridBagConstraints.WEST;
gbc_blackP1.gridx = 0;
gbc_blackP1.gridy = 3;
LeftPanel.add(blackP1, gbc_blackP1);

JPanel RightPanel = new JPanel();
contentPane.add(RightPanel, BorderLayout.EAST);
GridBagLayout gbl_RightPanel = new GridBagLayout();
gbl_RightPanel.columnWidths = new int[]{65, 0};
gbl_RightPanel.rowHeights = new int[]{103, 0, 0, 0, 0, 0, 0, 0};
gbl_RightPanel.columnWeights = new double[]{0.0, Double.MIN_VALUE};
gbl_RightPanel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
RightPanel.setLayout(gbl_RightPanel);

JLabel lblPlayerTwo = new JLabel("Player Two");
GridBagConstraints gbc_lblPlayerTwo = new GridBagConstraints();
gbc_lblPlayerTwo.insets = new Insets(0, 0, 5, 0);
gbc_lblPlayerTwo.gridx = 0;
gbc_lblPlayerTwo.gridy = 0;
RightPanel.add(lblPlayerTwo, gbc_lblPlayerTwo);

JButton greenP2 = new JButton("Green");
GridBagConstraints gbc_greenP2 = new GridBagConstraints();
gbc_greenP2.insets = new Insets(0, 0, 5, 0);
gbc_greenP2.anchor = GridBagConstraints.NORTHWEST;
gbc_greenP2.gridx = 0;
gbc_greenP2.gridy = 1;
RightPanel.add(greenP2, gbc_greenP2);

JButton whiteP2 = new JButton("White");
GridBagConstraints gbc_whiteP2 = new GridBagConstraints();
gbc_whiteP2.insets = new Insets(0, 0, 5, 0);
gbc_whiteP2.anchor = GridBagConstraints.NORTHWEST;
gbc_whiteP2.gridx = 0;
gbc_whiteP2.gridy = 2;
RightPanel.add(whiteP2, gbc_whiteP2);

JButton blackP2 = new JButton("Black ");
GridBagConstraints gbc_blackP2 = new GridBagConstraints();
gbc_blackP2.insets = new Insets(0, 0, 5, 0);
gbc_blackP2.anchor = GridBagConstraints.NORTHWEST;
gbc_blackP2.gridx = 0;
gbc_blackP2.gridy = 3;
RightPanel.add(blackP2, gbc_blackP2);

JPanel CenterPanel = new JPanel();
contentPane.add(CenterPanel, BorderLayout.CENTER);
CenterPanel.setLayout(new GridLayout(0, 5, 0, 0));
JButton[] buttons = new JButton[16];


JPanel SouthPanel = new JPanel();
contentPane.add(SouthPanel, BorderLayout.SOUTH);

JPanel NorthPanel = new JPanel();
contentPane.add(NorthPanel, BorderLayout.NORTH);

JLabel lbltitlefoursight = new JLabel("FourSight in Java");
NorthPanel.add(lbltitlefoursight);
}

}

I just don't know how I would make this array work, or have it so when I click a side button it affects the next button I click by changing its color to the color assigned by the first button.. (I have to make certain buttons have certain functions that only happen when another button is clicked after one is clicked.. if that makes any sense lol)

NKN

2013-02-27 18:37:12

From: [XoL]
Alright so I started using the GUI thing but I realized it would make things really messy with 16 buttons right up in the middle, I was told to use an array and make them all react the same way? Here is the code I have now


import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.GridLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import java.awt.CardLayout;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import javax.swing.BoxLayout;
import java.awt.FlowLayout;
import javax.swing.JLabel;


public class FourSight extends JFrame {

private JPanel contentPane;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FourSight frame = new FourSight();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public FourSight() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 500, 378);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);

JPanel LeftPanel = new JPanel();
contentPane.add(LeftPanel, BorderLayout.WEST);
GridBagLayout gbl_LeftPanel = new GridBagLayout();
gbl_LeftPanel.columnWidths = new int[]{61, 0};
gbl_LeftPanel.rowHeights = new int[]{99, 23, 0, 0, 0, 0, 0, 0};
gbl_LeftPanel.columnWeights = new double[]{0.0, Double.MIN_VALUE};
gbl_LeftPanel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
LeftPanel.setLayout(gbl_LeftPanel);

JLabel lblPlayerOne = new JLabel("Player One");
GridBagConstraints gbc_lblPlayerOne = new GridBagConstraints();
gbc_lblPlayerOne.insets = new Insets(0, 0, 5, 0);
gbc_lblPlayerOne.gridx = 0;
gbc_lblPlayerOne.gridy = 0;
LeftPanel.add(lblPlayerOne, gbc_lblPlayerOne);

JButton greenP1 = new JButton("Green");
GridBagConstraints gbc_greenP1 = new GridBagConstraints();
gbc_greenP1.anchor = GridBagConstraints.WEST;
gbc_greenP1.insets = new Insets(0, 0, 5, 0);
gbc_greenP1.gridx = 0;
gbc_greenP1.gridy = 1;
LeftPanel.add(greenP1, gbc_greenP1);

JButton whiteP1 = new JButton("White");
GridBagConstraints gbc_whiteP1 = new GridBagConstraints();
gbc_whiteP1.anchor = GridBagConstraints.WEST;
gbc_whiteP1.insets = new Insets(0, 0, 5, 0);
gbc_whiteP1.gridx = 0;
gbc_whiteP1.gridy = 2;
LeftPanel.add(whiteP1, gbc_whiteP1);

JButton blackP1 = new JButton("Black ");
GridBagConstraints gbc_blackP1 = new GridBagConstraints();
gbc_blackP1.insets = new Insets(0, 0, 5, 0);
gbc_blackP1.anchor = GridBagConstraints.WEST;
gbc_blackP1.gridx = 0;
gbc_blackP1.gridy = 3;
LeftPanel.add(blackP1, gbc_blackP1);

JPanel RightPanel = new JPanel();
contentPane.add(RightPanel, BorderLayout.EAST);
GridBagLayout gbl_RightPanel = new GridBagLayout();
gbl_RightPanel.columnWidths = new int[]{65, 0};
gbl_RightPanel.rowHeights = new int[]{103, 0, 0, 0, 0, 0, 0, 0};
gbl_RightPanel.columnWeights = new double[]{0.0, Double.MIN_VALUE};
gbl_RightPanel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
RightPanel.setLayout(gbl_RightPanel);

JLabel lblPlayerTwo = new JLabel("Player Two");
GridBagConstraints gbc_lblPlayerTwo = new GridBagConstraints();
gbc_lblPlayerTwo.insets = new Insets(0, 0, 5, 0);
gbc_lblPlayerTwo.gridx = 0;
gbc_lblPlayerTwo.gridy = 0;
RightPanel.add(lblPlayerTwo, gbc_lblPlayerTwo);

JButton greenP2 = new JButton("Green");
GridBagConstraints gbc_greenP2 = new GridBagConstraints();
gbc_greenP2.insets = new Insets(0, 0, 5, 0);
gbc_greenP2.anchor = GridBagConstraints.NORTHWEST;
gbc_greenP2.gridx = 0;
gbc_greenP2.gridy = 1;
RightPanel.add(greenP2, gbc_greenP2);

JButton whiteP2 = new JButton("White");
GridBagConstraints gbc_whiteP2 = new GridBagConstraints();
gbc_whiteP2.insets = new Insets(0, 0, 5, 0);
gbc_whiteP2.anchor = GridBagConstraints.NORTHWEST;
gbc_whiteP2.gridx = 0;
gbc_whiteP2.gridy = 2;
RightPanel.add(whiteP2, gbc_whiteP2);

JButton blackP2 = new JButton("Black ");
GridBagConstraints gbc_blackP2 = new GridBagConstraints();
gbc_blackP2.insets = new Insets(0, 0, 5, 0);
gbc_blackP2.anchor = GridBagConstraints.NORTHWEST;
gbc_blackP2.gridx = 0;
gbc_blackP2.gridy = 3;
RightPanel.add(blackP2, gbc_blackP2);

JPanel CenterPanel = new JPanel();
contentPane.add(CenterPanel, BorderLayout.CENTER);
CenterPanel.setLayout(new GridLayout(0, 5, 0, 0));
JButton[] buttons = new JButton[16];


JPanel SouthPanel = new JPanel();
contentPane.add(SouthPanel, BorderLayout.SOUTH);

JPanel NorthPanel = new JPanel();
contentPane.add(NorthPanel, BorderLayout.NORTH);

JLabel lbltitlefoursight = new JLabel("FourSight in Java");
NorthPanel.add(lbltitlefoursight);
}

}

I just don't know how I would make this array work, or have it so when I click a side button it affects the next button I click by changing its color to the color assigned by the first button.. (I have to make certain buttons have certain functions that only happen when another button is clicked after one is clicked.. if that makes any sense lol)

Make an array of Booleans for how ever many you need, then make a button click toggle a Boolean?

J J

2013-02-27 18:40:39

Add different behaviour so make a method to return a result.. or make multiple actionlisteners.

Can use a GridLayout 3, 1 to align 3 panels next to eachother.

Multiple classes would make it easier.. but HARDER if they all need to react on eachother.

Could use Observer perhaps, just had some classes about that last week. Made a neat Color model with sliders and 2 different Jframes listening to same model. Think its called MVC

[XoL]

2013-02-27 18:51:40

Ill work on it for a few hours and get back to you guys with what I have

[XoL]

2013-02-28 00:17:10


import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JLabel;
import javax.swing.border.LineBorder;


public class FourSight extends JFrame {

private JPanel contentPane;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FourSight frame = new FourSight();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public FourSight() {
super("FourSight");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 500, 378);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);

JPanel LeftPanel = new JPanel();
LeftPanel.setBorder(new LineBorder(new Color(0, 0, 0), 2));
contentPane.add(LeftPanel, BorderLayout.WEST);
GridBagLayout gbl_LeftPanel = new GridBagLayout();
gbl_LeftPanel.columnWidths = new int[]{61, 0};
gbl_LeftPanel.rowHeights = new int[]{99, 23, 0, 0, 0, 0, 0, 0};
gbl_LeftPanel.columnWeights = new double[]{0.0, Double.MIN_VALUE};
gbl_LeftPanel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
LeftPanel.setLayout(gbl_LeftPanel);

JLabel lblPlayerOne = new JLabel("Player One");
GridBagConstraints gbc_lblPlayerOne = new GridBagConstraints();
gbc_lblPlayerOne.insets = new Insets(0, 0, 5, 0);
gbc_lblPlayerOne.gridx = 0;
gbc_lblPlayerOne.gridy = 0;
LeftPanel.add(lblPlayerOne, gbc_lblPlayerOne);

JButton greenP1 = new JButton("Green");
greenP1.setOpaque(true);
greenP1.setBackground(Color.GREEN);
GridBagConstraints gbc_greenP1 = new GridBagConstraints();
gbc_greenP1.anchor = GridBagConstraints.WEST;
gbc_greenP1.insets = new Insets(0, 0, 5, 0);
gbc_greenP1.gridx = 0;
gbc_greenP1.gridy = 1;
LeftPanel.add(greenP1, gbc_greenP1);
//P1Green g1 = new P1Green();
//greenP1.addActionListener(g1);

JButton whiteP1 = new JButton("White");
whiteP1.setOpaque(true);
whiteP1.setBackground(Color.WHITE);
GridBagConstraints gbc_whiteP1 = new GridBagConstraints();
gbc_whiteP1.anchor = GridBagConstraints.WEST;
gbc_whiteP1.insets = new Insets(0, 0, 5, 0);
gbc_whiteP1.gridx = 0;
gbc_whiteP1.gridy = 2;
LeftPanel.add(whiteP1, gbc_whiteP1);

JButton blackP1 = new JButton("Black ");
blackP1.setOpaque(true);
blackP1.setForeground(Color.WHITE);
blackP1.setBackground(Color.BLACK);
GridBagConstraints gbc_blackP1 = new GridBagConstraints();
gbc_blackP1.insets = new Insets(0, 0, 5, 0);
gbc_blackP1.anchor = GridBagConstraints.WEST;
gbc_blackP1.gridx = 0;
gbc_blackP1.gridy = 3;
LeftPanel.add(blackP1, gbc_blackP1);

JPanel RightPanel = new JPanel();
RightPanel.setBorder(new LineBorder(new Color(0, 0, 0), 2));
contentPane.add(RightPanel, BorderLayout.EAST);
GridBagLayout gbl_RightPanel = new GridBagLayout();
gbl_RightPanel.columnWidths = new int[]{65, 0};
gbl_RightPanel.rowHeights = new int[]{103, 0, 0, 0, 0, 0, 0, 0};
gbl_RightPanel.columnWeights = new double[]{0.0, Double.MIN_VALUE};
gbl_RightPanel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
RightPanel.setLayout(gbl_RightPanel);

JLabel lblPlayerTwo = new JLabel("Player Two");
GridBagConstraints gbc_lblPlayerTwo = new GridBagConstraints();
gbc_lblPlayerTwo.insets = new Insets(0, 0, 5, 0);
gbc_lblPlayerTwo.gridx = 0;
gbc_lblPlayerTwo.gridy = 0;
RightPanel.add(lblPlayerTwo, gbc_lblPlayerTwo);

JButton greenP2 = new JButton("Green");
GridBagConstraints gbc_greenP2 = new GridBagConstraints();
greenP2.setOpaque(true);
greenP2.setBackground(Color.GREEN);
gbc_greenP2.insets = new Insets(0, 0, 5, 0);
gbc_greenP2.anchor = GridBagConstraints.NORTHWEST;
gbc_greenP2.gridx = 0;
gbc_greenP2.gridy = 1;
RightPanel.add(greenP2, gbc_greenP2);

JButton whiteP2 = new JButton("White");
GridBagConstraints gbc_whiteP2 = new GridBagConstraints();
whiteP2.setOpaque(true);
whiteP2.setBackground(Color.WHITE);
gbc_whiteP2.insets = new Insets(0, 0, 5, 0);
gbc_whiteP2.anchor = GridBagConstraints.NORTHWEST;
gbc_whiteP2.gridx = 0;
gbc_whiteP2.gridy = 2;
RightPanel.add(whiteP2, gbc_whiteP2);

JButton blackP2 = new JButton("Black ");
GridBagConstraints gbc_blackP2 = new GridBagConstraints();
blackP2.setOpaque(true);
blackP2.setForeground(Color.WHITE);
blackP2.setBackground(Color.BLACK);
gbc_blackP2.insets = new Insets(0, 0, 5, 0);
gbc_blackP2.anchor = GridBagConstraints.NORTHWEST;
gbc_blackP2.gridx = 0;
gbc_blackP2.gridy = 3;
RightPanel.add(blackP2, gbc_blackP2);

JPanel CenterPanel = new JPanel();
CenterPanel.setBorder(new LineBorder(new Color(0, 0, 0), 2));
contentPane.add(CenterPanel, BorderLayout.CENTER);
CenterPanel.setLayout(new GridLayout(4,4));


//array loop adds buttons to the grid
JButton[] grayButtons = new JButton[16];
for(int i = 0; i < 16; i++){
grayButtons[i]= new JButton();
grayButtons[i].setSize(80, 80);
grayButtons[i].setBackground(Color.GRAY);
CenterPanel.add(grayButtons[i]);
}


JPanel SouthPanel = new JPanel();
contentPane.add(SouthPanel, BorderLayout.SOUTH);

JPanel NorthPanel = new JPanel();
NorthPanel.setBorder(new LineBorder(new Color(0, 0, 0), 2));
contentPane.add(NorthPanel, BorderLayout.NORTH);

JLabel lbltitlefoursight = new JLabel("FourSight");
NorthPanel.add(lbltitlefoursight);
}

}

heres what i got so far, but… i need help lol cant figure out how to make it so when i click one button and one in the array the button in the array changes colors.

inferno

2013-02-28 10:34:27

Hey, I implemented some ActionListeners to get the colour switching working for you. I've also added a comment above each part I added in so you can easily see it. To sum it up:

-One variable 'btnColor' which holds the value of what the next button should be turned to. (If a player just clicked his green button, it will hold Color.GREEN etc)
-Simple ActionListener for each of your players colour choice buttons which utilises the setColor method to change the current value of 'btnColor'
-One ActionListener for your grid of 16 buttons. Determines which button was pressed and sets its background colour to match the value currently in 'btnColor'

If you want to restrict players to only changing one button at a time simply add
setColor(Color.GRAY);
into the grid ActionListener.

I've left the rules for you to code in since it is your assignment, but of course people around here are always happy to help if needed.

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JLabel;
import javax.swing.border.LineBorder;


public class FourSight extends JFrame {

private JPanel contentPane;
private Color btnColor = Color.GRAY;

/* Setter method for btnColor */
private void setColor(Color c) {
btnColor = c;
}

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FourSight frame = new FourSight();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public FourSight() {
super("FourSight");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 500, 378);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);

JPanel LeftPanel = new JPanel();
LeftPanel.setBorder(new LineBorder(new Color(0, 0, 0), 2));
contentPane.add(LeftPanel, BorderLayout.WEST);
GridBagLayout gbl_LeftPanel = new GridBagLayout();
gbl_LeftPanel.columnWidths = new int[]{61, 0};
gbl_LeftPanel.rowHeights = new int[]{99, 23, 0, 0, 0, 0, 0, 0};
gbl_LeftPanel.columnWeights = new double[]{0.0, Double.MIN_VALUE};
gbl_LeftPanel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
LeftPanel.setLayout(gbl_LeftPanel);

JLabel lblPlayerOne = new JLabel("Player One");
GridBagConstraints gbc_lblPlayerOne = new GridBagConstraints();
gbc_lblPlayerOne.insets = new Insets(0, 0, 5, 0);
gbc_lblPlayerOne.gridx = 0;
gbc_lblPlayerOne.gridy = 0;
LeftPanel.add(lblPlayerOne, gbc_lblPlayerOne);

JButton greenP1 = new JButton("Green");
greenP1.setOpaque(true);
greenP1.setBackground(Color.GREEN);
// Add an actionlistener to our button, sets btnColor to GREEN when clicked
greenP1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setColor(Color.GREEN);
}
});
GridBagConstraints gbc_greenP1 = new GridBagConstraints();
gbc_greenP1.anchor = GridBagConstraints.WEST;
gbc_greenP1.insets = new Insets(0, 0, 5, 0);
gbc_greenP1.gridx = 0;
gbc_greenP1.gridy = 1;
LeftPanel.add(greenP1, gbc_greenP1);

JButton whiteP1 = new JButton("White");
whiteP1.setOpaque(true);
whiteP1.setBackground(Color.WHITE);
// Add an actionlistener to our button, sets btnColor to WHITE when clicked
whiteP1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setColor(Color.WHITE);
}
});
GridBagConstraints gbc_whiteP1 = new GridBagConstraints();
gbc_whiteP1.anchor = GridBagConstraints.WEST;
gbc_whiteP1.insets = new Insets(0, 0, 5, 0);
gbc_whiteP1.gridx = 0;
gbc_whiteP1.gridy = 2;
LeftPanel.add(whiteP1, gbc_whiteP1);

JButton blackP1 = new JButton("Black ");
blackP1.setOpaque(true);
blackP1.setForeground(Color.WHITE);
blackP1.setBackground(Color.BLACK);
// Add an actionlistener to our button, sets btnColor to BLACK when clicked
blackP1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setColor(Color.BLACK);
}
});
GridBagConstraints gbc_blackP1 = new GridBagConstraints();
gbc_blackP1.insets = new Insets(0, 0, 5, 0);
gbc_blackP1.anchor = GridBagConstraints.WEST;
gbc_blackP1.gridx = 0;
gbc_blackP1.gridy = 3;
LeftPanel.add(blackP1, gbc_blackP1);

JPanel RightPanel = new JPanel();
RightPanel.setBorder(new LineBorder(new Color(0, 0, 0), 2));
contentPane.add(RightPanel, BorderLayout.EAST);
GridBagLayout gbl_RightPanel = new GridBagLayout();
gbl_RightPanel.columnWidths = new int[]{65, 0};
gbl_RightPanel.rowHeights = new int[]{103, 0, 0, 0, 0, 0, 0, 0};
gbl_RightPanel.columnWeights = new double[]{0.0, Double.MIN_VALUE};
gbl_RightPanel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
RightPanel.setLayout(gbl_RightPanel);

JLabel lblPlayerTwo = new JLabel("Player Two");
GridBagConstraints gbc_lblPlayerTwo = new GridBagConstraints();
gbc_lblPlayerTwo.insets = new Insets(0, 0, 5, 0);
gbc_lblPlayerTwo.gridx = 0;
gbc_lblPlayerTwo.gridy = 0;
RightPanel.add(lblPlayerTwo, gbc_lblPlayerTwo);

JButton greenP2 = new JButton("Green");
GridBagConstraints gbc_greenP2 = new GridBagConstraints();
greenP2.setOpaque(true);
greenP2.setBackground(Color.GREEN);
// Add an actionlistener to our button, sets btnColor to GREEN when clicked
greenP2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setColor(Color.GREEN);
}
});
gbc_greenP2.insets = new Insets(0, 0, 5, 0);
gbc_greenP2.anchor = GridBagConstraints.NORTHWEST;
gbc_greenP2.gridx = 0;
gbc_greenP2.gridy = 1;
RightPanel.add(greenP2, gbc_greenP2);

JButton whiteP2 = new JButton("White");
GridBagConstraints gbc_whiteP2 = new GridBagConstraints();
whiteP2.setOpaque(true);
whiteP2.setBackground(Color.WHITE);
// Add an actionlistener to our button, sets btnColor to WHITE when clicked
whiteP2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setColor(Color.WHITE);
}
});
gbc_whiteP2.insets = new Insets(0, 0, 5, 0);
gbc_whiteP2.anchor = GridBagConstraints.NORTHWEST;
gbc_whiteP2.gridx = 0;
gbc_whiteP2.gridy = 2;
RightPanel.add(whiteP2, gbc_whiteP2);

JButton blackP2 = new JButton("Black ");
GridBagConstraints gbc_blackP2 = new GridBagConstraints();
blackP2.setOpaque(true);
blackP2.setForeground(Color.WHITE);
blackP2.setBackground(Color.BLACK);
// Add an actionlistener to our button, sets btnColor to BLACK when clicked
blackP2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setColor(Color.BLACK);
}
});
gbc_blackP2.insets = new Insets(0, 0, 5, 0);
gbc_blackP2.anchor = GridBagConstraints.NORTHWEST;
gbc_blackP2.gridx = 0;
gbc_blackP2.gridy = 3;
RightPanel.add(blackP2, gbc_blackP2);

JPanel CenterPanel = new JPanel();
CenterPanel.setBorder(new LineBorder(new Color(0, 0, 0), 2));
contentPane.add(CenterPanel, BorderLayout.CENTER);
CenterPanel.setLayout(new GridLayout(4,4));

//array loop adds buttons to the grid
JButton[] grayButtons = new JButton[16];
for(int i = 0; i < 16; i++){
grayButtons[i]= new JButton();
grayButtons[i].setSize(80, 80);
grayButtons[i].setBackground(Color.GRAY);
//grid ActionListener changes selected button to match current btnColor
grayButtons[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JButton button = (JButton)e.getSource();
button.setBackground(btnColor);
}
});
CenterPanel.add(grayButtons[i]);
}

JPanel SouthPanel = new JPanel();
contentPane.add(SouthPanel, BorderLayout.SOUTH);

JPanel NorthPanel = new JPanel();
NorthPanel.setBorder(new LineBorder(new Color(0, 0, 0), 2));
contentPane.add(NorthPanel, BorderLayout.NORTH);

JLabel lbltitlefoursight = new JLabel("FourSight");
NorthPanel.add(lbltitlefoursight);
}

}

[XoL]

2013-02-28 12:25:14

Thank you so much :)
Will get back to you guys in a bit, once I begin working on this again!

[XoL]

2013-03-01 10:33:26

Hey so I am having an issue with the actual selecting of the button; likee I need to be able to be able to stop it from putting black/white pieces down without having a green piece on the ground first.

Example scenario:
I place a green piece, I can now place a black or a white piece on top of that spot.

Another scenario:
I try to place a black piece without a green piece on the spot; I would get some error of the sort

Another scenario:
I try to place a white piece on top of a black piece (that is on top of a green piece); I would get some error telling me I cannot do that..

The problem I am having is actually finding which button in the array is being clicked, I have it so it works if there is a green piece anyyywhereee on the grid but I need to to work on only one spot.

Any help would be highly appreciated!


import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JLabel;
import javax.swing.border.LineBorder;

public class FourSight extends JFrame {

private JPanel contentPane;
private Color btnColor = Color.GRAY;

/* Setter method for btnColor */
private void setColor(Color c) {
btnColor = c;
}

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FourSight frame = new FourSight();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public FourSight() {
super("FourSight");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 500, 378);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);

JPanel LeftPanel = new JPanel();
LeftPanel.setBorder(new LineBorder(new Color(0, 0, 0), 2));
contentPane.add(LeftPanel, BorderLayout.WEST);
GridBagLayout gbl_LeftPanel = new GridBagLayout();
gbl_LeftPanel.columnWidths = new int[]{61, 0};
gbl_LeftPanel.rowHeights = new int[]{99, 23, 0, 0, 0, 0, 0, 0};
gbl_LeftPanel.columnWeights = new double[]{0.0, Double.MIN_VALUE};
gbl_LeftPanel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
LeftPanel.setLayout(gbl_LeftPanel);

JLabel lblPlayerOne = new JLabel("Player One");
GridBagConstraints gbc_lblPlayerOne = new GridBagConstraints();
gbc_lblPlayerOne.insets = new Insets(0, 0, 5, 0);
gbc_lblPlayerOne.gridx = 0;
gbc_lblPlayerOne.gridy = 0;
LeftPanel.add(lblPlayerOne, gbc_lblPlayerOne);

JButton greenP1 = new JButton("Green");
greenP1.setOpaque(true);
greenP1.setBackground(Color.GREEN);
// Add an actionlistener to our button, sets btnColor to GREEN when clicked
greenP1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (btnColor == Color.GRAY)
setColor(Color.GREEN);
else if
(btnColor == Color.BLACK)
System.out.println("Sorry you can't place that piece there");
else if
(btnColor == Color.WHITE)
System.out.println("Sorry you can't place that piece there");
else
System.out.println("You cant do that, must place GREEN piece first...");
}
});
GridBagConstraints gbc_greenP1 = new GridBagConstraints();
gbc_greenP1.anchor = GridBagConstraints.WEST;
gbc_greenP1.insets = new Insets(0, 0, 5, 0);
gbc_greenP1.gridx = 0;
gbc_greenP1.gridy = 1;
LeftPanel.add(greenP1, gbc_greenP1);

JButton whiteP1 = new JButton("White");
whiteP1.setOpaque(true);
whiteP1.setBackground(Color.WHITE);
// Add an actionlistener to our button, sets btnColor to WHITE when clicked
whiteP1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (btnColor == Color.GREEN)
setColor(Color.WHITE);
else if
(btnColor == Color.BLACK)
System.out.println("Sorry you can't place that piece there");
else
System.out.println("You cant do that, must place GREEN piece first...");
}
});
GridBagConstraints gbc_whiteP1 = new GridBagConstraints();
gbc_whiteP1.anchor = GridBagConstraints.WEST;
gbc_whiteP1.insets = new Insets(0, 0, 5, 0);
gbc_whiteP1.gridx = 0;
gbc_whiteP1.gridy = 2;
LeftPanel.add(whiteP1, gbc_whiteP1);

JButton blackP1 = new JButton("Black ");
blackP1.setOpaque(true);
blackP1.setForeground(Color.WHITE);
blackP1.setBackground(Color.BLACK);
// Add an actionlistener to our button, sets btnColor to BLACK when clicked
blackP1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (btnColor == Color.GREEN)
setColor(Color.BLACK);
else if
(btnColor == Color.WHITE)
System.out.println("Sorry you can't place that piece there");
else
System.out.println("You cant do that, must place GREEN piece first...");
}
});
GridBagConstraints gbc_blackP1 = new GridBagConstraints();
gbc_blackP1.insets = new Insets(0, 0, 5, 0);
gbc_blackP1.anchor = GridBagConstraints.WEST;
gbc_blackP1.gridx = 0;
gbc_blackP1.gridy = 3;
LeftPanel.add(blackP1, gbc_blackP1);

JPanel RightPanel = new JPanel();
RightPanel.setBorder(new LineBorder(new Color(0, 0, 0), 2));
contentPane.add(RightPanel, BorderLayout.EAST);
GridBagLayout gbl_RightPanel = new GridBagLayout();
gbl_RightPanel.columnWidths = new int[]{65, 0};
gbl_RightPanel.rowHeights = new int[]{103, 0, 0, 0, 0, 0, 0, 0};
gbl_RightPanel.columnWeights = new double[]{0.0, Double.MIN_VALUE};
gbl_RightPanel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
RightPanel.setLayout(gbl_RightPanel);

JLabel lblPlayerTwo = new JLabel("Player Two");
GridBagConstraints gbc_lblPlayerTwo = new GridBagConstraints();
gbc_lblPlayerTwo.insets = new Insets(0, 0, 5, 0);
gbc_lblPlayerTwo.gridx = 0;
gbc_lblPlayerTwo.gridy = 0;
RightPanel.add(lblPlayerTwo, gbc_lblPlayerTwo);

JButton greenP2 = new JButton("Green");
GridBagConstraints gbc_greenP2 = new GridBagConstraints();
greenP2.setOpaque(true);
greenP2.setBackground(Color.GREEN);
// Add an actionlistener to our button, sets btnColor to GREEN when clicked
greenP2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (btnColor == Color.GRAY)
setColor(Color.GREEN);
else if
(btnColor == Color.BLACK)
System.out.println("Sorry you can't place that piece there");
else if
(btnColor == Color.WHITE)
System.out.println("Sorry you can't place that piece there");
else
System.out.println("You cant do that, must place GREEN piece first...");
}
});
gbc_greenP2.insets = new Insets(0, 0, 5, 0);
gbc_greenP2.anchor = GridBagConstraints.NORTHWEST;
gbc_greenP2.gridx = 0;
gbc_greenP2.gridy = 1;
RightPanel.add(greenP2, gbc_greenP2);

JButton whiteP2 = new JButton("White");
GridBagConstraints gbc_whiteP2 = new GridBagConstraints();
whiteP2.setOpaque(true);
whiteP2.setBackground(Color.WHITE);
// Add an actionlistener to our button, sets btnColor to WHITE when clicked
whiteP2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (btnColor == Color.GREEN)
setColor(Color.WHITE);
else if
(btnColor == Color.BLACK)
System.out.println("Sorry you can't place that piece there");
else
System.out.println("You cant do that, must place GREEN piece first...");


}
});
gbc_whiteP2.insets = new Insets(0, 0, 5, 0);
gbc_whiteP2.anchor = GridBagConstraints.NORTHWEST;
gbc_whiteP2.gridx = 0;
gbc_whiteP2.gridy = 2;
RightPanel.add(whiteP2, gbc_whiteP2);

JButton blackP2 = new JButton("Black ");
GridBagConstraints gbc_blackP2 = new GridBagConstraints();
blackP2.setOpaque(true);
blackP2.setForeground(Color.WHITE);
blackP2.setBackground(Color.BLACK);
// Add an actionlistener to our button, sets btnColor to BLACK when clicked
blackP2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (btnColor == Color.GREEN)
setColor(Color.BLACK);
else if
(btnColor == Color.WHITE)
System.out.println("Sorry you can't place that piece there");
else
System.out.println("You cant do that, must place GREEN piece first...");

}
});
gbc_blackP2.insets = new Insets(0, 0, 5, 0);
gbc_blackP2.anchor = GridBagConstraints.NORTHWEST;
gbc_blackP2.gridx = 0;
gbc_blackP2.gridy = 3;
RightPanel.add(blackP2, gbc_blackP2);

JPanel CenterPanel = new JPanel();
CenterPanel.setBorder(new LineBorder(new Color(0, 0, 0), 2));
contentPane.add(CenterPanel, BorderLayout.CENTER);
CenterPanel.setLayout(new GridLayout(4,4));

//array loop adds buttons to the grid
JButton[] grayButtons = new JButton[16];
for(int i = 0; i < 16; i++){
grayButtons[i]= new JButton();
grayButtons[i].setSize(80, 80);
grayButtons[i].setBackground(Color.GRAY);
//grid ActionListener changes selected button to match current btnColor
grayButtons[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JButton button = (JButton)e.getSource();
button.setBackground(btnColor);
}
});
CenterPanel.add(grayButtons[i]);
}

JPanel SouthPanel = new JPanel();
contentPane.add(SouthPanel, BorderLayout.SOUTH);

JPanel NorthPanel = new JPanel();
NorthPanel.setBorder(new LineBorder(new Color(0, 0, 0), 2));
contentPane.add(NorthPanel, BorderLayout.NORTH);

JLabel lbltitlefoursight = new JLabel("FourSight");
NorthPanel.add(lbltitlefoursight);
}

}

@inferno
@J J
@NKN

J J

2013-03-01 10:59:13

@[XoL]; (that's how you tag properly :P)
You should really take a look at the code I posted earlier because it debugs whichs button has been pressed… ;)

[XoL]

2013-03-01 12:13:10

ahhh i see, thanks! lol, sorry im in derp mode too much going on too little time lol

J J

2013-03-01 14:11:16

Damn I forgot to add a DefaultClose operation so I had 35 java processes running after a while .. found out when I went to task manager because my PC was getting a bit slower than normal lol.

My progress from scratch so far

Object oriented design

Currently adding behaviour.

[XoL]

2013-03-01 14:20:55

Does anybody see the syntax error here, ive been trying to fix it for a bit but i keep making it worse :p

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JLabel;
import javax.swing.border.LineBorder;

public class FourSight extends JFrame {

private JPanel contentPane;
private Color btnColor = Color.GRAY;

/* Setter method for btnColor */
private void setColor(Color c) {
btnColor = c;
}

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FourSight frame = new FourSight();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public FourSight() {
super("FourSight");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 500, 378);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);

JPanel CenterPanel = new JPanel();
CenterPanel.setBorder(new LineBorder(new Color(0, 0, 0), 2));
contentPane.add(CenterPanel, BorderLayout.CENTER);
CenterPanel.setLayout(new GridLayout(4,4));

//array loop adds buttons to the grid
JButton[] grayButtons = new JButton[16];
for(int i = 0; i < 16; i++){
grayButtons[i]= new JButton();
grayButtons[i].setSize(80, 80);
grayButtons[i].setBackground(Color.GRAY);
//grid ActionListener changes selected button to match current btnColor
grayButtons[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JButton button = (JButton)e.getSource();
for(int x = 0; x < WIDTH; x++){
for(int y = 0; y < HEIGHT; y++){
if(button.equals(buttons[x][y])){
System.out.println("Button[" + x + "][" + y + "] has been clicked!");
button.setBackground(btnColor);
}
});
CenterPanel.add(grayButtons[i]);
}

JPanel LeftPanel = new JPanel();
LeftPanel.setBorder(new LineBorder(new Color(0, 0, 0), 2));
contentPane.add(LeftPanel, BorderLayout.WEST);
GridBagLayout gbl_LeftPanel = new GridBagLayout();
gbl_LeftPanel.columnWidths = new int[]{61, 0};
gbl_LeftPanel.rowHeights = new int[]{99, 23, 0, 0, 0, 0, 0, 0};
gbl_LeftPanel.columnWeights = new double[]{0.0, Double.MIN_VALUE};
gbl_LeftPanel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
LeftPanel.setLayout(gbl_LeftPanel);

JLabel lblPlayerOne = new JLabel("Player One");
GridBagConstraints gbc_lblPlayerOne = new GridBagConstraints();
gbc_lblPlayerOne.insets = new Insets(0, 0, 5, 0);
gbc_lblPlayerOne.gridx = 0;
gbc_lblPlayerOne.gridy = 0;
LeftPanel.add(lblPlayerOne, gbc_lblPlayerOne);

JButton greenP1 = new JButton("Green");
greenP1.setOpaque(true);
greenP1.setBackground(Color.GREEN);
// Add an actionlistener to our button, sets btnColor to GREEN when clicked
greenP1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (btnColor == Color.GRAY)
setColor(Color.GREEN);
else if
(btnColor == Color.BLACK)
System.out.println("Sorry you can't place that piece there");
else if
(btnColor == Color.WHITE)
System.out.println("Sorry you can't place that piece there");
else
System.out.println("You cant do that, must place GREEN piece first...");
}
});
GridBagConstraints gbc_greenP1 = new GridBagConstraints();
gbc_greenP1.anchor = GridBagConstraints.WEST;
gbc_greenP1.insets = new Insets(0, 0, 5, 0);
gbc_greenP1.gridx = 0;
gbc_greenP1.gridy = 1;
LeftPanel.add(greenP1, gbc_greenP1);

JButton whiteP1 = new JButton("White");
whiteP1.setOpaque(true);
whiteP1.setBackground(Color.WHITE);
// Add an actionlistener to our button, sets btnColor to WHITE when clicked
whiteP1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (btnColor == Color.GREEN)
setColor(Color.WHITE);
else if
(btnColor == Color.BLACK)
System.out.println("Sorry you can't place that piece there");
else
System.out.println("You cant do that, must place GREEN piece first...");
}
});
GridBagConstraints gbc_whiteP1 = new GridBagConstraints();
gbc_whiteP1.anchor = GridBagConstraints.WEST;
gbc_whiteP1.insets = new Insets(0, 0, 5, 0);
gbc_whiteP1.gridx = 0;
gbc_whiteP1.gridy = 2;
LeftPanel.add(whiteP1, gbc_whiteP1);

JButton blackP1 = new JButton("Black ");
blackP1.setOpaque(true);
blackP1.setForeground(Color.WHITE);
blackP1.setBackground(Color.BLACK);
// Add an actionlistener to our button, sets btnColor to BLACK when clicked
blackP1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (btnColor == Color.GREEN)
setColor(Color.BLACK);
else if
(btnColor == Color.WHITE)
System.out.println("Sorry you can't place that piece there");
else
System.out.println("You cant do that, must place GREEN piece first...");
}
});
GridBagConstraints gbc_blackP1 = new GridBagConstraints();
gbc_blackP1.insets = new Insets(0, 0, 5, 0);
gbc_blackP1.anchor = GridBagConstraints.WEST;
gbc_blackP1.gridx = 0;
gbc_blackP1.gridy = 3;
LeftPanel.add(blackP1, gbc_blackP1);

JPanel RightPanel = new JPanel();
RightPanel.setBorder(new LineBorder(new Color(0, 0, 0), 2));
contentPane.add(RightPanel, BorderLayout.EAST);
GridBagLayout gbl_RightPanel = new GridBagLayout();
gbl_RightPanel.columnWidths = new int[]{65, 0};
gbl_RightPanel.rowHeights = new int[]{103, 0, 0, 0, 0, 0, 0, 0};
gbl_RightPanel.columnWeights = new double[]{0.0, Double.MIN_VALUE};
gbl_RightPanel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
RightPanel.setLayout(gbl_RightPanel);

JLabel lblPlayerTwo = new JLabel("Player Two");
GridBagConstraints gbc_lblPlayerTwo = new GridBagConstraints();
gbc_lblPlayerTwo.insets = new Insets(0, 0, 5, 0);
gbc_lblPlayerTwo.gridx = 0;
gbc_lblPlayerTwo.gridy = 0;
RightPanel.add(lblPlayerTwo, gbc_lblPlayerTwo);

JButton greenP2 = new JButton("Green");
GridBagConstraints gbc_greenP2 = new GridBagConstraints();
greenP2.setOpaque(true);
greenP2.setBackground(Color.GREEN);
// Add an actionlistener to our button, sets btnColor to GREEN when clicked
greenP2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (btnColor == Color.GRAY)
setColor(Color.GREEN);
else if
(btnColor == Color.BLACK)
System.out.println("Sorry you can't place that piece there");
else if
(btnColor == Color.WHITE)
System.out.println("Sorry you can't place that piece there");
else
System.out.println("You cant do that, must place GREEN piece first...");
}
});
gbc_greenP2.insets = new Insets(0, 0, 5, 0);
gbc_greenP2.anchor = GridBagConstraints.NORTHWEST;
gbc_greenP2.gridx = 0;
gbc_greenP2.gridy = 1;
RightPanel.add(greenP2, gbc_greenP2);

JButton whiteP2 = new JButton("White");
GridBagConstraints gbc_whiteP2 = new GridBagConstraints();
whiteP2.setOpaque(true);
whiteP2.setBackground(Color.WHITE);
// Add an actionlistener to our button, sets btnColor to WHITE when clicked
whiteP2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (btnColor == Color.GREEN)
setColor(Color.WHITE);
else if
(btnColor == Color.BLACK)
System.out.println("Sorry you can't place that piece there");
else
System.out.println("You cant do that, must place GREEN piece first...");


}
});
gbc_whiteP2.insets = new Insets(0, 0, 5, 0);
gbc_whiteP2.anchor = GridBagConstraints.NORTHWEST;
gbc_whiteP2.gridx = 0;
gbc_whiteP2.gridy = 2;
RightPanel.add(whiteP2, gbc_whiteP2);

JButton blackP2 = new JButton("Black ");
GridBagConstraints gbc_blackP2 = new GridBagConstraints();
blackP2.setOpaque(true);
blackP2.setForeground(Color.WHITE);
blackP2.setBackground(Color.BLACK);
// Add an actionlistener to our button, sets btnColor to BLACK when clicked
blackP2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (btnColor == Color.GREEN)
setColor(Color.BLACK);
else if
(btnColor == Color.WHITE)
System.out.println("Sorry you can't place that piece there");
else
System.out.println("You cant do that, must place GREEN piece first...");

}
});
gbc_blackP2.insets = new Insets(0, 0, 5, 0);
gbc_blackP2.anchor = GridBagConstraints.NORTHWEST;
gbc_blackP2.gridx = 0;
gbc_blackP2.gridy = 3;
RightPanel.add(blackP2, gbc_blackP2);


JPanel SouthPanel = new JPanel();
contentPane.add(SouthPanel, BorderLayout.SOUTH);

JPanel NorthPanel = new JPanel();
NorthPanel.setBorder(new LineBorder(new Color(0, 0, 0), 2));
contentPane.add(NorthPanel, BorderLayout.NORTH);

JLabel lbltitlefoursight = new JLabel("FourSight");
NorthPanel.add(lbltitlefoursight);
}
}
}
}
}

J J

2013-03-01 15:17:48

Here is my solution.
Includes a .jar to play the game.

It does detect when the game has ended but does not do anything with that fact. Read the README for more info :P


EDIT: Added pictures, and what do you think about the source code? I'm going to update my site in the near-future to make it look better. You can guess the name based on my package name I guess.. :) I'm looking to host some java stuff I've made and add some information etc.

I assume you are okay with that after you handed your project in to avoid getting in trouble for "ripping" or something.

[XoL]

2013-03-01 17:33:39

From: J J
Here is my solution.
Includes a .jar to play the game.

It does detect when the game has ended but does not do anything with that fact. Read the README for more info :P


EDIT: Added pictures, and what do you think about the source code? I'm going to update my site in the near-future to make it look better. You can guess the name based on my package name I guess.. :) I'm looking to host some java stuff I've made and add some information etc.

I assume you are okay with that after you handed your project in to avoid getting in trouble for "ripping" or something.


Thank you :) and yeah feel free! He will probably read through it in like a week or so

inferno

2013-03-01 19:34:51

Nicely done J J, although I noticed you haven't included a restriction for players placing more than 8 green or 4 black/white markers. E.g you can turn the entire board green as player 1 whilst turning it all black as player 2, going down to -8 greens and -12 blacks. Also I don't think the opposite teams colour is meant to be restricted or else there would be no reason for a player to start with 4 of them and the finishing condition is "when all markers have been played". I think that's part of the strategy of the game, placing your tiles which match your opponents colour in a way that they can't make them into a 3 in-a-row. That could just be my misunderstanding of the game though, I've never played it myself.

A word of caution as well [Xol], I'd advise against taking a project completely written by someone else and submitting it. It's quite easy to read through code and see that it doesn't match the style of a persons previous projects. Teachers pick up on this all the time, especially if the code is beyond the level that they've taught you so far and it may lead to inquiring questions. You're better off reading through the files, figuring out each individual part and how it works then writing your own when you have an understanding of how to do it (without just copying it).

J J creating it for you was awesome of him, but it's better for you in the long run to ask for small bits of help at a time. If you do most of the work yourself, and just get pointed in the right direction every so often then it may not end up particularly pretty or optimized but you will come away from the assignment with a far better understanding than if you had submitted someone else's complete work. Programming is really all about learning from experience.

[XoL]

2013-03-02 15:30:01

Oh I didn't use his entire project :p, the only thing I took from it was the nested forloop for distinguishing each array button.

Thank you all of you!