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
类设置了图块的基本绘图,并创建了一个新的 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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
绘制字符串时需要指定底部/左侧坐标。尝试如下:
此外,您需要确保组件具有有效的大小。默认情况下,大小为 (0, 0),这意味着没有任何内容可绘制。这通常是通过指定首选尺寸然后让布局管理器设置尺寸来完成的。
You need to specify the bottom/left coordinate when you draw a string. Try something like:
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.
如果您使用的是 OS X,请
在
drawString()
之前尝试尝试 Apple Gothic。
Try
before
drawString()
try Apple Gothic if you are on OS X.
您使用什么
颜色
调用g2d.drawString()
?例如:
您确定当前字体包含您要绘制的符号吗? - 更改字体。
With what
color
are you callingg2d.drawString()
?For example:
Are you sure, that current font has the symbols you are trying to paint? - Change the font.
我很高兴我不是唯一一个在课堂上遇到这个问题的人。我还没有测试过这个,但根据这个网站 http ://www.herongyang.com/Swing/JFrame-Draw-Chinese-Character-on-Frame.html 此字体应该适用于创建创建字符所需的字体。
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.