| | |
how will i repeat my whole game program?
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
psylocke... you should really learn to program more modularly & object-orientated-ly. If you had, making little modifications to your program (and controlling the flow of the program) would be much easier.
But as it looks now... I think you should switch your program to the following structure:
Then get your YES button ActionListener to call only the init() method and then the start() method.
But as it looks now... I think you should switch your program to the following structure:
- a create() method in which you 'create' everything (all the new labels etc.)
- a reset() method in which you set all values, fields, label names, guesses etc. to their initial (pre-game) values
- a start() method (same as you already have)
Then get your YES button ActionListener to call only the init() method and then the start() method.
Since everything here appears to be in one method, I get the impression that psylocke is brand new to Java and hasn't yet learned how to create multiple methods and classes. Am I right?
Dani the Computer Science Gal 
Follow my Twitter feed! twitter.com/DaniWeb
And if you're interested in Internet marketing there is twitter.com/DaniWebAds

Follow my Twitter feed! twitter.com/DaniWeb
And if you're interested in Internet marketing there is twitter.com/DaniWebAds
psylocke,
Another suggestion: Since you have so many related variables (i.e. Label peglbl1, peglbl2, peglbl3) it may make your code neater if you used arrays.
Instead of multiple Label statements, do this:
Then instead of what you do in your action listener
You can do this:
Let us know if this makes sense. I'm not sure what you have learned. Definitely give arrays a try. It should tidy up your code much.
Ed
Another suggestion: Since you have so many related variables (i.e. Label peglbl1, peglbl2, peglbl3) it may make your code neater if you used arrays.
Instead of multiple Label statements, do this:
Java Syntax (Toggle Plain Text)
Label[] pegLabel = new Label[15]; for (int i = 0; i < pegLabel.length; i++) { pegLabel[i] = new Label(""); pegLabel.setBackground(Color.DARK_GRAY); add(pegLabel[i]) } // - or if you have a string table 'pegStrings' with all the initial // strings of the labels, you could do this: for (int i = 0; i < pegLabel.length; i++) pegLabel[i] = new Label(pegStrings[i]);
Then instead of what you do in your action listener
Java Syntax (Toggle Plain Text)
if(tri==1) peglbl1.setBackground(Color.GREEN); else if(tri==2) peglbl2.setBackground(Color.GREEN); else if(tri==3) peglbl3.setBackground(Color.GREEN);
You can do this:
Java Syntax (Toggle Plain Text)
pegLabel[tri].setBackground(Color.GREEN);
Let us know if this makes sense. I'm not sure what you have learned. Definitely give arrays a try. It should tidy up your code much.
Ed
•
•
•
•
In a world without walls or fences,
What use are Windows and Gates.
•
•
Join Date: Sep 2004
Posts: 18
Reputation:
Solved Threads: 0
hi cosi, thanks for the big help. i really know that there is a way to make this short using arrays but i dont know how to do that i cant find reference material for this during that time, coz i was in a rush doing this project. your recommendations are all great this exactly what i also wanted to know...THANK U so much cosi. muah
•
•
Join Date: Sep 2004
Posts: 18
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by cscgal
Since everything here appears to be in one method, I get the impression that psylocke is brand new to Java and hasn't yet learned how to create multiple methods and classes. Am I right?
![]() |
Similar Threads
Other Threads in the Java Forum
- Previous Thread: my Java implementation of Mastermind Game....
- Next Thread: Create menu from properties file by recursion
Views: 4821 | Replies: 16
| Thread Tools | Search this Thread |
Tag cloud for Java
android api apple applet application applications arguments array arrays automation bank binary bluetooth chat class classes clear client code codesnippet collections component database db development dice draw eclipse error event exception file formatingtextintooltipjava fractal game givemetehcodez graphics gui helpwithhomework html ide image infinite input integer j2me jarfile java javaprojects jmf jni jpanel julia linux list loop looping map method methods mobile mysql netbeans newbie number object openjavafx oracle php print problem program programming project recursion repositories scanner screen scrollbar server set size sms socket sort sorting sql sqlserver state storm string superclass swing test text-file threads time transfer tree windows






