【发布时间】: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 一个学生提出的好问题。
-
是的,我试过写普通的拉丁字符,结果是一样的。