Java Upside Down Text - Bug or Feature?

扶醉桌前 提交于 2019-12-10 17:13:08

问题


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?

Try it out:

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();
    }
}


回答1:


Seems like this is happening:

  1. 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.
  2. 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.


来源:https://stackoverflow.com/questions/18682364/java-upside-down-text-bug-or-feature

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!