Java扑克游戏 - 将钻石和心脏变为红色

时间:2014-04-23 23:57:27

标签: java unicode poker

我正在使用Java进行扑克游戏。 我想知道如何使钻石和心脏符号变成红色而不是白色填充,因为我在unicode列表中找不到它。 任何帮助表示赞赏。

编辑:我想知道如何将符号绘制成红色。

到目前为止,这是我的代码:

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

2 个答案:

答案 0 :(得分:1)

如果你的Unicode字体有心形,球形,钻石和黑桃符号,那么你用你选择的颜色绘制它们所需要的就像用你选择的颜色绘制任何其他字符一样:只需使用的setColor

现在你应该从&#34;填充&#34;黑桃,心,钻石和俱乐部的版本,它们是:

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

然后你只需:

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

然后你改变颜色,比如说,回到黑色来绘制文字,然后回到红色/黑色的西装(或红色,黑色,蓝色和绿色,如果你想使用&#34;四色套牌&# 34。)

答案 1 :(得分:1)

这(最终)为我工作!最终,unicode不会针对西服本身进行更改,而是\ uFE0F使其“弹出”并为其赋予颜色。

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