• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to incorporate JFrame components into an enum class.  RSS feed

 
rick pine
Ranch Hand
Posts: 90
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, i have a simple rock paper scissors game. That displays a gui with three jbuttons, place inside a jpanel two of which contain an image that is loaded by calling an enum that contains the images. I hope that made sense. Now whenever there is a match the result will be displayed in the middle jbuton that contains a jlabel, i can work out the logic inside of the gui class by saying something like, if icon on left jbutton equals rock and icon on right button equals paper, change the jlabel text to who ever wins, that is nice and easy. But being a school assignment im requiered to build a winner losser method inside of my enum class. The problem is that i dont understand how i can bring the components from the jframe into the enum. My idea is that i will need a method that returns a string because of jLabel.setText("some string").  the code its rather large, so i will post what i believe to be necessary to communicate my issue.




 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apart from the fact that enum names start with a capital letter . . . the title of this t‍hread hides a misconception, I am afraid. You do not add GUI components to an enum, nor do you have a game which displays it on a GUI. You have a game and the game is played. Later you can add a GUI to it so its display is prettier. The GUI doesn't play the game or anything; it simply shows what is going on. You shou‍ld be able to play the game entirely without a GUI.

I don't like the anonymous class there. Forgetting that you shou‍ld write a λ instead, you will have three anonymous classes which do the same thing: apply a symbol to various GUI components. That requires a named class, maybe RockPaperScissorsListener.
Don' do arithmetic with Math#random. Use a Random instance instead. Much easier.
 
rick pine
Ranch Hand
Posts: 90
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did notice the enum type was incorrect and have fixed it. Also i figured it out, i just needed to create the method with paramteres of type enum. I was very wrong with what i thought i needed to do. Regarding the Mathrandom i will try to use a random instance instead. Thank you for replying, this the solution i came up with

 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sure there is a more elegant way to implement that method. You may have to use the ordinal() method and remainders. I am saying no more at present.
 
salvin francis
Bartender
Posts: 1653
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your method "whoWins" returns a string message. Particularly, it returns the success message. Instead, I suggest you can return the Player that won or null if its a tie.

Next, something really smells wrong when you write "RockPaperScissors user". Is it a Player object or is it a RockPaperScissors "holder" of some sort.

My suggestion:

 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd put the win/lose/draw logic in the enum.
Give the Choice/RockPaperScissors enum an attribute identifying the ordinal that it beats, and provide a method on the enum that takes another enum object and uses the ordinal to determine who won.
 
salvin francis
Bartender
Posts: 1653
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:I'd put the win/lose/draw logic in the enum.

Thats a preference, I would put that in some game logic class instead. But like I said, its just a preference.

Dave Tolls wrote:identifying the ordinal that it beats

This is something that I would protest against. Imagine someone edits the enum like this:



or

 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it's a fair point.
Then, if the logic was in the Game class (or whatever you want to call it) then that's where the enum would live as well.
Maybe...
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:. . . Give the Choice/RockPaperScissors enum an attribute identifying the ordinal that it beats . . .
Or the object that it beats, or the object that beats it. That would probably obviate Salvin's objection.

Please search this forum and Java in General and you may find an implementation of rock paper scissors using that technique.
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Dave Tolls wrote:. . . Give the Choice/RockPaperScissors enum an attribute identifying the ordinal that it beats . . .
Or the object that it beats, or the object that beats it. That would probably obviate Salvin's objection.


I picked the ordinal as you can't pass the enum it beats into the enum constructor, for hopefully obvious reasons.
I will say (in my defence) I didn't like the idea...
And (doing a search) I do remember the overriding method solution, which is much neater.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:. . . you can't pass the enum it beats into the enum constructor, . . .
Damn! I forgot that bit.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!