- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在为学校的期末项目创建一个简单的麻将游戏,似乎在使用 Graphics/Graphics2D 对象上的 drawString()
方法时遇到了一些问题。我调用了 drawString
方法,但没有看到任何内容写入屏幕。
在我的场景中,我扩展了 JFrame
类并覆盖了 paintComponent()
方法来创建自定义图形对象,特别是麻将牌。使用各种数组中描述的多边形,我创建了一个瓷砖的人造 3D View ,绘制了瓷砖的正面、右侧和底部。这些多边形使用 GradientPaint
对象进行填充,以使图 block 具有更好的外观。这是它的样子:
我的 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/
使用 Graphics.DrawString 有什么缺点吗?要将一堆(相当静态的)文本渲染到屏幕外位图,请将其转换为 Texture2D一次,然后只需调用 SpriteBatch.Draw ,而不是使
我上传了一个带有 jpg 扩展名的图像,并在 .NET Web 应用程序中使用 Graphics.DrawString 在照片上添加了文本。然后,如果我应用其他编辑工具(例如旋转图像),添加的文本会变
我正在尝试创建一个带有一些文本的测试窗口,当我运行代码时,它不会绘制字符串。我指定了它的颜色。有人可以帮我解决这个问题吗? import javax.swing.*; import java.awt.
当我在 Eclipse 中运行此代码时,我在新窗口中打印了字符串和行,但它不是一个空白窗口,而是我当前窗口的屏幕截图,设置为我声明的大小。 /image/8KFU0.png 对我做错了什么有什么建议吗
我正在尝试制作这个 GUI,其中有一个文本字段(下面的变量查询),并且如果 GUI 注意到按下了某个键,它将绘制出输入到文本字段中的字符串。但是,如果我将字符串 str 初始化为“”,它在 GUI 上
我为了好玩而制作了一个随机行生成器,当我运行该程序时,“Y(结束):”字符串看起来很奇怪。数字显示为矩形符号,没有任何意义。 这是我的代码: public class LineFractal exte
我有一个数据库,所有值都存储在一个表中。我想使用 drawString() 方法从表中打印一个名称。我创建了一个结果集和数组列表。有什么方法可以在 drawString 方法中从数据库传递字符串??
就目前情况而言,我正在尝试将组件上的字符串绘制到框架中,SSCCE 将如下所示: // The component class through which I draw the string! pac
我正在使用drawString()在字符串上写入文本,但没有显示任何内容。我将颜色设置为白色并将坐标设置为(100,100)。我还有什么做错的吗? import java.awt.Color; imp
我正在尝试制作一个数字时钟,然后添加到 JPanel,现在当我在 taskPerformer 方法之外调用 DrawString 时它绘制但在内部时,它不绘制字符串!为什么会这样,我将如何实现这个类以
我有一个 Jpanel,例如宽度为 500,高度为 100。我有计算屏幕垂直中心 (y) 和水平中心 (x) 的公式。我用它来 drawString(myText, x, y) 但它将文本写在 jpa
我想使用 Graphics 和字符串外的矩形边框绘制字符串。 这是我已经在做的: public void paint(Graphics g) { Graphics2D g2d
我有一个 JPanel 对象,我将其用作绘制矩形的 Canvas 并调用 updateString(String c, int x, int) 方法来每当我在 main 中调用该方法时,绘制一个 St
我试图在图像上从右上角向左下角绘制文本。我的代码弄乱了拉绳的定位。到目前为止,这段代码是这样绘制的,但我需要帮助在这些红线之间绘制文本。 string img_src = "F:\\blank_img
我正在使用 C# 为 Windows Mobile 应用程序创建 fatal error 对话框。问题是当我尝试使用 DrawString 绘制堆栈跟踪时,一半的堆栈跟踪被剪掉了,因为 DrawStr
在我将一个字符串传递给 Graphics.DrawString() 之前,我想知道每个字符的确切位置 - 不仅仅是它的偏移量和宽度,还有它的带有上升/下降的精确边界框,以便我可以在字符级别进行碰撞检测
我有一个关于 drawString 函数质量的问题,目前我有这张图片: 基本上,您在底部中间看到的是一张按其大小的 125% 绘制的卡片。背景中的卡片大小分别为 50% 和 25%。 但是,我注意到描
在 Paint XP 或 Paint Windows 8 中,当您书写文本时,程序会自动提高字体质量(至少对于财务打印机而言)。我想知道如何使用下面的相同 Java 代码执行此操作。 首先,看这张图片
我想使用 在我的图形实例中编写垂直文本拉绳 . 我的代码是这样的: var graph = Graphics.FromImage(map); 我从数据库中阅读了文本及其位置: graph.DrawSt
我正在开发 .net 3.5 Win Forms 程序,但遇到了文本绘制的“有趣”问题。 我正在实现一个文本编辑控件,它使用 DrawString() 和 StringFormat.GenericTy
我是一名优秀的程序员,十分优秀!