1 /*
2  * Copyright (c) 2000, 2004, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  *
23  */
24 
25 package sun.jvm.hotspot.ui;
26 
27 import java.awt.*;
28 import java.awt.font.*;
29 import java.awt.geom.*;
30 import java.util.Random;
31 import javax.swing.*;
32 import javax.swing.border.*;
33 
34 /** Useful utilities for drawing graphics */
35 
36 public class GraphicsUtilities {
37   /** Returns a plain-styled 12-point version of the given font, or
38       null if the font could not be found */
lookupFont(String fontName)39   public static Font lookupFont(String fontName) {
40     Font[] allFonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
41     Font font = null;
42     for (int i = 0; i < allFonts.length; i++) {
43       if (allFonts[i].getFontName().indexOf(fontName) != -1) {
44         font = allFonts[i];
45         break;
46       }
47     }
48     if (font == null) {
49       return null;
50     }
51     return font.deriveFont(Font.PLAIN, 12);
52   }
53 
54   /** Compute the width and height of given string given the current
55       font context in the Graphics object */
getStringBounds(String s, Graphics g)56   public static Rectangle2D getStringBounds(String s, Graphics g) {
57     FontMetrics fm = g.getFontMetrics();
58     return fm.getStringBounds(s, 0, s.length(), g);
59   }
60 
61   /** Compute just the width of the given string with the given
62       FontMetrics. This is less accurate then getStringBounds(),
63       above, since the graphics context is not taken into account. */
getStringWidth(String s, FontMetrics fm)64   public static int getStringWidth(String s, FontMetrics fm) {
65     return fm.stringWidth(s);
66   }
67 
reshapeToAspectRatio(Component component, float aspectRatio, float fillRatio, Dimension containerDimension)68   public static void reshapeToAspectRatio(Component component,
69                                           float aspectRatio,
70                                           float fillRatio,
71                                           Dimension containerDimension) {
72     int x = containerDimension.width;
73     int y = containerDimension.height;
74 
75     int desiredX;
76     int desiredY;
77 
78     if (((float) x / (float) y) > aspectRatio) {
79       desiredY = (int) (fillRatio * y);
80       desiredX = (int) (desiredY * aspectRatio);
81     } else {
82       desiredX = (int) (fillRatio * x);
83       desiredY = (int) (desiredX / aspectRatio);
84     }
85     component.setSize(desiredX, desiredY);
86   }
87 
constrainToSize(Component component, Dimension containerDimension)88   public static void constrainToSize(Component component, Dimension containerDimension) {
89     Dimension d = component.getSize();
90     int x = d.width;
91     int y = d.height;
92     boolean changed = false;
93 
94     if (x > containerDimension.width) {
95       x = containerDimension.width;
96       changed = true;
97     }
98     if (y > containerDimension.height) {
99       y = containerDimension.height;
100       changed = true;
101     }
102 
103     if (changed) {
104       component.setSize(x, y);
105     }
106   }
107 
centerInContainer(Component c)108   public static void centerInContainer(Component c) {
109     centerInContainer(c, c.getParent().getSize());
110   }
111 
centerInContainer(Component component, Dimension containerDimension)112   public static void centerInContainer(Component component,
113                                        Dimension containerDimension) {
114     Dimension sz = component.getSize();
115     int x = ((containerDimension.width - sz.width) / 2);
116     int y = ((containerDimension.height - sz.height) / 2);
117     component.setLocation(x, y);
118   }
119 
moveToInContainer(Component component, float relativeX, float relativeY, int minX, int minY)120   public static void moveToInContainer(Component component,
121                                        float relativeX,
122                                        float relativeY,
123                                        int minX,
124                                        int minY) {
125     Dimension d = component.getParent().getSize();
126     // Move the center of this component to the relative position in
127     // the parent. Don't clip this component, however.
128     Dimension sz = component.getSize();
129     int xPos = Math.min(d.width - sz.width,
130                         (int) ((d.width * relativeX) - (sz.width / 2)));
131     int yPos = Math.min(d.height - sz.height,
132                         (int) ((d.height * relativeY) - (sz.height / 2)));
133     xPos = Math.max(xPos, minX);
134     yPos = Math.max(yPos, minY);
135     component.setLocation(xPos, yPos);
136   }
137 
138   static Random random = new Random();
139 
randomLocation(Component c)140   public static void randomLocation(Component c) {
141     randomLocation(c, c.getParent().getSize());
142   }
143 
randomLocation(Component component, Dimension containerDimension)144   public static void randomLocation(Component component,
145                                     Dimension containerDimension) {
146     Dimension sz = component.getSize();
147     int x = (int)((containerDimension.width - sz.width) * random.nextFloat());
148     int y = (int)((containerDimension.height - sz.height) * random.nextFloat());
149     component.setLocation(x, y);
150   }
151 
152 
newBorder(int size)153   public static Border newBorder(int size) {
154     return BorderFactory.createEmptyBorder(size, size, size, size);
155   }
156 }
157