Java 扑克游戏 - 将钻石和红心更改为红色

标签 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

最佳答案

如果您的 Unicode 字体有红心、梅花、菱形和黑桃符号,那么用您选择的颜色绘制它们所需要做的就是用您选择的颜色绘制任何其他字符:只需使用设置颜色

现在你应该从黑桃、红心、方 block 和梅花的“填充”版本开始,它们是:

- 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");

然后你改变颜色,比如说,回到黑色来绘制文本,回到红色/黑色来绘制套装(或者如果你想使用“四色牌”,则改变为红色、黑色、蓝色和绿色)。

关于Java 扑克游戏 - 将钻石和红心更改为红色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23257373/

相关文章:

Java:输入流标记限制

c# - 来自 C# 的 javascript unicode 属性

正则表达式计算直扑克手 - 使用 ASCII 码

java - 计算字符串 java 中某个元素出现次数的代码

oracle - 如何从可能的编码列表中将 Oracle VARCHAR2 值转换为 UTF-8?

python - 在 python 中逐行创建一个大数据集

java - 套接字超时异常 : Read timed out httpclient

java - JodaTime:如何从 LocalTime 获取格式为 "HH:mm Z"的字符串表示形式

java - 将我的 Java 开发从 Windows 切换到 Mac 时,我应该注意什么问题或好处吗?

c++ - 计算 C++ 中的 unicode 字符