Java Upside Down Text - error or feature?

When playing with the Java and Swing font class, I set the font size to a negative value.

I found that this causes the text to be turned upside down. Is this a bug or a function? Can anyone explain why this is happening?

Try:

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

Upside down

+4
source share
1 answer

This seems to be happening:

  • Swing draws your font height down because it multiplies the font size with the font glyph height. -50 * glyph_height negative -50 * glyph_height drawing down, not up.
  • It also draws the width of the character (letter) on the left, again because it multiplies your font size with the glyph width indicated by the font.
+6
source

Source: https://habr.com/ru/post/1501151/


All Articles