Java 颠倒文本 - 错误还是功能? [英] Java Upside Down Text - Bug or Feature?
本文介绍了Java 颠倒文本 - 错误还是功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在使用 Java 字体类和 Swing 时,我将字体大小设置为负值.
While playing around with the Java font class and Swing, I set the font size to a negative value.
我发现这会使文本被倒置.这是错误还是功能?谁能解释为什么会发生这种行为?
I discovered that this makes the text be drawn upside down. Is this a bug or a feature? Can anyone explain why this behavior happens?
试试看:
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class UpsideDown extends JFrame{
public UpsideDown(){
setSize(500,500);
setContentPane(new Panel());
setVisible(true);
}
public class Panel extends JPanel{
public void paintComponent(Graphics g){
Font f = new Font("Sans-Serif", Font.PLAIN, -50);
g.setFont(f);
g.drawString("Upside Down", 400, 100);
}
}
public static void main(String args[]){
new UpsideDown();
}
}
推荐答案
好像是这样:
- Swing 将字体的高度向下绘制,因为它将字体大小乘以字体的字形高度.
-50 * glyph_height
是负数 -> 向下绘制而不是向上绘制. - 它还向左绘制字形(字母的)宽度,同样是因为它将您的字体大小乘以字体指定的字形宽度.
- Swing draws your font's height downwards, because it multiplies the font size with the glyph height of the font.
-50 * glyph_height
is negative -> drawing downwards instead of upwards. - It also draws the glyph's (the letter's) width to the left, again because it multiplies your font size with the glyph width specified by the font.
这篇关于Java 颠倒文本 - 错误还是功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文