0N/A/*
3909N/A * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Aimport java.awt.*;
0N/Aimport java.util.Properties;
0N/Aimport sun.awt.*;
0N/A
0N/Apublic class StringWidth extends Frame {
0N/A
0N/A public StringWidth() {
0N/A Font plain = new Font("Dialog", Font.PLAIN, 10);
0N/A Font bold = new Font("Dialog", Font.BOLD, 10);
0N/A Properties props = new Properties();
0N/A int x, y;
0N/A
0N/A // we must have visible Frame context for PrintDialog in Solaris
0N/A setSize(400, 300);
0N/A setVisible(true);
0N/A
0N/A PrintJob pj = getToolkit().getPrintJob(this, "", props);
0N/A if (pj == null) {
0N/A return;
0N/A }
0N/A Graphics pg = pj.getGraphics();
0N/A
0N/A
0N/A String test = "Hello World!";
0N/A
0N/A FontMetrics plainFm = pg.getFontMetrics(plain);
0N/A FontMetrics boldFm = pg.getFontMetrics(bold);
0N/A Dimension size = pj.getPageDimension();
0N/A
0N/A // now right justify on the printed page
0N/A int center = size.width/2;
0N/A y = 150;
0N/A x = center - plainFm.stringWidth(test);
0N/A pg.setFont(plain);
0N/A pg.drawString(test, x-1, y);
0N/A
0N/A pg.dispose();
0N/A pj.end();
0N/A setVisible(false);
0N/A }
0N/A
0N/A public static void main(String[] args) {
3544N/A StringWidth sw = new StringWidth();
3544N/A sw.dispose();
0N/A }
0N/A
0N/A}