【问题标题】:Graphics.drawString() Isn't DrawingGraphics.drawString() 不是绘图
【发布时间】:2011-03-21 10:35:40
【问题描述】:

我正在为学校的期末项目创建一个简单的麻将游戏,但在 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 类设置了瓷砖的基本绘图,我创建了一个新的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() 方法在左上角写入字符。问题?它不写任何东西。我做错了什么?

【问题讨论】:

  • 您是否选择了包含这些汉字的字体? (注意:我对 AWT 一无所知,所以这个问题可能从根本上被误解了。)如果您使用纯 ol' ASCII 字符作为 charToWrite 值会发生什么?
  • 是的,先尝试写“A”之类的“普通”字符。
  • +1 一个学生提出的好问题。
  • 是的,我试过写普通的拉丁字符,结果是一样的。

标签: java awt


【解决方案1】:
g2d.drawString(charToWrite, 0, 0); 

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

g2d.drawString(charToWrite, 0, 10); 

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

【讨论】:

  • 它根本看不见。感谢您的回答!
【解决方案2】:

试试

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

drawString()之前

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

【讨论】:

    【解决方案3】:

    你用什么color 打电话给g2d.drawString()

    例如:

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

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

    【讨论】:

      【解决方案4】:

      我很高兴我不是唯一一个在课堂上遇到这个问题的人。我还没有测试过这个,但是根据这个网站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);
      

      【讨论】:

      • 你甚至不需要担心一些晦涩的字体。我正在使用 Arial,一切都很好。
      • 我最终让它使用 Arial Unicode MS 字体。 String fontString = "Arial Unicode MS";不过谢谢你的帖子。这对我帮助很大。
      猜你喜欢
      • 1970-01-01
      • 2021-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-01
      • 1970-01-01
      • 2019-12-30
      • 1970-01-01
      相关资源
      最近更新 更多