Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Swing / AWT / SWT
Change ascent value on Font LineMetrics
Joao Cunha
Greenhorn
Posts: 24
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi all!
How can I set the ascent value of a font?
I'd Like to have the text height equal to the font size. So, if the font size is 20, I'd like to have the text height equal 20, without any spacing at the top and bottom of the text.
The code to get the ascent of font is:
Font theFont = new Font("Serif", Font.PLAIN, 100); String message; BoxedLabel(String message) { this.message = message; } public void paintComponent(Graphics g) { super.paintComponent(g); Insets insets = getInsets(); g.translate(insets.left, insets.top); int baseline = 150; g.setFont(theFont); FontMetrics fm = g.getFontMetrics(); int ascent = fm.getAscent();
Thanks a lot for the help!
Craig Wood
Ranch Hand
Posts: 1535
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
import java.awt.*; import java.awt.font.*; import java.awt.geom.AffineTransform; import javax.swing.*; public class FontMethods { public static void main(String[] args) { JOptionPane.showMessageDialog(null, new BL("Hello World"), "", JOptionPane.PLAIN_MESSAGE); } } class BL extends JComponent { Font theFont = new Font("Serif", Font.PLAIN, 100); String message; BL(String message) { this.message = message; setPreferredSize(new Dimension(600,100)); setBorder(BorderFactory.createEtchedBorder()); } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); Insets insets = getInsets(); // g.translate(insets.left, insets.top); int w = getWidth(); int h = getHeight(); g2.setFont(theFont); FontRenderContext frc = g2.getFontRenderContext(); float width = (float)theFont.getStringBounds(message, frc).getWidth(); LineMetrics lm = theFont.getLineMetrics(message, frc); float ascent = lm.getAscent(); float descent = lm.getDescent(); float height = ascent + descent; float x = (w - width)/2; float y = (h + height)/2 - descent; g2.drawString(message, x, y); } }
Don't get me started about those stupid
light bulbs
.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
one last try
Size of String in Pixels from JTextField
Bug in Sun's Java SE 1.3 Graphics.setFont?
drawing only upper half of word
How to change Font in j2me
More...