在 Java Graphics2D 中,文本如何在矩形上居中对齐? [英] IN Java Graphics2D, how can text be center aligned on a Rectangle?
问题描述
在这段代码中,我有一个窗口,其中一个角落有一个蓝色框.
我需要在此框上对齐文本中心.
public class drawComponent extends JComponent {public voidpaintComponent(Graphics g){//在窗口更新时调用INT线索高度差异= 0;整数间隙 = 5;国际边界= 10;Font font = new Font("Ariel", Font.PLAIN, 30);颜色蓝色 = 新颜色(0,0,255);颜色白色 = 新颜色(255,255,255);int winH = Jeopardy.window.getBounds().getSize().height;int winW = Jeopardy.window.getBounds().getSize().width;int width = (winW - ((2 * border) + (5 * gap)))/6 ;int height = ((winH - ((4*border) + (5 * gap) + pixelHeightDiff )))/6;int 线索高度 = 高度 + 线索高度差异;int Fx = 边框 +(5* 宽度)+(5*间隙);int foY = 边界 + (2*gap) + (2*height) + 线索高度差异;矩形 F2 = 新矩形(Fx,foY, width , height);Graphics2D g2f2 = (Graphics2D) g;g2f2.draw(F2);g2f2.fill(F2);g2f2.setColor(蓝色);FontMetrics 指标 = g2f2.getFontMetrics(font);int height = metrics.getHeight();int width = metrics.stringWidth(text);尺寸尺寸=新尺寸(宽+2,高+2);
鉴于此框的 X 和 Y 边界,我需要将文本舒适地放入其中.根据 ,稍微复杂一些,但为您提供了一个边界框,表示呈现文本所需的区域.这对于属性文本更有用,但仍然有用
Im In this code, I have a window with a blue box in one of the corners.
I need to get text center aligned on this box.
public class drawComponent extends JComponent {
public void paintComponent(Graphics g){ //called on window update
int clueHeightDiff= 0;
int gap = 5;
int border = 10;
Font font = new Font("Ariel", Font.PLAIN, 30);
Color blue = new Color(0,0,255);
Color white = new Color(255,255,255);
int winH = Jeopardy.window.getBounds().getSize().height;
int winW = Jeopardy.window.getBounds().getSize().width;
int width = (winW - ((2 * border) + (5 * gap))) / 6 ;
int height = ((winH - ((4*border) + (5 * gap) + clueHeightDiff ))) / 6;
int clueHeight = height + clueHeightDiff;
int Fx = border + (5* width) + (5*gap);
int foY = border + (2*gap) + (2*height) + clueHeightDiff;
Rectangle F2 = new Rectangle(Fx,foY, width , height);
Graphics2D g2f2 = (Graphics2D) g;
g2f2.draw(F2);
g2f2.fill(F2);
g2f2.setColor(blue);
FontMetrics metrics = g2f2.getFontMetrics(font);
int height = metrics.getHeight();
int width = metrics.stringWidth(text);
Dimension size = new Dimension(width+2, height+2);
Given the X and Y bounds of this box, I need the text to fit comfortably inside. I just can't find a way to do this effectively, as according to the documentation, stringwidth() returns the value of only the first character.
FontMetrics#stringwidth
will, generally, return the amount of pixels required to render the text
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane(new Rectangle(150, 150, 50, 50)));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private Rectangle boxIn;
public TestPane(Rectangle boxIn) {
this.boxIn = boxIn;
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
String text = "Hello";
FontMetrics fm = g2d.getFontMetrics();
int x = boxIn.x + ((boxIn.width - fm.stringWidth(text)) / 2);
int y = boxIn.y + (((boxIn.height - fm.getHeight()) / 2) + fm.getAscent());
g2d.setColor(Color.BLUE);
g2d.fill(boxIn);
g2d.setColor(Color.WHITE);
g2d.drawString(text, x, y);
g2d.dispose();
}
}
}
You can also use TextLayout
, which is a little more complictated, but provides you with a bounding box, representing the area that would required to render the text. This is more useful for attributed text, but can still be useful
这篇关于在 Java Graphics2D 中,文本如何在矩形上居中对齐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!