Results 1 to 5 of 5
  1. #1
    zimbabweo is offline Member
    Join Date
    Apr 2013
    Posts
    3
    Rep Power
    0

    Default Using GridBagLayout to display Blackjack hand

    Program compiles and runs okay, but card1 in lines 70-76 does not display. Nearly identical code in lines 30-36 does display a card.
    Ultimately I need to set up a loop to display a whole hand, but for now I'll be happy just to get one card showing inside the ActionListener.

    Java Code:
    package blackjack;
    
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    
    public class BJGame extends JFrame {
        private JButton newGame, hit, stand;
        private ImageIcon pack;
        private JLabel label1, label2;
        private ImageIcon[] hHand, pHand;
        Deck deck = new Deck(); // create new deck
        BJHand houseHand = new BJHand(); // create new hand for computer
        BJHand playerHand = new BJHand(); // create new hand for player
        GridBagConstraints c = new GridBagConstraints();
    
        BJGame() {
            setLayout(new GridBagLayout());
            // GridBagConstraints c = new GridBagConstraints();
            eventN startNewGame = new eventN();
            eventH hitMe = new eventH();
            eventS stay = new eventS();
            
            pack = new ImageIcon(getClass().getResource("../cardImages1/53.png"));
            label1 = new JLabel(pack);
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 0;
            c.gridy = 0;
            add(label1, c);
            
            newGame = new JButton("New Game");
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 0;
            c.gridy = 1;
            add(newGame, c);
            newGame.addActionListener(startNewGame);   
            
            hit = new JButton("Hit");
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 1;
            c.gridy = 1;
            add(hit, c);
            hit.setEnabled(false);
            hit.addActionListener(hitMe);
     
            stand = new JButton("Stand");
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 2;
            c.gridy = 1;
            add(stand, c);
            stand.setEnabled(false);
            stand.addActionListener(stay);
        }
        
        public class eventN implements ActionListener {
            @Override
            public void actionPerformed(ActionEvent ae) {
                // start a new game
                deck.clearDeck(); // reset currentCard (top card) to zero
                deck.shuffle(); // shuffle deck
                playerHand.clearHand(); // reset player hand 
                houseHand.clearHand(); // reset house hand
                
                ImageIcon card1;
                card1 = new ImageIcon(getClass().getResource("../cardImages1/52.png"));
                label2 = new JLabel(card1);
                c.fill = GridBagConstraints.HORIZONTAL;
                c.gridx = 0;
                c.gridy = 1;
                add(label2, c);
                
                for (int loop = 1; loop <= 2; loop++)
                {
                    playerHand.addCard(deck.dealCard());
                    System.out.printf("\nPlayer: ");
                    playerHand.displayHand();
                    System.out.printf("%s", playerHand.getPicture(loop-1));
                    houseHand.addCard(deck.dealCard());
                    System.out.printf("\nHouse: ");
                    houseHand.displayHand();
                    System.out.printf("%s", houseHand.getPicture(loop-1));
                }
                
                
                
                if (houseHand.getValue() == 21)
                    System.out.printf("\nHouse wins.");
                else if (playerHand.getValue() == 21)
                    System.out.printf("\nPlayer wins.");
                
            }
        }
        
        public class eventH implements ActionListener {
            @Override
            public void actionPerformed(ActionEvent ae) {
                // take a hit -- add action here
                
            }
        }
    
        public class eventS implements ActionListener {
            @Override
            public void actionPerformed(ActionEvent ae) {
                // stay -- add action here
                
            }
        }
            
        public static void main(String args[]){
            BJGame bjGame = new BJGame();
            bjGame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            bjGame.setVisible(true);
            bjGame.setTitle("BlackJack");
            bjGame.setSize(300,300);
        }
    }

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,227
    Rep Power
    6

    Default Re: Using GridBagLayout to display Blackjack hand

    When you add components to a visible GUI then the code should be like:

    Java Code:
    panel.add(...);
    panel.revalidate();
    panel.repaint();
    A better design for you GUI would probably be to use multiple panels. Create one panel for your buttons and then maybe add that panel to the NORTH of the BorderLayout, which is the default layout for the frame.

    Then you can create a second panel for your cards. You would then probably add this panel to the CENTER of the BorderLayout. You can use a GridBagLayout or any other layout you wish for this panel. Splitting the GUI into logical components generally makes it easier to work with.

  3. #3
    zimbabweo is offline Member
    Join Date
    Apr 2013
    Posts
    3
    Rep Power
    0

    Default Re: Using GridBagLayout to display Blackjack hand

    Quote Originally Posted by camickr View Post
    When you add components to a visible GUI then the code should be like:

    Java Code:
    panel.add(...);
    panel.revalidate();
    panel.repaint();
    A better design for you GUI would probably be to use multiple panels. Create one panel for your buttons and then maybe add that panel to the NORTH of the BorderLayout, which is the default layout for the frame.

    Then you can create a second panel for your cards. You would then probably add this panel to the CENTER of the BorderLayout. You can use a GridBagLayout or any other layout you wish for this panel. Splitting the GUI into logical components generally makes it easier to work with.

    Thanks camickr. I'll play around with the BorderLayout and see if I can make that work.

  4. #4
    zimbabweo is offline Member
    Join Date
    Apr 2013
    Posts
    3
    Rep Power
    0

    Default Re: Using GridBagLayout to display Blackjack hand

    I found out for the image display:

    1. This works:
    String imgPath = "../cardImages1/" + playerHand.getPicture(0);
    card1 = new ImageIcon(getClass().getResource(imgPath));

    2. and This works, to display a static card, but not one from a hand:
    card1 = new ImageIcon(getClass().getResource("../cardImages1/28.png");

    3. but this does not work. getResource() can not handle the String addition.
    I had to do the string addition in a separate line to construct the path first.
    card1 = new ImageIcon(getClass().getResource("../cardImages1/" + playerHand.getPicture(0));

    So #1 above is how I implemented it in my game.
    For the layout I still need to spend more time playing with BorderLayout.

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,227
    Rep Power
    6

    Default Re: Using GridBagLayout to display Blackjack hand

    I'm sure all 3 method work. This has nothing to do with the getResource() method. A String is a String. The String is resolved by the compiler. If you create it correctly then it will work.

    I always prefer method 1 for the simple reason that you can use System.out.println(imgPath) to make sure you are building the path correctly. With the other approaches you have no way to verify that the string you created is correct.

Similar Threads

  1. Blackjack
    By jediassassin37 in forum New To Java
    Replies: 2
    Last Post: 10-24-2012, 04:01 AM
  2. Help With BlackJack Game !
    By TangoMango in forum New To Java
    Replies: 1
    Last Post: 01-24-2011, 05:40 PM
  3. blackjack help
    By jordaejava in forum New To Java
    Replies: 1
    Last Post: 12-11-2009, 05:44 AM
  4. BlackJack help please,, adding hand values
    By javakid9000 in forum New To Java
    Replies: 1
    Last Post: 11-18-2007, 04:26 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •