gpt4 book ai didi

java - Graphics.drawString() 不是绘图

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:26:21 26 4
gpt4 key购买 nike

我正在为学校的期末项目创建一个简单的麻将游戏,似乎在使用 Graphics/Graphics2D 对象上的 drawString() 方法时遇到了一些问题。我调用了 drawString 方法,但没有看到任何内容写入屏幕。

在我的场景中,我扩展了 JFrame 类并覆盖了 paintComponent() 方法来创建自定义图形对象,特别是麻将牌。使用各种数组中描述的多边形,我创建了一个瓷砖的人造 3D View ,绘制了瓷砖的正面、右侧和底部。这些多边形使用 GradientPaint 对象进行填充,以使图 block 具有更好的外观。这是它的样子:

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 类设置了图 block 的基本绘图,我创建了一个新的 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() 方法在左上角写入字符.问题?它不写任何东西。我做错了什么?

最佳答案

g2d.drawString(charToWrite, 0, 0); 

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

g2d.drawString(charToWrite, 0, 10); 

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

关于java - Graphics.drawString() 不是绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5372195/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com