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 3 of 3

Thread: How to add card images to Blackjack game??

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to add card images to Blackjack game??

    I have completed the code for the game however I'm having trouble adding the card images to the application.

    import javax.swing.*; 
    import javax.swing.ImageIcon.*;
     
     import javax.swing.border.*;  
     
     import java.awt.*;  
     
     import java.awt.event.*;  
     
     import java.awt.image.*;  
     
     import java.util.*;  
     
     import java.io.*;  
     
     import javax.imageio.*;  
     
     
     
     public class BlackJack extends JFrame{   
     
         int cash = 0;   
     
         int currentBet = 0;   
     
         ArrayList<Card> deck;   
     
         Card[] usersCards = new Card[10];   
     
         Card[] dealersCards = new Card[10];   
     
         private JButton buttonHit, buttonStay, buttonDeal, buttonCashOut;   
     
         private JPanel userPane, dealerPane, userCardPane;   
     
         private JLabel labelMoney, labelBet;
     
     
         BufferedImage img;   
     
         final int cardWidth = 700;   
     
         final int cardHeight = 600;   
         boolean gameOn = true;   
     public BlackJack() {  
     
         setTitle("BlackJack");    
     
         setSize(450,500);     
     
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       
     
         setLayout(new BorderLayout());  
     
         setVisible(true);     
     
         setBackground(Color.green);       
     
         buildTable();     
     
         gameInit();       							
         repaint();  
         }  
     public void paint(Graphics g) {       
     
         super.paint(g);       
     
         g.drawString("Dealers Hand", 20, 60);
     
    	g.drawString("Sum: "+getDealerSum(), 140, 60);     
     
         g.drawString("Your Hand", 20, 230);           
     
         g.drawString("Sum: "+getUserSum(), 90, 230);          
     
         int x1 = 10;      
     
         int y1 = 250;     
     
         int x2 = x1+cardWidth;    
     
         int y2 = y1+cardHeight;           
         int dx1 = 10;     
     
         int dy1 = 80;     
     
         int dx2 = dx1+cardWidth;      
     
         int dy2 = dy1+cardHeight;         
     
     
     
     
    File F = new File("Cards");   
     
     try {   
     
         img = ImageIO.read(new File(F));  
     
         } catch (IOException e) {}
     
     
    // Draw Dealers Cards 
    for(int i = 1; i<=getNumDealerCards(); i++)    
     
         {     
     
         g.drawImage(img, dx1+(cardWidth*(i-1)), dy1, dx2+(cardWidth*(i-1)), dy2, dealersCards[i-1].getX(),dealersCards[i-1].getY(), dealersCards[i-1].getX()+cardWidth ,dealersCards[i-1].getY()+cardHeight,null);  
     
         }         
     
         // Draw User Cards  
     
         for(int i = 1; i<=getNumUserCards(); i++)  
     
         {     
     
             g.drawImage(img, x1+(cardWidth*(i-1)), y1, x2+(cardWidth*(i-1)), y2, usersCards[i-1].getX(),  
     
                 usersCards[i-1].getY(), usersCards[i-1].getX()+cardWidth ,usersCards[i-1].getY()+cardHeight,null);  
     
         }      
         }    
     
         public int getNumUserCards()   
     
         {     
     
             int numCards = 0;     
     
             while(usersCards[numCards]!=null)  
     
             numCards++;  
            return numCards;  
     
         }   
         public int getNumDealerCards() {  
     
         int numCards = 0;  
     
         while(dealersCards[numCards]!=null)  
     
             numCards++;       
     
         return numCards;   
     
         } public int getUserSum()   
     
         {     
     
         int numCards = 0;     
     
         int sum = 0;      
     
         int cardVal = 0;  
     
         while(usersCards[numCards]!=null){  
     
             cardVal = usersCards[numCards].getValue();  
     
             if(cardVal == 1 && sum<=10)  
     
                     cardVal = 11;  
     
             else if(cardVal == 11)  
     
                     cardVal = 10;  
     
             else if(cardVal == 12)  
     
                     cardVal = 10;  
     
             else if(cardVal == 13)  
     
                     cardVal = 10;  
     
             sum+=cardVal;  
     
             numCards++;  
     
         }     
     
         return sum;  
     
      }   
     
      public int getDealerSum() {      
     
         int numCards = 0;     
     
         int dsum = 0;      
     
         int cardVal = 0;      
     
         while(dealersCards[numCards]!=null){          
     
         cardVal = dealersCards[numCards].getValue();          
     
         if(cardVal == 1 && dsum<=10)                
     
             cardVal = 11;  
     
             else if(cardVal == 11)  
     
                     cardVal = 10;  
     
             else if(cardVal == 12)  
     
                     cardVal = 10;  
     
             else if(cardVal == 13)  
     
                     cardVal = 10;  
     
             dsum+=cardVal;  
     
             numCards++;  
     
         }     
     
         return dsum;  
     
      }   
       public int setBet(int bet)  
     
       {  
     
         try{      
     
         bet = Integer.parseInt(JOptionPane.showInputDialog("Enter Your Bet: "));  
     
         }catch(NumberFormatException ex)  
     
         {   bet=0; }  
     
         if(bet <= 0 || bet > cash)  
     
         bet=setBet(0);  
     
             else  
     
             labelBet.setText(Integer.toString(bet));  
     
             return bet;  
     
      }  
     
      public void buildTable() {  
     
         //Dealer Pane
     
     
         dealerPane = new JPanel();  
     
         dealerPane.setBackground(Color.gray);  
     
        buttonDeal = new JButton("Deal");  
     
         buttonCashOut = new JButton("Walk Away");  
     
         dealerPane.add(buttonCashOut);  
     
         dealerPane.add(buttonDeal);  
     
         buttonDeal.addActionListener(new Dealer());  
     
         buttonCashOut.addActionListener(new Walk());  
     
     
     
         //UserCardPane 
     
         userCardPane = new JPanel();  
     
     
     
         userPane = new JPanel(new GridLayout(3,2));  
     
         Border names = BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Your Money");  
     
         userPane.setBorder(names);  
     
         userPane.setBackground(Color.gray);  
     
         buttonHit = new JButton("Hit");  
     
         buttonStay = new JButton("Stay");  
     
         labelBet = new JLabel("0");  
     
         labelMoney = new JLabel("0");  
     
         userPane.add(new JLabel("Total Cash left: "));  
         userPane.add(labelMoney);  
     
         userPane.add(new JLabel("Your Bet: "));  
     
         userPane.add(labelBet);  
     
         userPane.add(buttonHit);  
     
         userPane.add(buttonStay);  
     
         buttonHit.addActionListener(new HitMe());  
     
         buttonStay.addActionListener(new Stay());  
     
         add(dealerPane, BorderLayout.SOUTH);  
     
         add(userPane, BorderLayout.NORTH);  
     
          // Set Starting Money  
     
     
     
         cash = getStartingAmmt();  
     
         labelMoney.setText(Integer.toString(cash));  
     
         }    
          //Code for New Round   
     
         public void gameInit() {  
     
         currentBet = setBet(0);  
     
         usersCards = new Card[10];  
     
         dealersCards = new Card[10];  
     
         deck = new ArrayList<Card>(52);  
     
         for(int i = 0; i<52; i++)  
     
             deck.add(i, new Card((i%4),(i%13)+1));        
     
         // Give User and Dealer 2 Cards Each          
     
         usersCards[0] = pullRandomCard();  
     
         usersCards[1] = pullRandomCard();  
     
         dealersCards[0] = pullRandomCard();  
     
         dealersCards[1] = pullRandomCard();  
     
         repaint();   
     
         }   
         public int getStartingAmmt() {  
     
             int amnt = 0;  
     
         while(amnt<=0)  
     
         {  
     
             try{  
     
             amnt = Integer.parseInt(JOptionPane.showInputDialog("Enter Starting money:"));  
     
             }catch(NumberFormatException nfe){      amnt=0;     }  
     
         }  
     
         return amnt;  
     
         } 
         public Card pullRandomCard()  
     
         {  
     
            Random rand = new Random();  
     
                     return deck.remove(rand.nextInt(deck.size()));  
     
         }  
     
         //*********************EVENTS*******************\ \
         class Dealer implements ActionListener{  
     
             public void actionPerformed(ActionEvent ae)  
     
             {  
     
                 gameOn = true;  
     
                 gameInit();  
     
             } }      
    //Events
        //Hit Me  
     
         // Check the new sum of cards  
    	//if < 21 user can choose to hit/stay   
     
         class HitMe implements ActionListener{  
     
             public void actionPerformed(ActionEvent ae)  
     
             {  
     
                 if(gameOn){  
     
                 usersCards[getNumUserCards()] = pullRandomCard();  
     
                 repaint();  
     
                 if(getUserSum()>21)  
     
                 {  
     
                         // You Lose  
     
                     JOptionPane.showMessageDialog(null, "HAH You Lost!!!!");  
     
                     gameOn = false;  
     
                     cash-=currentBet;  
     
                     labelMoney.setText(Integer.toString(cash));  
     
                 }  
     
                 if(cash<=0)  
     
                 {  
     
                     JOptionPane.showMessageDialog(null,"Go get more money!!!");  
     
                     System.exit(0);  
    }  }   } }
         //Check for winner  
     
         class Stay implements ActionListener{  
     
             public void actionPerformed(ActionEvent ae)  
     
             {  
     
                 if(gameOn){  
     
                 gameOn = false;  
     
                 if(getUserSum()>21)  
     
                 {  
     
                     // You Lose  
     
                     JOptionPane.showMessageDialog(null, "HAH You Lost!!!");  
     
                     cash-=currentBet;  
     
                     labelMoney.setText(Integer.toString(cash));  
     
                 }  
     
                 //
     
                 else {  
     
                     while(getDealerSum()<17)  
     
                     {  
     
                       dealersCards[getNumDealerCards()] = pullRandomCard();  
     
                     }  
     
                     if(getDealerSum()>21)  
     
                     {  
     
                         // You Win  
     
                         JOptionPane.showMessageDialog(null, "You Win!!");  
     
                         cash+=currentBet;  
     
                         labelMoney.setText(Integer.toString(cash));  
     
                     }  
     
                     else{  
     
                         if(21-getUserSum() < 21-getDealerSum())  
     
                         {// You win  
     
                             JOptionPane.showMessageDialog(null, "You Win!!");  
     
                             cash+=currentBet;  
     
                             labelMoney.setText(Integer.toString(cash));  
     
                         }  
     
                         else if(getUserSum() == getDealerSum())  
     
                         {  
     
                             JOptionPane.showMessageDialog(null, "Tie...");  
     
                         }  
     
                         else // You Lose  
     
                         {  
     
                             JOptionPane.showMessageDialog(null, "HAH You Lost!");  
     
                             cash-=currentBet;  
     
                             labelMoney.setText(Integer.toString(cash));  
     
                         }  
                     }  
                 }  
                 repaint();  
     
                 if(cash<=0)  
     
                 {  
     
                     JOptionPane.showMessageDialog(null,"Go get more money!!!!");   
     
                 System.exit(0);  
                 }  
                 }  
             } } 
           //Leav Game  
          class Walk implements ActionListener{  
     
             public void actionPerformed(ActionEvent ae)  
     
             {  
     
                 JOptionPane.showMessageDialog(null, "You played and walked away with $" + cash);  
     
                 System.exit(0);  
     
             }  
     
         }   
     
     
     
         public static void main(String[] args) {  
     
             new BlackJack();   
         }  }



    I am using Bufferedimage however an image only displays a card(ex. 4clubs). When i try to call all the images by writing the filename(Cards) none of the images appear in the game. Any help would be appreciated!!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,112
    Thanks
    65
    Thanked 2,718 Times in 2,668 Posts

    Default Re: How to add card images to Blackjack game??

    Does this code compile without errors? I get 15 errors.
    What version of the JDK are you using?
    Running: D:\Java\jdk1.6.0_02\bin\javac.exe -Xlint -g -deprecation -classpath D:\JavaDevelopment\;.;..\..\..\..\. BlackJack3.java

    BlackJack3.java:97: cannot find symbol
    symbol : constructor File(java.io.File)
    location: class java.io.File
    img = ImageIO.read(new File(F));
    ^
    BlackJack3.java:107: cannot find symbol
    symbol : method getX()
    location: class Card
    g.drawImage(img, dx1+(cardWidth*(i-1)), dy1, dx2+(cardWidth*(i-1)), dy2, dealersCards[i-1].getX(),dealersCards[i-1].getY(), dealersCards[i-1].getX()+cardWidth ,dealersCards[i-1].getY()+cardHeight,null);
    ^
    BlackJack3.java:107: cannot find symbol
    symbol : method getY()
    location: class Card
    g.drawImage(img, dx1+(cardWidth*(i-1)), dy1, dx2+(cardWidth*(i-1)), dy2, dealersCards[i-1].getX(),dealersCards[i-1].getY(), dealersCards[i-1].getX()+cardWidth ,dealersCards[i-1].getY()+cardHeight,null);
    ^
    BlackJack3.java:107: cannot find symbol
    symbol : method getX()
    location: class Card
    g.drawImage(img, dx1+(cardWidth*(i-1)), dy1, dx2+(cardWidth*(i-1)), dy2, dealersCards[i-1].getX(),dealersCards[i-1].getY(), dealersCards[i-1].getX()+cardWidth ,dealersCards[i-1].getY()+cardHeight,null);
    ^
    BlackJack3.java:107: operator + cannot be applied to Card.getX,int
    g.drawImage(img, dx1+(cardWidth*(i-1)), dy1, dx2+(cardWidth*(i-1)), dy2, dealersCards[i-1].getX(),dealersCards[i-1].getY(), dealersCards[i-1].getX()+cardWidth ,dealersCards[i-1].getY()+cardHeight,null);
    ^
    BlackJack3.java:107: cannot find symbol
    symbol : method getY()
    location: class Card
    g.drawImage(img, dx1+(cardWidth*(i-1)), dy1, dx2+(cardWidth*(i-1)), dy2, dealersCards[i-1].getX(),dealersCards[i-1].getY(), dealersCards[i-1].getX()+cardWidth ,dealersCards[i-1].getY()+cardHeight,null);
    ^
    BlackJack3.java:107: operator + cannot be applied to Card.getY,int
    g.drawImage(img, dx1+(cardWidth*(i-1)), dy1, dx2+(cardWidth*(i-1)), dy2, dealersCards[i-1].getX(),dealersCards[i-1].getY(), dealersCards[i-1].getX()+cardWidth ,dealersCards[i-1].getY()+cardHeight,null);
    ^
    BlackJack3.java:117: cannot find symbol
    symbol : method getX()
    location: class Card
    g.drawImage(img, x1+(cardWidth*(i-1)), y1, x2+(cardWidth*(i-1)), y2, usersCards[i-1].getX(),
    ^
    BlackJack3.java:119: cannot find symbol
    symbol : method getY()
    location: class Card
    usersCards[i-1].getY(), usersCards[i-1].getX()+cardWidth ,usersCards[i-1].getY()+cardHeight,null);
    ^
    BlackJack3.java:119: cannot find symbol
    symbol : method getX()
    location: class Card
    usersCards[i-1].getY(), usersCards[i-1].getX()+cardWidth ,usersCards[i-1].getY()+cardHeight,null);
    ^
    BlackJack3.java:119: operator + cannot be applied to Card.getX,int
    usersCards[i-1].getY(), usersCards[i-1].getX()+cardWidth ,usersCards[i-1].getY()+cardHeight,null);
    ^
    BlackJack3.java:119: cannot find symbol
    symbol : method getY()
    location: class Card
    usersCards[i-1].getY(), usersCards[i-1].getX()+cardWidth ,usersCards[i-1].getY()+cardHeight,null);
    ^
    BlackJack3.java:119: operator + cannot be applied to Card.getY,int
    usersCards[i-1].getY(), usersCards[i-1].getX()+cardWidth ,usersCards[i-1].getY()+cardHeight,null);
    ^
    BlackJack3.java:158: cannot find symbol
    symbol : method getValue()
    location: class Card
    cardVal = usersCards[numCards].getValue();
    ^
    BlackJack3.java:196: cannot find symbol
    symbol : method getValue()
    location: class Card
    cardVal = dealersCards[numCards].getValue();
    ^
    BlackJack3.java:334: cannot find symbol
    symbol : constructor Card(int,int)
    location: class Card
    deck.add(i, new Card((i%4),(i%13)+1));
    ^
    BlackJack3.java:17: warning: [serial] serializable class BlackJack3 has no definition of serialVersionUID
    public class BlackJack3 extends JFrame{
    ^
    16 errors
    1 warning

  3. #3
    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: How to add card images to Blackjack game??

    Quote Originally Posted by Jayc44 View Post
    .. an image only displays a card(ex. 4clubs). When i try to call all the images by writing the filename(Cards) none of the images appear in the game.
    This makes no sense to me:
    what is an image supposed to display if not a card?
    how can you 'call' an image?
    what does 'writing the filename(Cards)' mean?

Similar Threads

  1. BlackJack Game can"t figure out how to finsh it.
    By flurr in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 13th, 2011, 10:06 AM
  2. Card Game help....
    By macFs89H in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 2nd, 2011, 07:55 AM
  3. Card Game Problem....Need Help
    By macFs89H in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 28th, 2011, 07:30 AM
  4. [SOLVED] BlackJack.. Random Card Assignment?
    By Hallowed in forum Object Oriented Programming
    Replies: 5
    Last Post: April 5th, 2011, 07:25 AM
  5. Help with blackjack game
    By santosd1118 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 12th, 2010, 12:55 AM