Path: blob/master/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/ui/GraphicsUtilities.java
41161 views
/*1* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324package sun.jvm.hotspot.ui;2526import java.awt.*;27import java.awt.font.*;28import java.awt.geom.*;29import java.util.Random;30import javax.swing.*;31import javax.swing.border.*;3233/** Useful utilities for drawing graphics */3435public class GraphicsUtilities {36private static final int FONT_SIZE = 12;3738public static Font getMonospacedFont() {39return new Font(Font.MONOSPACED, Font.PLAIN, FONT_SIZE);40}4142/** Compute the width and height of given string given the current43font context in the Graphics object */44public static Rectangle2D getStringBounds(String s, Graphics g) {45FontMetrics fm = g.getFontMetrics();46return fm.getStringBounds(s, 0, s.length(), g);47}4849/** Compute just the width of the given string with the given50FontMetrics. This is less accurate then getStringBounds(),51above, since the graphics context is not taken into account. */52public static int getStringWidth(String s, FontMetrics fm) {53return fm.stringWidth(s);54}5556public static void reshapeToAspectRatio(Component component,57float aspectRatio,58float fillRatio,59Dimension containerDimension) {60int x = containerDimension.width;61int y = containerDimension.height;6263int desiredX;64int desiredY;6566if (((float) x / (float) y) > aspectRatio) {67desiredY = (int) (fillRatio * y);68desiredX = (int) (desiredY * aspectRatio);69} else {70desiredX = (int) (fillRatio * x);71desiredY = (int) (desiredX / aspectRatio);72}73component.setSize(desiredX, desiredY);74}7576public static void constrainToSize(Component component, Dimension containerDimension) {77Dimension d = component.getSize();78int x = d.width;79int y = d.height;80boolean changed = false;8182if (x > containerDimension.width) {83x = containerDimension.width;84changed = true;85}86if (y > containerDimension.height) {87y = containerDimension.height;88changed = true;89}9091if (changed) {92component.setSize(x, y);93}94}9596public static void centerInContainer(Component c) {97centerInContainer(c, c.getParent().getSize());98}99100public static void centerInContainer(Component component,101Dimension containerDimension) {102Dimension sz = component.getSize();103int x = ((containerDimension.width - sz.width) / 2);104int y = ((containerDimension.height - sz.height) / 2);105component.setLocation(x, y);106}107108public static void moveToInContainer(Component component,109float relativeX,110float relativeY,111int minX,112int minY) {113Dimension d = component.getParent().getSize();114// Move the center of this component to the relative position in115// the parent. Don't clip this component, however.116Dimension sz = component.getSize();117int xPos = Math.min(d.width - sz.width,118(int) ((d.width * relativeX) - (sz.width / 2)));119int yPos = Math.min(d.height - sz.height,120(int) ((d.height * relativeY) - (sz.height / 2)));121xPos = Math.max(xPos, minX);122yPos = Math.max(yPos, minY);123component.setLocation(xPos, yPos);124}125126static Random random = new Random();127128public static void randomLocation(Component c) {129randomLocation(c, c.getParent().getSize());130}131132public static void randomLocation(Component component,133Dimension containerDimension) {134Dimension sz = component.getSize();135int x = (int)((containerDimension.width - sz.width) * random.nextFloat());136int y = (int)((containerDimension.height - sz.height) * random.nextFloat());137component.setLocation(x, y);138}139140141public static Border newBorder(int size) {142return BorderFactory.createEmptyBorder(size, size, size, size);143}144}145146147