aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Java grid colouring in game Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Java grid colouring in game" Watch "Java grid colouring in game" New topic
Author

Java grid colouring in game

Pj Hughes
Greenhorn

Joined: Mar 24, 2012
Posts: 16
HI I am new to java and wanted to design a programme to do the following:

1-create a 6x6 grid
2-each time a button is pressed ask the user to select from 6 colours and colour in the selected button
3-if a button is clicked that has a colour already go to default
4-each row/column can have one instance of each colour
5-as soon as the fifth colour is filled in in a row or column the sixth colour appears in the final blank square automatically.
6-once a row is completed the squares can nopt be clicked on any more.
7-data should be read in and colour the squares from a file which is of the form R1 C2 Green R1=row number C2=column number.

i have the 6x6 grid and a window for colour choice but it needs fine tuning. the window should appear only once and it appears each time teh same button is pressed.



thanks in advance

[update: added code tags]
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
I'd start by changing the design - all those popup frames (which should have been JDialog's), would be annoying.

instead, have at the top of the grid 6 x JToggleButton set in a ButtonGroup (so only one can be selected at a time).

now, if you want to set all your (e.g.) yellow buttons, you select the yellow toggleButton then click each button you want
to be yellow. When 6 buttons are yellow that toggleButton is disabled (with error handling to ensure no duplicates in rows/columns)
Pj Hughes
Greenhorn

Joined: Mar 24, 2012
Posts: 16
OK I have managed to get buttons to change colour with my current design. I now need to read in a file in the following form
R1 C2 Green
R2 C3 Red

where r=row number c = column number and the string will equal the colour the specified square should be. How would i modify the code below to fill in the specified square? i am new to using file readers so have writeen one that just prints out the file as a test. i think i should use if statements but i am not sure how. thanks

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
you would change the indicated line


1) split() strLine on the delimiter (space/tab/whatever), so you have an array of strings
2) parse array[0] to get its int value = row
3) parse array[1] to get its int value = col
4) somehow you'll need to convert the string color to an actual color

then its just button[row][col].setBackground(color);
Pj Hughes
Greenhorn

Joined: Mar 24, 2012
Posts: 16
Hi thanks for that Michael. So would the code for the split parse the character R to an integer aswell? what i have so far is:



this code above did not work as im sure it was attempting to parse the character R to an integer as well. I tried using a character array but had no luck with that either.

is there actually a way of doing this?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
R1 C2 Green

I wrongly thought the R and C were references only, as no one in their right mind would save data that way,
so that makes this a homework assignment.

you need to look up the methods of the String class - there are a couple there that will get the part you want,
e.g. the 1 of R1, which you can convert to an int for use in your button array
Pj Hughes
Greenhorn

Joined: Mar 24, 2012
Posts: 16
Michael Dunn wrote:R1 C2 Green

I wrongly thought the R and C were references only, as no one in their right mind would save data that way,
so that makes this a homework assignment.

you need to look up the methods of the String class - there are a couple there that will get the part you want,
e.g. the 1 of R1, which you can convert to an int for use in your button array


Hi ok i have managed to write my file reader and just want to know why my variable rNum does not change according to the if statements? Thanks

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19695
    
  20

Don't use == for comparing Strings, use the equals method.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Pj Hughes
Greenhorn

Joined: Mar 24, 2012
Posts: 16
Rob Spoor wrote:Don't use == for comparing Strings, use the equals method.


Hi I have tried that and i get Error:null when i run the program. everything works except the file reader now. here is my latest attempt at it:

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
why are you doing this

instead of this

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19695
    
  20

Pj Hughes wrote:Hi I have tried that and i get Error:null when i run the program.

Don't just print the message, print the entire stack trace using e.printStackTrace(). It will tell you exactly what type of exception it is and where it is thrown from.
Pj Hughes
Greenhorn

Joined: Mar 24, 2012
Posts: 16
managed to get it working thanks for all the help
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java grid colouring in game