JavaのGraphicsまたはGraphics2Dクラスを使用して、Stringをペイントするにはどうすればよいですか?

StackOverflow https://stackoverflow.com/questions/1206193

質問

String があり、それを画像にペイントしたい。ただし、 2Dグラフィックスチュートリアルのテキスト部分 String を取得して描画にペイントする方法がわかりません。

間違ったチュートリアルを見ていない限り(ただし、Javaについて何かを検索し、 Graphics または Graphics2D を使用して文字列をペイントするたびに表示されるチュートリアルです)。まだ困惑しています。

役に立ちましたか?

解決

次の方法を確認してください。

g.drawString();

drawString() メソッドは必要なことを行います。

使用例:

protected void paintComponent(Graphics g){
    g.setColor(Color.BLACK);
    g.drawString(5, 40, "Hello World!");
}

座標は、描画中の String の左下に関するものであることを忘れないでください。

他のヒント

文字列の形状を使用する場合(例:fill:redおよびstroke:blue):

Graphics2D yourGraphicsContext=(...);
Font f= new Font("Dialog",Font.PLAIN,14);
FontRenderContext frc = yourGraphicsContext.getFontRenderContext();
TextLayout tl = new TextLayout(e.getTextContent(), f, frc);
Shape shape= tl.getOutline(null);

//here, you can move your shape with AffineTransform (...)

yourGraphicsContext.setColor(Color.RED);
yourGraphicsContext.fill(shape);
yourGraphicsContext.setColor(Color.BLUE);
yourGraphicsContext.draw(shape);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top