Swing问题:关于getY()和getHeight()
getY()到底是什么意思呢?和getHeight()有什么区别呢?我看getY()的解释是返回组件原点的当前 y 坐标。但是组件原点又是什么呢?
一个书上的例子:
class FontPanel extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
String message = "Hello , World!";
Font f = new Font("Serif", Font.BOLD, 36);
g2.setFont(f);
FontRenderContext context = g2.getFontRenderContext();
Rectangle2D bounds = f.getStringBounds(message, context);
double x = (getWidth() - bounds.getWidth())/2;
double y = (getHeight() - bounds.getHeight())/2;
double ascent = -bounds.getY();
double baseY = y + ascent;
g2.drawString(message, (int)x, (int) baseY);
g2.setPaint(Color.GRAY);
g2.draw(new Line2D.Double(x, baseY, x+bounds.getWidth(), baseY));
Rectangle2D rect = new Rectangle2D.Double(x, y, bounds.getWidth(), bounds.getHeight());
g2.draw(rect);
}
}
请问那根线(基线)的位置是如何算出来的?
谢谢