Graphics.drawString()不是绘图

时间:2011-03-20 23:05:43

标签: java awt

我正在为学校的最终项目创建一个简单的麻将游戏,并且似乎在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类设置了一个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()方法在左上角写入字符。问题?它没有写任何东西。我做错了什么?

4 个答案:

答案 0 :(得分:4)

g2d.drawString(charToWrite, 0, 0); 

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

g2d.drawString(charToWrite, 0, 10); 

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

答案 1 :(得分:0)

尝试

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

之前drawString()

如果您使用的是OS X,请尝试使用Apple Gothic。

答案 2 :(得分:0)

您正在调用color g2d.drawString()

例如:

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

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

答案 3 :(得分:0)

我很高兴我不是唯一一个在课堂上遇到这个问题的人。我尚未对此进行测试,但根据此网站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);