Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/ui/GraphicsUtilities.java
41161 views
1
/*
2
* Copyright (c) 2000, 2021, 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
private static final int FONT_SIZE = 12;
38
39
public static Font getMonospacedFont() {
40
return new Font(Font.MONOSPACED, Font.PLAIN, FONT_SIZE);
41
}
42
43
/** Compute the width and height of given string given the current
44
font context in the Graphics object */
45
public static Rectangle2D getStringBounds(String s, Graphics g) {
46
FontMetrics fm = g.getFontMetrics();
47
return fm.getStringBounds(s, 0, s.length(), g);
48
}
49
50
/** Compute just the width of the given string with the given
51
FontMetrics. This is less accurate then getStringBounds(),
52
above, since the graphics context is not taken into account. */
53
public static int getStringWidth(String s, FontMetrics fm) {
54
return fm.stringWidth(s);
55
}
56
57
public static void reshapeToAspectRatio(Component component,
58
float aspectRatio,
59
float fillRatio,
60
Dimension containerDimension) {
61
int x = containerDimension.width;
62
int y = containerDimension.height;
63
64
int desiredX;
65
int desiredY;
66
67
if (((float) x / (float) y) > aspectRatio) {
68
desiredY = (int) (fillRatio * y);
69
desiredX = (int) (desiredY * aspectRatio);
70
} else {
71
desiredX = (int) (fillRatio * x);
72
desiredY = (int) (desiredX / aspectRatio);
73
}
74
component.setSize(desiredX, desiredY);
75
}
76
77
public static void constrainToSize(Component component, Dimension containerDimension) {
78
Dimension d = component.getSize();
79
int x = d.width;
80
int y = d.height;
81
boolean changed = false;
82
83
if (x > containerDimension.width) {
84
x = containerDimension.width;
85
changed = true;
86
}
87
if (y > containerDimension.height) {
88
y = containerDimension.height;
89
changed = true;
90
}
91
92
if (changed) {
93
component.setSize(x, y);
94
}
95
}
96
97
public static void centerInContainer(Component c) {
98
centerInContainer(c, c.getParent().getSize());
99
}
100
101
public static void centerInContainer(Component component,
102
Dimension containerDimension) {
103
Dimension sz = component.getSize();
104
int x = ((containerDimension.width - sz.width) / 2);
105
int y = ((containerDimension.height - sz.height) / 2);
106
component.setLocation(x, y);
107
}
108
109
public static void moveToInContainer(Component component,
110
float relativeX,
111
float relativeY,
112
int minX,
113
int minY) {
114
Dimension d = component.getParent().getSize();
115
// Move the center of this component to the relative position in
116
// the parent. Don't clip this component, however.
117
Dimension sz = component.getSize();
118
int xPos = Math.min(d.width - sz.width,
119
(int) ((d.width * relativeX) - (sz.width / 2)));
120
int yPos = Math.min(d.height - sz.height,
121
(int) ((d.height * relativeY) - (sz.height / 2)));
122
xPos = Math.max(xPos, minX);
123
yPos = Math.max(yPos, minY);
124
component.setLocation(xPos, yPos);
125
}
126
127
static Random random = new Random();
128
129
public static void randomLocation(Component c) {
130
randomLocation(c, c.getParent().getSize());
131
}
132
133
public static void randomLocation(Component component,
134
Dimension containerDimension) {
135
Dimension sz = component.getSize();
136
int x = (int)((containerDimension.width - sz.width) * random.nextFloat());
137
int y = (int)((containerDimension.height - sz.height) * random.nextFloat());
138
component.setLocation(x, y);
139
}
140
141
142
public static Border newBorder(int size) {
143
return BorderFactory.createEmptyBorder(size, size, size, size);
144
}
145
}
146
147