3

I write a game with cards. And I'm able to output with String. However, I'd like to change the display of String into images. I'm able to use JFrame and GridLayout to show four cards in 2*2 image.

My question is that I have a .png for the deck of cards, how can I get individual cards accordingly?

Class Card

package Chapter7;

public class Card {
    Suit suit;
    Rank rank;

    public Card(Suit suit, Rank rank) {
        this.suit = suit;
        this.rank = rank;
    }

    public Rank getRank() {
        return this.rank;
    }

    public Suit getSuit() {
        return this.suit;
    }

    public String toString() {
        return this.rank + " of " + this.suit;
    }

    public enum Suit {
        CLUBS(1), SPADE(2), HEARTS(3), DIAMONDS(4);

        int value;

        Suit(int v) {
            this.value = v;
        }

        int getSuit() {
            return this.value;
        }
    }

    public enum Rank {
        ACE(1), DEUCE(2), THREE(3), FOUR(4), FIVE(5), SIX(6), SEVEN(7), EIGHT(8), NINE(9), TEN(10), JACK(11), QUEEN(12), KING(13);

        int rank;   

        Rank(int num) {
            this.rank = num;
        }

        int getNumber() {
            return this.rank;
        }
    }
}

Class Deck

package Chapter7;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import Chapter7.Card.Rank;
import Chapter7.Card.Suit;

public class Deck {
    List <Card> deck = new ArrayList <Card> ();

    Deck() {
        for(Suit s : Suit.values()) {
            for(Rank r : Rank.values()) {
                deck.add(new Card(s, r));
            }
        }
//      System.out.println("A new deck created!!!");
//      for(Card c : this.deck) {
//          System.out.println(c.toString());
//      }
    }

    public void shuffle(){
        this.deck.clear();
        Deck ret = new Deck();
        for(int i = 0; i < 52; i++) {
            int j = (int) (Math.random() * (52-i));
            deck.add(ret.deck.remove(j));
        }
//      System.out.println("After shuffle!!!");
//      for(Card c : this.deck) {
//          System.out.println(c.toString());
//      }
    }

}

package Chapter7;

import java.io.IOException;



public class Game {

    public static void main(String args[]) throws IOException {

        Deck deck = new Deck();
        deck.shuffle();
        int aGame = 4;
        for(int i = 0; i < 52; i++) {
            if(aGame == 1) {
                System.out.println(deck.deck.get(i).toString());
                System.out.println("Start!!!");
                while (true) {
                    char c = (char) System.in.read();
                    if (c == '\n') {
                        break;
                    }
                }

                aGame = 4;
//              if(i == 51) {
//                  deck.shuffle();
//                  i = -1;
//              }
            }
            else if(aGame == 4) {
                System.out.println("Calculate based on the following four cards!");
                System.out.println(deck.deck.get(i).toString());
                aGame --;
            }
            else {
                System.out.println(deck.deck.get(i).toString());
                aGame --;
            }
        }
    }



}

Class LoadImageApp

Class to load image in 2*2 GridLayout.

package Chapter7;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;

/**
 * This class demonstrates how to load an Image from an external file
 */
public class LoadImageApp extends Component {

    BufferedImage img;

    public void paint(Graphics g) {
        g.drawImage(img, 0, 0, null);
    }

    public LoadImageApp(String s) {
       try {
           img = ImageIO.read(new File(s));
       } catch (IOException e) {
       }

    }

    public Dimension getPreferredSize() {
        if (img == null) {
             return new Dimension(100,100);
        } else {
           return new Dimension(img.getWidth(null), img.getHeight(null));
       }
    }

    public static void main(String[] args) {

        JFrame f = new JFrame("Load Image Sample");
        GridLayout grid = new GridLayout(2, 2);
        f.setLayout(grid);

        f.addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });

        f.add(new LoadImageApp("27.jpg"));
        f.add(new LoadImageApp("hw6.jpg"));
        f.add(new LoadImageApp("27.jpg"));
        f.add(new LoadImageApp("hw6.jpg"));
        f.pack();
        f.setVisible(true);
    }
}

How should I change these two classes to display?

The .png with deck of cards is here.

http://math.hws.edu/javanotes/c13/s1.html

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Zoe
  • 997
  • 4
  • 10
  • 19

1 Answers1

2

I have a .png for the deck of cards, how can I get individual cards accordingly?

Use the subImage(..) method as detailed in this answer.

That is a single image broken into 9 sub-images. 4 are used for the buttons (with an alternate 'red bordered' icon for the pressed state) and 5 for labels.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433