📄 graphicutils.java
字号:
/**
* $RCSfile$
* $Revision: 128 $
* $Date: 2004-10-25 20:42:00 -0300 (Mon, 25 Oct 2004) $
*
* Copyright (C) 2008 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution, or a commercial license
* agreement with Jive.
*/
package org.jivesoftware.openfire.launcher;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.net.URL;
import java.util.Hashtable;
import javax.swing.*;
/**
* <code>GraphicsUtils</code> class defines common user-interface related utility
* functions.
*/
public final class GraphicUtils {
private static final Insets HIGHLIGHT_INSETS = new Insets(1, 1, 1, 1);
public static final Color SELECTION_COLOR = new java.awt.Color(166, 202, 240);
public static final Color TOOLTIP_COLOR = new java.awt.Color(166, 202, 240);
protected final static Component component = new Component() {
};
protected final static MediaTracker tracker = new MediaTracker(component);
private static Hashtable imageCache = new Hashtable();
private GraphicUtils() {
}
/**
* Sets the location of the specified window so that it is centered on screen.
*
* @param window The window to be centered.
*/
public static void centerWindowOnScreen(Window window) {
final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
final Dimension size = window.getSize();
if (size.height > screenSize.height) {
size.height = screenSize.height;
}
if (size.width > screenSize.width) {
size.width = screenSize.width;
}
window.setLocation((screenSize.width - size.width) / 2,
(screenSize.height - size.height) / 2);
}
/**
* Draws a single-line highlight border rectangle.
*
* @param g The graphics context to use for drawing.
* @param x The left edge of the border.
* @param y The top edge of the border.
* @param width The width of the border.
* @param height The height of the border.
* @param raised <code>true</code> if the border is to be drawn raised,
* <code>false</code> if lowered.
* @param shadow The shadow color for the border.
* @param highlight The highlight color for the border.
* @see javax.swing.border.EtchedBorder
* @see javax.swing.plaf.basic.BasicGraphicsUtils#drawEtchedRect
*/
public static void drawHighlightBorder(Graphics g, int x, int y,
int width, int height, boolean raised,
Color shadow, Color highlight) {
final Color oldColor = g.getColor();
g.translate(x, y);
g.setColor(raised ? highlight : shadow);
g.drawLine(0, 0, width - 2, 0);
g.drawLine(0, 1, 0, height - 2);
g.setColor(raised ? shadow : highlight);
g.drawLine(width - 1, 0, width - 1, height - 1);
g.drawLine(0, height - 1, width - 2, height - 1);
g.translate(-x, -y);
g.setColor(oldColor);
}
/**
* Return the amount of space taken up by a highlight border drawn by
* <code>drawHighlightBorder()</code>.
*
* @return The <code>Insets</code> needed for the highlight border.
* @see #drawHighlightBorder
*/
public static Insets getHighlightBorderInsets() {
return HIGHLIGHT_INSETS;
}
public static ImageIcon createImageIcon(Image image) {
if (image == null) {
return null;
}
synchronized (tracker) {
tracker.addImage(image, 0);
try {
tracker.waitForID(0, 0);
} catch (InterruptedException e) {
System.out.println("INTERRUPTED while loading Image");
}
tracker.removeImage(image, 0);
}
return new ImageIcon(image);
}
/**
* Returns a point where the given popup menu should be shown. The
* point is calculated by adjusting the X and Y coordinates from the
* given mouse event so that the popup menu will not be clipped by
* the screen boundaries.
*
* @param popup the popup menu
* @param event the mouse event
* @return the point where the popup menu should be shown
*/
public static Point getPopupMenuShowPoint(JPopupMenu popup, MouseEvent event) {
Component source = (Component) event.getSource();
Point topLeftSource = source.getLocationOnScreen();
Point ptRet = getPopupMenuShowPoint(popup,
topLeftSource.x + event.getX(),
topLeftSource.y + event.getY());
ptRet.translate(-topLeftSource.x, -topLeftSource.y);
return ptRet;
}
/**
* Returns a point where the given popup menu should be shown. The
* point is calculated by adjusting the X and Y coordinates so that
* the popup menu will not be clipped by the screen boundaries.
*
* @param popup the popup menu
* @param x the x position in screen coordinate
* @param y the y position in screen coordinates
* @return the point where the popup menu should be shown in screen
* coordinates
*/
public static Point getPopupMenuShowPoint(JPopupMenu popup, int x, int y) {
Dimension sizeMenu = popup.getPreferredSize();
Point bottomRightMenu = new Point(x + sizeMenu.width, y + sizeMenu.height);
Rectangle[] screensBounds = getScreenBounds();
int n = screensBounds.length;
for (int i = 0; i < n; i++) {
Rectangle screenBounds = screensBounds[i];
if (screenBounds.x <= x && x <= (screenBounds.x + screenBounds.width)) {
Dimension sizeScreen = screenBounds.getSize();
sizeScreen.height -= 32; // Hack to help prevent menu being clipped by Windows/Linux/Solaris Taskbar.
int xOffset = 0;
if (bottomRightMenu.x > (screenBounds.x + sizeScreen.width))
xOffset = -sizeMenu.width;
int yOffset = 0;
if (bottomRightMenu.y > (screenBounds.y + sizeScreen.height))
yOffset = sizeScreen.height - bottomRightMenu.y;
return new Point(x + xOffset, y + yOffset);
}
}
return new Point(x, y); // ? that would mean that the top left point was not on any screen.
}
/**
* Centers the window over a component (usually another window).
* The window must already have been sized.
*/
public static void centerWindowOnComponent(Window window, Component over) {
if ((over == null) || !over.isShowing()) {
centerWindowOnScreen(window);
return;
}
Point parentLocation = over.getLocationOnScreen();
Dimension parentSize = over.getSize();
Dimension size = window.getSize();
// Center it.
int x = parentLocation.x + (parentSize.width - size.width) / 2;
int y = parentLocation.y + (parentSize.height - size.height) / 2;
// Now, make sure it's onscreen
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
// This doesn't actually work on the Mac, where the screen
// doesn't necessarily start at 0,0
if (x + size.width > screenSize.width)
x = screenSize.width - size.width;
if (x < 0)
x = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -