Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Expanding my knowledge in Java: GUIs, Images, OO concepts. Mini project Video Poker.

  1. #1
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Expanding my knowledge in Java: GUIs, Images, OO concepts. Mini project Video Poker.

    Hello a quick introduction my major experiance of Java has been college work I did 2 semesters one based around GUIs. In my first semester it was mostly theory which was very dissapointing but my second semester I had a very hands on lecturer and really enjoyed it unfortunatly this was a fairly advanced course and while I did well in it I don't think I had fully understood some fairly basic OO concepts such as inheritence since I didn't get assigments to practice them in the first semester. While I had all this theory in my head I misunderstood it and late in the second semester it all started making sense. I was fairly handy when it came to coding and debugging it's just the inheritence stuff like say Mountain Bike extends Bike that sort of stuff.

    A couple of months ago in my spare time I decided to try and do some Java and I wanted to do something with poker or at least cards my first challenge was fairly simple create a Card class and a Deck class and a Deck Test class. I had done this before but it was a nice refresher and got it done fairly quick.

    I then decided to build a GUI to show all the cards in the deck and as a test if a card was clicked a message box would pop up show what card it is. This wasn't too hard what I decided to do was create a Graphic Card class which extended Card. I don't have the code with me but I think the additional methods were things like drawCard which took in a compent to draw it on along with width height and I think where to draw it. Once again pretty simple and it worked it showed up all the cards(I got images for them off the internet).

    I then added a contains method which took in two integers (mouse x and mouse y) and returned a boolean if the point was in the card, so when a position was clicked it would make this method call to all the Graphic Cards and if it returned true it would call the method printCardName. Now at this stage I started working on a website with a friend and left this project.

    I went back to it over the weekend and wasn't really happy with the way this worked so changed things around, I wanted to expand on the poker idea and decided to make a simple video poker game. I changed it so there is a Class called CardCollection which is extended by Hand and Deck. I scrapped the graphic card class and added a StrengthChecker class(determines the strength of the hand passed to it) it only works for 5 card hands at the moment but I quite like it and it works I also started a VideoPoker and once again this worked quite well in the command line I was able to show a hand and toggle whether a card was held or not and print out the strength of the finishing hand, all very good. But then I started on a GUI for the game I was able to display the deck(well an image representing the deck) and the five cards, but putting any interactivity on top of this proved beyond me. What I want is that when the player clicks the deck it will invoke the dealFirstHand method and then when a player clicks the cards it toggles whether they are held or not and when the player clicks the deck again it will deal out second hand. Now I was kind of thinking Image would have a contains method similar to that of the shape class which I worked fairly extensively with before. Unfortunatly it didn't. Now I thought a way to avaoid this was to put some buttons in but I wasn't keen on that I also thought maybe since theres always going to be 5 cards and 1 deck I could just have an If point is in range1 then toggle this card1 that sort of thing. I think this would work but I thought there had to be a way to check if an image is clicked. I couldn't find any solution for this with a quick internet search I didn't delve too deeply but I was hoping someone could point me in the right direction for this.

    Now I don't know why this didn't occur to me yesterday but I've just checked BufferedImage would the getData method from that be a way to go about things?

    Also one other thing I that confused me. I cant exactly remember the problem my Deck class had an Array of Card objects I would have been under the impression that I could then put GraphicCard objects(which extends Card) but I don't think I was able to anyone help me on this?

    I think I had a few more minor things but I can't remember the exact details right now and I don't have the code with me to check so I might add these at another time.

    Anyway that was a fairly long winded post so thanks to anyone who took the time to read it, and if anything wasn't clear please don't hesitate to let me know.

    Regards
    Faz


  2. #2
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Expanding my knowledge in Java: GUIs, Images, OO concepts. Mini project Video Pok

    So what, exactly, is the question again?

  3. #3
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: Expanding my knowledge in Java: GUIs, Images, OO concepts. Mini project Video Pok

    Yes that was pretty terrible wasn't it I have a bad habit of going off track and rambling .

    Anyway I guess my main problem is I'm not too sure how to have interactive images. Should I just add a mouse listener to the GUI class? I'm not too sure how to check if a point is contained within an image.

    I don't know why I couldn't find anything on it yesterday(lack of sleep probably) I've found a few things to read up on now but a nudge in the right direction would be appreciated.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Expanding my knowledge in Java: GUIs, Images, OO concepts. Mini project Video Pok

    presumably you're going to add you're image to some sort of a Panel, right? In which case, just add a MouseListener to the JPanel and parse where the hit test comes up, or you can extend JPanel and create a special image panel and have Swing handle if the hit was inside that panel or not (simply add a MouseListener)

  5. #5
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: Expanding my knowledge in Java: GUIs, Images, OO concepts. Mini project Video Pok

    Quote Originally Posted by helloworld922 View Post
    presumably you're going to add you're image to some sort of a Panel, right? In which case, just add a MouseListener to the JPanel and parse where the hit test comes up, or you can extend JPanel and create a special image panel and have Swing handle if the hit was inside that panel or not (simply add a MouseListener)
    The second thing was something along the lines of what I was thinking but I just went with the first way in the end just keep it simple sure. I might take a look at it another time. I got it all working but one thing that is bothering me now.

    The new problem is that the Hand seems to reorder when I wouldn't expect it to. The relevant classes here are Video Poker(which controls the game) Hand which extends CardCollection, Card and StrengthChecker which determines the strength of the hand.

    What happens is when the StrengthChecker is setting the strength (1 for pair, 8 for Straight Flush) I decided to reorder the cards in ascending order based on Rank(Two,Three....Ace) and then suit. This is done with the Hands orderCards() method and it uses a sort defined in the Card class if that makes sense.

    But the thing is I pass the Hand Object in the VideoPoker Object to the StrengthChecker and the Hand object inside the StrengthChecker is what I would expect to be ordered but the original Hand also gets reordered which seems strange to me

    The orderCards() function in the CardsCollection class looks like this
    public void orderCards()
    {
             Collections.sort(cards);
    }

    I mean when I call this method I would expect it to only sort the array in that hand object but it seems to reorder the one in the VideoPoker class. By the way the StrengthChecker class only returns Integers and Strings so it's not retrieving the hand back or anything.

    Would it make a diffence if I changed to this
    public void orderCards()
    {
             Collections.sort(this.cards);
    }

    I mean OO concepts is my weakpoint which is what I'm going to be working over the next few months but from an OO perscpetive that doesn't make any sense to me why is it sorting all the hand objects not just the one I call the method from?

    The way I sort it is described here:

    Object Ordering (The Java™ Tutorials > Collections > Interfaces)

    Once again apologies if I haven't been clear I'm not used to asking questions online.

  6. #6
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Expanding my knowledge in Java: GUIs, Images, OO concepts. Mini project Video Pok

    Quote Originally Posted by Faz View Post
    But the thing is I pass the Hand Object in the VideoPoker Object to the StrengthChecker and the Hand object inside the StrengthChecker is what I would expect to be ordered but the original Hand also gets reordered which seems strange to me.
    Sounds like you're just passing a reference to the Hand object. In Java, all access to objects is via references. Object variables are references that point to the object. When you pass your Hand object around and sort it, the object being referenced is sorted, and all variables (references) will now point to the sorted object. This is something you need to be aware of and careful about.

    If you want to have one sorted Hand and keep the original unsorted, you must make a new copy of the Hand to sort.

    This issue can be a reason for making a class immutable (e.g. like String), so that once constructed, you can only make changes by creating a modified copy. In your example, if the Hand class was immutable, your sort feature would return a new sorted copy of the original. If you're going to be passing this object around and also using it as a member variable in other classes, you may find it much safer to make it immutable. On the other hand, frequently modifying an immutable object can be a pain, and a bit slow. Often, classes like String have a mutable version (e.g. StringBuilder) that can be used as a temporary editable 'workspace' when you want to do a lot of modifications to the object in a method.

Similar Threads

  1. Expanding the capacity of an array
    By dubois.ford in forum Collections and Generics
    Replies: 5
    Last Post: May 7th, 2010, 05:58 AM
  2. increasing my java knowledge
    By doogiexiv in forum Java Theory & Questions
    Replies: 1
    Last Post: March 8th, 2010, 11:00 PM
  3. Help with GUIs please & methods
    By killerknight141 in forum Java Theory & Questions
    Replies: 1
    Last Post: February 2nd, 2010, 07:12 AM
  4. Please help Me out with this poker Problem
    By ckrisasa in forum Java Networking
    Replies: 1
    Last Post: November 29th, 2009, 02:31 AM
  5. Mini Sudoku game 4x4 ( four 2x2 grids) program
    By derky in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 16th, 2009, 07:39 AM