Graphics.drawString() 不是绘图

发布于 2024-10-24 18:46:30 字数 5712 浏览 3 评论 0原文

我正在为学校的期末项目创建一个简单的麻将游戏,并且似乎在使用 Graphics/Graphics2D 对象上的 drawString() 方法时遇到了一些问题。我调用 drawString 方法,但没有看到屏幕上写入任何内容。

在我的场景中,我扩展了 JFrame 类并重写了 paintComponent() 方法来创建自定义图形对象,特别是麻将牌。使用各种数组中描述的多边形,我创建了一个图块的人造 3D 视图,绘制了图块的正面、右侧和底部。这些多边形使用 GradientPaint 对象进行填充,以使图块具有更好的外观。它看起来像这样:

Rendered Tile

我的 Tile 类看起来像这样(注意:一些为简洁起见,省略了代码):

public class Tile extends JPanel
{
    private int _width;
    private int _height;
    private int _depth;

    /**
     * Accessor to get the center of the face of the rendered
     * mahjong tile.
     *
     * @return A point containing the center of the face of the tile.
     */
    public Point getCenter()
    {
        return new Point(_width / 2, _height / 2);
    }

    /**
     * The default constructor creates a tile that is proportionally
     * calculated to 80 pixels wide.
     */
    public Tile()
    {
        this(80);
    }

    /**
     * Given the width parameter a mahjong tile is drawn according to
     * the proportions of a size 8 mahjong tile.
     *
     * @param width The width of the tile to be rendered.
     */
    public Tile(int width)
    {
        _width = width;
        _height = (int)(width * 1.23);
        _depth = (int)((width * 0.3) / 2);

        setPreferredSize(new Dimension((_width + _depth) + 1, (_height + _depth) + 1));
    }

    @Override public void paintComponent(Graphics g)
    {
        // ... setup polygon arrays ...

        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D)g;

        // Turn on anti-aliasing.
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        // ... setup various gradients ...

        // Fill the face of the tile.
        g2d.setPaint(faceGradient);
        g2d.fillPolygon(faceXPolyPoints, faceYPolyPoints, faceXPolyPoints.length);

        // Fill the right side of the top portion of the tile.
        g2d.setPaint(ivoryRightSideGradient);
        g2d.fillPolygon(ivoryRightSideXPolyPoints, ivoryRightSideYPolyPoints, ivoryRightSideXPolyPoints.length);

        // Fill the bottom side of the top portion of the tile.
        g2d.setPaint(ivoryBottomGradient);
        g2d.fillPolygon(ivoryBottomXPolyPoints, ivoryBottomYPolyPoints, ivoryBottomXPolyPoints.length);

        // Fill the right side of the bottom portion of the tile.
        g2d.setPaint(jadeRightSideGradient);
        g2d.fillPolygon(jadeRightSideXPolyPoints, jadeRightSideYPolyPoints, jadeRightSideXPolyPoints.length);

        // Fill the bottom side of the bottom portion of the tile.
        g2d.setPaint(jadeBottomGradient);
        g2d.fillPolygon(jadeBottomXPolyPoints, jadeBottomYPolyPoints, jadeBottomXPolyPoints.length);

        // Draw the outlines for the tile.
        g2d.setPaint(Color.BLACK);
        g2d.drawPolygon(faceXPolyPoints, faceYPolyPoints, faceXPolyPoints.length);
        g2d.drawPolygon(ivoryRightSideXPolyPoints, ivoryRightSideYPolyPoints, ivoryRightSideXPolyPoints.length);
        g2d.drawPolygon(ivoryBottomXPolyPoints, ivoryBottomYPolyPoints, ivoryBottomXPolyPoints.length);
        g2d.drawPolygon(jadeBottomXPolyPoints, jadeBottomYPolyPoints, jadeBottomXPolyPoints.length);
        g2d.drawPolygon(jadeRightSideXPolyPoints, jadeRightSideYPolyPoints, jadeRightSideXPolyPoints.length);
    }
}

正如您所知,麻将牌有许多不同类型,其中一种是字符牌,在牌面上显示各种汉字。因此,我的 Tile 类设置了图块的基本绘图,并创建了一个新的 CharacterTile 类来扩展/继承 Tile 类。该类的代码如下:

public class CharacterTile extends Tile
{
    private Character _character;

    public CharacterTile(Character character)
    {
        super();

        _character = character;
    }

    @Override public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D)g;
        String charToWrite;


        switch (_character)
        {
            case ONE:
                charToWrite = "\u4E00";
                break;
            case TWO:
                charToWrite = "\u4E8C";
                break;
            case THREE:
                charToWrite = "\u4E09";
                break;
            case FOUR:
                charToWrite = "\u56DB";
                break;
            case FIVE:
                charToWrite = "\u4E94";
                break;
            case SIX:
                charToWrite = "\u516D";
                break;
            case SEVEN:
                charToWrite = "\u4E03";
                break;
            case EIGHT:
                charToWrite = "\u516B";
                break;
            case NINE:
                charToWrite = "\u4E5D";
                break;
            case NORTH:
                charToWrite = "\u5317";
                break;
            case EAST:
                charToWrite = "\u6771";
                break;
            case WEST:
                charToWrite = "\u897F";
                break;
            case SOUTH:
                charToWrite = "\u5357";
                break;
            case RED:
                charToWrite = "\u4E2D";
                break;
            case GREEN:
                charToWrite = "\u767C";
                break;
            case WAN:
                charToWrite = "\u842C";
                break;
            default:
                charToWrite = "?";
                break;
        }

        g2d.drawString(charToWrite, 0, 0);
    }
}

如您所见,CharacterTile 类的默认构造函数接受指示所需面值的 enum。在重写的 PaintComponent 中,我有一个 switch 语句,它设置要写入的适当字符,然后调用 g2d.drawString() 方法在左上角写入字符。问题?它什么也没写。我做错了什么?

I am creating a simple Mahjong game for a final project at school and seem to be having some troubles with the drawString() method on the Graphics/Graphics2D object. I call the drawString method and see nothing written to the screen.

In my scenario I have extended the JFrame class and overridden the paintComponent() method to create a custom graphical object, specifically a mahjong tile. Using polygons described in various arrays I create a faux 3D view of a tile drawing the face, right side, and bottom of the tile. These polygons are filled using GradientPaint objects to give the tile a better appearance. Here is what it looks like:

Rendered Tile

My Tile class looks like this (note: some code was omitted for brevity):

public class Tile extends JPanel
{
    private int _width;
    private int _height;
    private int _depth;

    /**
     * Accessor to get the center of the face of the rendered
     * mahjong tile.
     *
     * @return A point containing the center of the face of the tile.
     */
    public Point getCenter()
    {
        return new Point(_width / 2, _height / 2);
    }

    /**
     * The default constructor creates a tile that is proportionally
     * calculated to 80 pixels wide.
     */
    public Tile()
    {
        this(80);
    }

    /**
     * Given the width parameter a mahjong tile is drawn according to
     * the proportions of a size 8 mahjong tile.
     *
     * @param width The width of the tile to be rendered.
     */
    public Tile(int width)
    {
        _width = width;
        _height = (int)(width * 1.23);
        _depth = (int)((width * 0.3) / 2);

        setPreferredSize(new Dimension((_width + _depth) + 1, (_height + _depth) + 1));
    }

    @Override public void paintComponent(Graphics g)
    {
        // ... setup polygon arrays ...

        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D)g;

        // Turn on anti-aliasing.
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        // ... setup various gradients ...

        // Fill the face of the tile.
        g2d.setPaint(faceGradient);
        g2d.fillPolygon(faceXPolyPoints, faceYPolyPoints, faceXPolyPoints.length);

        // Fill the right side of the top portion of the tile.
        g2d.setPaint(ivoryRightSideGradient);
        g2d.fillPolygon(ivoryRightSideXPolyPoints, ivoryRightSideYPolyPoints, ivoryRightSideXPolyPoints.length);

        // Fill the bottom side of the top portion of the tile.
        g2d.setPaint(ivoryBottomGradient);
        g2d.fillPolygon(ivoryBottomXPolyPoints, ivoryBottomYPolyPoints, ivoryBottomXPolyPoints.length);

        // Fill the right side of the bottom portion of the tile.
        g2d.setPaint(jadeRightSideGradient);
        g2d.fillPolygon(jadeRightSideXPolyPoints, jadeRightSideYPolyPoints, jadeRightSideXPolyPoints.length);

        // Fill the bottom side of the bottom portion of the tile.
        g2d.setPaint(jadeBottomGradient);
        g2d.fillPolygon(jadeBottomXPolyPoints, jadeBottomYPolyPoints, jadeBottomXPolyPoints.length);

        // Draw the outlines for the tile.
        g2d.setPaint(Color.BLACK);
        g2d.drawPolygon(faceXPolyPoints, faceYPolyPoints, faceXPolyPoints.length);
        g2d.drawPolygon(ivoryRightSideXPolyPoints, ivoryRightSideYPolyPoints, ivoryRightSideXPolyPoints.length);
        g2d.drawPolygon(ivoryBottomXPolyPoints, ivoryBottomYPolyPoints, ivoryBottomXPolyPoints.length);
        g2d.drawPolygon(jadeBottomXPolyPoints, jadeBottomYPolyPoints, jadeBottomXPolyPoints.length);
        g2d.drawPolygon(jadeRightSideXPolyPoints, jadeRightSideYPolyPoints, jadeRightSideXPolyPoints.length);
    }
}

As you may know, there are many different types of mahjong tiles one being a character tile that displays various Chinese characters on the tile face. So my Tile class sets up the basic drawing of a tile and I create a new CharacterTile class that extends/inherits the Tile class. The code for that class follows:

public class CharacterTile extends Tile
{
    private Character _character;

    public CharacterTile(Character character)
    {
        super();

        _character = character;
    }

    @Override public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D)g;
        String charToWrite;


        switch (_character)
        {
            case ONE:
                charToWrite = "\u4E00";
                break;
            case TWO:
                charToWrite = "\u4E8C";
                break;
            case THREE:
                charToWrite = "\u4E09";
                break;
            case FOUR:
                charToWrite = "\u56DB";
                break;
            case FIVE:
                charToWrite = "\u4E94";
                break;
            case SIX:
                charToWrite = "\u516D";
                break;
            case SEVEN:
                charToWrite = "\u4E03";
                break;
            case EIGHT:
                charToWrite = "\u516B";
                break;
            case NINE:
                charToWrite = "\u4E5D";
                break;
            case NORTH:
                charToWrite = "\u5317";
                break;
            case EAST:
                charToWrite = "\u6771";
                break;
            case WEST:
                charToWrite = "\u897F";
                break;
            case SOUTH:
                charToWrite = "\u5357";
                break;
            case RED:
                charToWrite = "\u4E2D";
                break;
            case GREEN:
                charToWrite = "\u767C";
                break;
            case WAN:
                charToWrite = "\u842C";
                break;
            default:
                charToWrite = "?";
                break;
        }

        g2d.drawString(charToWrite, 0, 0);
    }
}

As you can see the default constructor for the CharacterTile class accepts an enum indicating the desired face value. Inside the overridden paintComponent I have a switch statement that sets the appropriate character to write and then I call the g2d.drawString() method to write the character in the upper left corner. The problem? It doesn't write anything. What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

小巷里的女流氓 2024-10-31 18:46:30
g2d.drawString(charToWrite, 0, 0); 

绘制字符串时需要指定底部/左侧坐标。尝试如下:

g2d.drawString(charToWrite, 0, 10); 

此外,您需要确保组件具有有效的大小。默认情况下,大小为 (0, 0),这意味着没有任何内容可绘制。这通常是通过指定首选尺寸然后让布局管理器设置尺寸来完成的。

g2d.drawString(charToWrite, 0, 0); 

You need to specify the bottom/left coordinate when you draw a string. Try something like:

g2d.drawString(charToWrite, 0, 10); 

Also, you need to make sure the component has a valid size. By default the size is (0, 0), which means there is nothing to paint. This is generally done by specifying a preferred size and then let the layout manager set the size.

君勿笑 2024-10-31 18:46:30

如果您使用的是 OS X,请

String fontString = "MS Gothic";
Font font = new Font(fontString, Font.PLAIN, 24);
g2d.setFont(font);

drawString() 之前

尝试尝试 Apple Gothic。

Try

String fontString = "MS Gothic";
Font font = new Font(fontString, Font.PLAIN, 24);
g2d.setFont(font);

before drawString()

try Apple Gothic if you are on OS X.

ま昔日黯然 2024-10-31 18:46:30

您使用什么颜色调用g2d.drawString()

例如:

    //add this line before next
    g2d.setBackground(Color.BLACK); //or 'g2d.setPaint(Color.BLACK);'
    g2d.drawString(charToWrite, 0, 0);

您确定当前字体包含您要绘制的符号吗? - 更改字体。

With what color are you calling g2d.drawString()?

For example:

    //add this line before next
    g2d.setBackground(Color.BLACK); //or 'g2d.setPaint(Color.BLACK);'
    g2d.drawString(charToWrite, 0, 0);

Are you sure, that current font has the symbols you are trying to paint? - Change the font.

阳光下慵懒的猫 2024-10-31 18:46:30

我很高兴我不是唯一一个在课堂上遇到这个问题的人。我还没有测试过这个,但根据这个网站 http ://www.herongyang.com/Swing/JFrame-Draw-Chinese-Character-on-Frame.html 此字体应该适用于创建创建字符所需的字体。

g2d.setFont(new Font("SimSun",Font.PLAIN, 12));
g2d.drawString(charToWrite, 0, 0);

I am glad I am not the only one having this issue for class. I haven't tested this yet but according to this site http://www.herongyang.com/Swing/JFrame-Draw-Chinese-Character-on-Frame.html this font should work for creating the font that you need to create the characters.

g2d.setFont(new Font("SimSun",Font.PLAIN, 12));
g2d.drawString(charToWrite, 0, 0);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文