From 5032121aef4128eb5c6671847e32017c629e1802 Mon Sep 17 00:00:00 2001 From: shaper Date: Thu, 23 Aug 2001 00:17:42 +0000 Subject: [PATCH] Added drawStringCentered(). git-svn-id: https://samskivert.googlecode.com/svn/trunk@312 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../com/samskivert/swing/util/SwingUtil.java | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/projects/samskivert/src/java/com/samskivert/swing/util/SwingUtil.java b/projects/samskivert/src/java/com/samskivert/swing/util/SwingUtil.java index b5582174..e16f50fb 100644 --- a/projects/samskivert/src/java/com/samskivert/swing/util/SwingUtil.java +++ b/projects/samskivert/src/java/com/samskivert/swing/util/SwingUtil.java @@ -1,5 +1,5 @@ // -// $Id: SwingUtil.java,v 1.3 2001/08/11 22:43:29 mdb Exp $ +// $Id: SwingUtil.java,v 1.4 2001/08/23 00:17:42 shaper Exp $ // // samskivert library - useful routines for java programs // Copyright (C) 2001 Michael Bayne @@ -40,4 +40,24 @@ public class SwingUtil window.setBounds((ss.width-width)/2, (ss.height-height)/2, width, height); } + + /** + * Draw a string centered within a rectangle. The string is drawn + * using the graphics context's current font and color. + * + * @param g the graphics context. + * @param str the string. + * @param x the bounding x position. + * @param y the bounding y position. + * @param width the bounding width. + * @param height the bounding height. + */ + public static void drawStringCentered ( + Graphics g, String str, int x, int y, int width, int height) + { + FontMetrics fm = g.getFontMetrics(g.getFont()); + int xpos = x + ((width - fm.stringWidth(str)) / 2); + int ypos = y + ((height + fm.getAscent()) / 2); + g.drawString(str, xpos, ypos); + } }