Hi this is my first post here.
basically i want a programme to do teh following:
1)create a 6x6 grid.
2)ask the user to choose from 6 colours to fill a square
3)test to see if one row or column has 5 colours in and autocomplete with the final colour. also it should not allow the user to click on any squares in that row or column if it is complete.
4) if a square has already been filled in and gets clicked again it should go back to blank/empty colour.
5) if a square is going to be coloured the same as one already in that row/column the square should be left blank and an error message displayed saying why and allowing the user to try again
the starting pattern is to be read in from a file i.e.
R1 C2 Green
where R1 is 1st row and C2 is second column.
I can create a 6x6 grid and open up a menu bar to ask for a colour but i can not get the button pressed to go into that colour. i also want the second window that opens to close when a colour is picked but dont know how to do that.
I can get to grips with file reading but not the testing of rows columns and colouring them in from button clicks etc.
the code belwo is what i have so far
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ButtonGrid extends JFrame implements ActionListener {
private int numClicks = 0;
public static void main(String[] args) {
int rowsGrid = 6;
int colsGrid = 6;
int sizeGrid = 600;
ButtonGrid grid = new ButtonGrid(rowsGrid, colsGrid);
grid.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
grid.setPreferredSize(new Dimension(sizeGrid, sizeGrid));
grid.pack();
grid.setLocationRelativeTo(null);
grid.setVisible(true);
}//end main
public ButtonGrid(int rows, int cols) {
Container pane = getContentPane();
pane.setLayout(new GridLayout(rows, cols));
for(int j =0; j<rows; j++)
for (int i = 0; i < cols; i++) {
JButton button = new JButton("");
button.addActionListener(this);
button.setBackground(new Color(100,100,100));
pane.add(button);
}
}//end buttonGrid
public void actionPerformed(ActionEvent e) {
numClicks++;
int rows = 6;
int cols = 6;
int size = 300;
ColourChoice choice = new ColourChoice(rows, cols);
choice.setPreferredSize(new Dimension(size/2, size));
choice.pack();
choice.setVisible(true);
}
public class ColourChoice extends JFrame{
public ColourChoice(int rows, int cols) {
String[] colourName ={"RED","GREEN","BLUE","YELLOW","PURPLE","BROWN" };
Color[] choices = {Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW, new Color(102, 0, 102), new Color(102, 51, 0)};
Container pane = getContentPane();
pane.setLayout(new GridLayout(rows, cols));
for (int i = 0; i < choices.length; i++) {
JButton button = new JButton(""+colourName[i]);
button.setForeground(new Color(100,100,100));
button.setBackground(choices[i]);
pane.add(button);
}
}
}//end choice class
}//end class
it creates a 6x6 grid and each time a button is pressed a new window opens with the colour choices.