java-Graphics.drawString()未绘制

我正在为学校的最终项目创建一个简单的麻将游戏,似乎在Graphics / Graphics2D对象上的drawString()方法遇到了一些麻烦.我调用drawString方法,但看不到任何内容写入屏幕.

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

我的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的基本图形,并创建了一个扩展/继承Tile类的新CharacterTile类.该类的代码如下:

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类的默认构造函数接受一个表示所需面值的枚举.在覆盖的paintComponent内部,我有一个switch语句,该语句设置要写入的适当字符,然后调用g2d.drawString()方法在左上角写入字符.问题?它什么也没写.我究竟做错了什么?

解决方法:

g2d.drawString(charToWrite, 0, 0); 

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

g2d.drawString(charToWrite, 0, 10); 

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

上一篇:关于DockerFile组件,java8新特性的AWT组件,


下一篇:java awt小程序菜单乱码怎么解决