Alex McCormick

Ranch Hand
+ Follow
since Mar 08, 2003
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Alex McCormick

Ok, I knew as soon as I posted the question I'd figure it out. Turns out that when I was creating my GridLayout upon which the JLabels sit, I was passing in a zero for the "hgap" (horizontal gap) which probably meant that my JLablel's were butting up against each other. (even though the card images were not, still not sure why this is) I put a gap of 50 in there and now it seems to work fine.
20 years ago
Hi all, I've got a very crude Swing GUI with 6 JLabel's sitting on a JPanel using a GridLayout. It's for a card game, and the 6 JLabel's are actually displaying my playing cards. (they are displayed as an ImageIcon within the JLabel)

Anyway, I've set up a MouseListener interface so that when a user clicks one of the JLabel's, it will execute some code. Everything seemed to be working great until I noticed that if you click in between the JLabel's, it will register as though you actually clicked ON the JLabel immediately to the left. I've tried adjusting the size of my JLabel's using something like lbl1.setSize(25,20); but it seems to do nothing. I can't figure why the heck it's doing this, but more importantly I can't seem to fix it.

Any help or suggestions would be greatly appreciated!
20 years ago
Thanks for the help guys, I think I might try a combination of both ideas to see which I like best.
Currently, I'm displaying the cards as an ImageIcon within a JLabel. From what Saara said, I think I could use the MouseListener interface to capture when a user clicks on the JLabel, which could then run something like:
cardLabel1.setEnabled(false);

which would "grey out" the card - making it appear as though the user selected it to be discarded. If I can't get that to work well, though, then I may try the JButton approach and just put the ImageIcon inside the button.
I may have more questions after I try implementing these approaches, so you may hear back from me! Thanks again!
20 years ago
Hi all, I'm working on a Cribbage game (and have been for a while now!). I've got the majority of the game logic done, but what's really frustrating me is the GUI. I'm using Swing (probably not very well), and I'm not sure what the best way is for the user to click on cards - such as for discarding, playing a card during the "pegging" phase, etc.
Currently, I've got a very crude GUI that displays the players cards with a JRadioButton underneath each card. The user clicks the radiobuttons of the cards he/she wants to discard, then clicks a "discard" button. This works, but it looks a little amateurish. I've seen other card games where if you click the card, it actually moves. (some type of animation, I guess?) I'd even settle for the ability to just click the card and have the card "highlighted" in some fashion before clicking the discard button, rather than using radio buttons for the selection process.
Is there a class or classes in the Java library that I should take a peek at for this type of thing, or is there no easy way to do this?

I'm open to any suggestions!! thanks!
20 years ago
OK, I think I've got it now. So basically I had 2 sets of array variables - one set were the member variables (class fields) and the other set were local variables. When I was debugging and saw the arrays getting initialized, I was actually seeing the local variables getting initialized, which is why they were set to null once the execution flow left the Hand constructor.
The member variables never actually did get initialized, only declared.
20 years ago
Yep, that fixed it - thank Julian! I feel a bit dumb now, but oh well. Would there ever be a reason why you'd want to use the syntax I did? (by putting Card[] in front of the variable names) I'm not sure I fully understand how they are "masked", other than seeing the undesired behavior in my program.
Thanks again!
20 years ago
Hi gang, I'm currently building a cribbage game and am running into a weird issue with 2 arrays getting mysteriously set to null. First some code, then an explanation:

snippet from GUI class:


from gameFlow class:

From Hand class:



Basically what's happening here is when the user clicks the "Deal cards" button, the actionperformed method on the GUI class gets called and creates a "gameFlow" object. The gameFlow object handles the mechanics of 1 round of cribbage. (dealing, discarding, pegging, counting, etc) In the constructor of the gameFlow class you'll see that it creates a Hand object, initializing it to a variable called "hand1."
In the constructor of the Hand class, it creates a number of different variables, including 2 Card[] arrays with a size of 6 called playerHand and compHand. These represent 2 different cribbage hands.

**Here's the problem**: Immediately after the arrays are initialized in the Hand constructor, program execution goes back to the gameFlow class. At this point the arrays somehow get set to null. Then, a bit later in the program it tries to deal cards to populate the arrays. This throws a null pointer exception because the arrays are null - even though they were originally initialized to a size of 6.
Using JBuilder I've stepped through each line of the program and I can see the arrays getting initialized in the Hand constructor, then getting set to null once execution goes back to the gameFlow class. But I can't for the life of me figure out why. My first thought was that maybe it was the GC destroying my arrays, but you'll notice that there's another Card object created in the constructor called "upcard." This doesn't get set to null, only the 2 arrays do.
I'm guessing there's a simple explanation for what's happening, but I'm absolutely baffled. Thanks in advance for anyone who can offer some help!!
[ September 09, 2004: Message edited by: Alex McCormick ]
20 years ago
Thanks guys! I will definitely check out CardlayoutManager.
One other quetsion: I've seen a number of java card games on the web (applets, I believe) and they often have a bit of animation along with the displaying of the cards. For instance, when choosing which cards to discard from your hand, clicking on a card will cause the card to move or change position a bit. So far, I'm unable to figure out how to do that using JLabels. -which is kinda what got me thinking about using Graphics. My JLabels seem very "stationary," and if I wanted to move the cards around a bit I'm not sure how I would do that. Anyway, any additional ideas would be great!
Also, what do you mean by "using the Java classes when you can"? Is Graphics not a native Java class or something?
Thanks again!
20 years ago
Hi all, I've got what's probably a dumb question on using the Graphics class in my GUI. Let's say I'm building a simple card game like war. Should I be using Graphics to "draw" the card images onto my GUI? (I've got a .gif image for each card in the deck) I'm currently using JLabel's to display the cards, and although it works, I'm not sure that's the best way to do it. I've never done this before, so I'm not sure if Graphics is the "standard" way to display things on the GUI. Thanks for any help!
20 years ago
Thanks for the replies guys. Maybe I need to learn more about the MVC philosophy... Any good links you could recommend?
Hi all, quick question on event handling as it relates to OO design. I'm a relative novice to Java (and programming in general) and am building a card game program. Without going into specifics, I'm struggling with how to handle the flow of the game from player, to computer, and back again. For instance, the user would choose a card in his or her hand to play. Then, the computer will play a card. Now, it's the user's turn to play another card, and so on and so forth.
I've got a number of classes in my program, one being the GUI class and another being the gameFlow() class which handles (or is supposed to handle) the back and forth of the flow of the game. I've got the actionPeformed() method in my GUI class - but the only event handling I know how to do is the basic:
...if (event.getEvent() == startbutton) {
do this;
do that;
}
if (event.getEvent() == exitbutton) {
do this;
do that;
}
etc, etc.
This is how I was taught event-handling - which works fine for simple things like starting or ending the game, but it doesn't seem to be appropriate for situations in which the program is waiting for some type of response from the user (choosing a card to play, for instance). I would think there's a better way to do this, but I just don't know how.
Then again, maybe that IS how I'm supposed to do things.... Any help or advice would be greatly appreciated. I realize my question is pretty generic, but I didn't want to post a bunch of code when that might not really be necessary. In addition to creating a working program, I really want to strive to use good OO design wherever possible. This is a learning experiance, and I'd rather take the time to do it right, rather than cobble something together that works but is architected poorly.
TIA!!
I'm taking a bit of a wild stab here as I haven't done file reading in a while, but don't you have to include your readLine() in the loop? Otherwise I think it doesn't know to read the next line, which is why it keeps printing 1871 over and over.
21 years ago
Thanks to everyone who responded! Yes, my GUI is Swing. And the "big loop" in my gameFlow class actually is a while loop, but I can certainly change it. I guess part of my confusion is how to make the gameFlow class "wait" while the user is deciding certain things, such as which cards to discard. But it sounds like some of you are saying that the action of the user clicking the "discard" button for instance, should trigger a method call from the GUI to a relavent method in the gameFlow class. This does make sense, but I'll have to go back and figure out how to make that work.

I would also add that it's better to have an ActionListener per gui control, rather than one big one with lots of "if" statements in it.


Not sure exactly what you mean by this, could you elaborate? Thanks!!
Hi all, I'm a relatively inexperienced Java programmer and am in the middle of building a Cribbage game written in Java. This is probably a bit ambitious for my skill level, but it's been fun so far and a great learning experience.
However, I'm running into a few roadblocks and want to make sure that I'm building this thing "correctly" from a design perspective, before I get too much farther. Anyway, my situation is this:
In my GUI class, I've got an actionPerformed method to handle events such as the user clicking on buttons, etc. I've also created a "gameFlow" class which is meant to handle the flow of the game - from dealing the cards to discarding, scoring, pegging, etc, etc. (I also have other classes as well such as a Card class, Deck class, etc.)
Anyway, my gameFlow class has a big loop that will keep repeating the necessary phases of the game until someone wins, as determined by checking their score at the conclusion of the loop. However, since my event handling is done back in the GUI class, there seems to be a real dissociation between the gameFlow and the event handling. I'm starting to wonder if my gameFlow logic should really be put inside the GUI class, where its methods can be directly called by the events that trigger the actionPerformed method.
Can anyone suggest any ideas, or even reassure me that the way I'm doing it so far is correct? I realize my question is a bit vague, but I didn't want to just post a bunch of code and hope people could figure out what the heck I was getting at. Again, my main concern is not with the complexity of the code - I think I'm handling that just fine. It's the design and structure of my program and how the various objects interact with each other, and pass control around the program that stumps me sometimes. Any suggestions are greatly appreciated!!
Thanks Ernest, I've got the idea now. I assume you would also pass the game object to all classes, not just the GUI class? So in essence, you're kind of "cross-pollinating" the objects so they can reference each other easily.... Cool. Thanks again!
21 years ago