• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Java grid colouring in game

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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]
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't use == for comparing Strings, use the equals method.
 
Pj Hughes
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4632
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why are you doing this

instead of this

 
Rob Spoor
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
managed to get it working thanks for all the help
 
Bras cause cancer. And tiny ads:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic