Question

I am working on a poker game in Java. I would like to know how to make the diamond and heart symbol red instead of white filled, since I cannot find it in the unicode list. Any help is appreciated.

EDIT: I would like to know how to paint the symbols to red.

This is my code so far:

import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class PokerHand extends Frame implements ActionListener {
    Button b = new Button("Click for new hand");
    boolean dealt[] = new boolean[52];
    String[] playercard = new String[10];
    String[] playersuit = new String[10];
    Random r = new Random();
    int drawn;

    public static void main(String args[]) {
        PokerHand ph = new PokerHand();
        ph.doIt();
    }

    public void doIt() {
        int ct;
        for (ct = 0; ct <= 9; ++ct) { // first loop that you learned tonight
            playercard[ct] = " ";
            playersuit[ct] = " ";
        } // ends for loop
        b.addActionListener(this);
        this.setLayout(new FlowLayout());
        this.add(b);
        this.setSize(500, 500);
        this.setVisible(true);
    }// ends doIt method

    public void paint(Graphics g) {
        g.setFont(new Font(null, 1, 30));
        g.drawString("Me", 400, 100);
        g.drawString(playercard[0], 30, 100);
        g.drawString(playersuit[0], 60, 100);
        g.drawString(playercard[1], 90, 100);
        g.drawString(playersuit[1], 120, 100);
        g.drawString(playercard[2], 150, 100);
        g.drawString(playersuit[2], 180, 100);
        g.drawString(playercard[3], 210, 100);
        g.drawString(playersuit[3], 240, 100);
        g.drawString(playercard[4], 270, 100);
        g.drawString(playersuit[4], 300, 100);
        g.drawString("You", 400, 200);
        g.drawString(playercard[5], 30, 200);
        g.drawString(playersuit[5], 60, 200);
        g.drawString(playercard[6], 90, 200);
        g.drawString(playersuit[6], 120, 200);
        g.drawString(playercard[7], 150, 200);
        g.drawString(playersuit[7], 180, 200);
        g.drawString(playercard[8], 210, 200);
        g.drawString(playersuit[8], 240, 200);
        g.drawString(playercard[9], 270, 200);
        g.drawString(playersuit[9], 300, 200);
    }

    public void actionPerformed(ActionEvent ae) {
        int ct;
        ct = 0;
        int card;
        int suit;
        // we draw 10 cards here
        while (ct < 10) { // a while loop in practice
            drawn = r.nextInt(52);
            if (dealt[drawn] != true) { // if in practice
                card = drawn % 13 + 1;
                suit = drawn / 13;
                dealt[drawn] = true;
                playercard[ct] = String.valueOf(card);
                if (card == 1) {
                    playercard[ct] = "A";
                }
                if (card == 11) {
                    playercard[ct] = "J";
                }
                if (card == 12) {
                    playercard[ct] = "Q";
                }
                if (card == 13) {
                    playercard[ct] = "K";
                }
                if (suit == 0) {
                    playersuit[ct] = "\u2660";
                }
                if (suit == 1) {
                    playersuit[ct] = "\u2661"; //change to red heart
                }
                if (suit == 2) {
                    playersuit[ct] = "\u2662"; //change to red diamond
                }
                if (suit == 3) {
                    playersuit[ct] = "\u2663";
                }
                ct = ct + 1;
            } // ends if
        }// ends while
        repaint();
        for (int x = 0; x <= 51; ++x)
            dealt[x] = false;
    }// ends method
}// ends the class
Was it helpful?

Solution

If your Unicode font has the hearts, clubs, diamonds and spades symbols, then all you need to do to draw them in the color of your choice is the same as drawing any other character in the color of your choice: simply use setColor.

Now you should start from the "filled" version of spades, hearts, diamonds and clubs, they are:

- spade:    \u2660
- heart:    \u2665    (use 2665 instead of 2661)
- diamonds: \u2666    (use 2666 instead of 2662)
- clubs:    \u2663b

Then you simply do:

g.setColor(Color.RED);
g.drawString("\u2665");

And then you change the color, say, back to black to draw text and back to red/black for suits (or red, black, blue and green if you want to use a "four-color deck").

OTHER TIPS

This is (FINALLY) working for me! Ultimately the unicode doesn't change for the suits themselves, it's the \uFE0F that makes it "pop" and give it the color.

public enum Suit {

    SPADES("\u2660\uFE0F"), HEARTS("\u2665\uFE0F"), DIAMONDS("\u2666\uFE0F"), CLUBS("\u2663\uFE0F");

    private final String icon;

    Suit(String icon) {
        this.icon = icon;
    }

    public String getIcon() {
        return icon;
    }
}

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top