This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
So, I am building a game that has the flag of a country in a JLabel. There are 6 flags that appear and a combobox with the names of countries. When the flag is "USA" and the ComboBox has "USA" then the submit button is clicked, it says it is right and adds 1 point to the total amount of points and changes the flag. If it is wrong, it does not add to the total points and changes the flag. I am a little bog down in the different strings I'm using and its not working, anybody know how to at least do this in a simple and short way? Thanks!
post your 'not working' code and we'll try to steer you in the right direction
Joined: Oct 07, 2010
This is the code I have so far, I cleaned out the if-then garbage that didn't work at all :P
Now what I think is the process would be to have all the pictures into a kind of randomizer. Then it displays a random flag on the Gui. When the answer is selected, and the submit button is pressed, if the flag and country are correct, it adds a point to the total (Not created yet). If the guess is correct, that flag is removed from the list of flags to be shown. If it is not right, another random flag is selected and the old flag returns to the list of flags to be shown.
Joined: Jun 09, 2003
I'd be more inclined to create an array of 'Country' objects
overloaded constructor so the object can be created with name/flag, or just name.
include an overridden toString() to return the country name (need this to add the Country object to the comboBox)
comboBox can be sorted, if required
also create another array of 6 ImageIcons (named e.g. 'flags')
iterate the country array, and if the flag is not null, point flags (then 1 etc) to the country flag
now shuffle the flags array
open the GUI showing flags
check if label.getIcon() equals the country's flag of (comboBox.getSelectedItem() cast to a Country object)
do what you want with correct/wrong answers
if last flag is showing, shuffle flags again, reset counter to 0, so next flag shown will be flags
I edited your post to have code tags around your source. It makes it MUCH easier to read. Next time, you can simply click the 'code' button above where you enter your post to get the tags, then paste your code between them.
or you can simply type them out:
(just leave out the space). You can see it preserves the spacing and even does highlighting of keywords.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors