graphicutils.java
来自「开源项目openfire的完整源程序」· Java 代码 · 共 671 行 · 第 1/2 页
JAVA
671 行
return c;
}
}
}
return null;
}
/**
* Puts the focus on the first component in the tree of <code>c</code> that
* can accept the focus.
*
* @see #getFocusableComponentOrChild
*/
public static Component focusComponentOrChild(Component c) {
return focusComponentOrChild(c, false);
}
/**
* Puts the focus on the first component in the tree of <code>c</code> that
* can accept the focus.
*
* @param c the root of the component hierarchy to search
* @param deepest if <code>deepest</code> is true the method will focus the first and deepest component that can
* accept the focus.
* For example, if both a child and its parent are focusable and <code>deepest</code> is true, the child is focused.
* @see #getFocusableComponentOrChild
*/
public static Component focusComponentOrChild(Component c, boolean deepest) {
final Component focusable = getFocusableComponentOrChild(c, deepest);
if (focusable != null) {
focusable.requestFocus();
}
return focusable;
}
/**
* Loads an {@link Image} named <code>imageName</code> as a resource
* relative to the Class <code>cls</code>. If the <code>Image</code> can
* not be loaded, then <code>null</code> is returned. Images loaded here
* will be added to an internal cache based upon the full {@link URL} to
* their location.
* <p/>
* <em>This method replaces legacy code from JDeveloper 3.x and earlier.</em>
*
* @see Class#getResource(String)
* @see Toolkit#createImage(URL)
*/
public static Image loadFromResource(String imageName, Class cls) {
try {
final URL url = cls.getResource(imageName);
if (url == null) {
return null;
}
Image image = (Image)imageCache.get(url.toString());
if (image == null) {
image = Toolkit.getDefaultToolkit().createImage(url);
imageCache.put(url.toString(), image);
}
return image;
}
catch (Exception e) {
Log.error(e);
}
return null;
}
public static Rectangle[] getScreenBounds() {
GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
final GraphicsDevice[] screenDevices = graphicsEnvironment.getScreenDevices();
Rectangle[] screenBounds = new Rectangle[screenDevices.length];
for (int i = 0; i < screenDevices.length; i++) {
GraphicsDevice screenDevice = screenDevices[i];
final GraphicsConfiguration defaultConfiguration = screenDevice.getDefaultConfiguration();
screenBounds[i] = defaultConfiguration.getBounds();
}
return screenBounds;
}
public static final void makeSameSize(JComponent... comps) {
if (comps.length == 0) {
return;
}
int max = 0;
for (int i = 0; i < comps.length; i++) {
int w = comps[i].getPreferredSize().width;
max = w > max ? w : max;
}
Dimension dim = new Dimension(max, comps[0].getPreferredSize().height);
for (int i = 0; i < comps.length; i++) {
comps[i].setPreferredSize(dim);
}
}
/**
* Return the hexidecimal color from a java.awt.Color
*
* @param c
* @return hexadecimal string
*/
public static final String toHTMLColor(Color c) {
int color = c.getRGB();
color |= 0xff000000;
String s = Integer.toHexString(color);
return s.substring(2);
}
public static final String createToolTip(String text, int width) {
final String htmlColor = toHTMLColor(TOOLTIP_COLOR);
final String toolTip = "<html><table width=" + width + " bgColor=" + htmlColor + "><tr><td>" + text + "</td></tr></table></table>";
return toolTip;
}
public static final String createToolTip(String text) {
final String htmlColor = toHTMLColor(TOOLTIP_COLOR);
final String toolTip = "<html><table bgColor=" + htmlColor + "><tr><td>" + text + "</td></tr></table></table>";
return toolTip;
}
public static final String getHighlightedWords(String text, String query) {
final StringTokenizer tkn = new StringTokenizer(query, " ");
int tokenCount = tkn.countTokens();
String[] words = new String[tokenCount];
for (int j = 0; j < tokenCount; j++) {
String queryWord = tkn.nextToken();
words[j] = queryWord;
}
String highlightedWords;
try {
highlightedWords = StringUtils.highlightWords(text, words, "<font style=background-color:yellow;font-weight:bold;>", "</font>");
}
catch (Exception e) {
highlightedWords = text;
}
return highlightedWords;
}
public static ImageIcon createShadowPicture(Image buf) {
buf = removeTransparency(buf);
BufferedImage splash = null;
JLabel label = new JLabel();
int width = buf.getWidth(null);
int height = buf.getHeight(null);
int extra = 4;
splash = new BufferedImage(width + extra, height + extra, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = (Graphics2D)splash.getGraphics();
BufferedImage shadow = new BufferedImage(width + extra, height + extra, BufferedImage.TYPE_INT_ARGB);
Graphics g = shadow.getGraphics();
g.setColor(new Color(0.0f, 0.0f, 0.0f, 0.3f));
g.fillRoundRect(0, 0, width, height, 12, 12);
g2.drawImage(shadow, getBlurOp(7), 0, 0);
g2.drawImage(buf, 0, 0, label);
return new ImageIcon(splash);
}
public static BufferedImage removeTransparency(Image image) {
int w = image.getWidth(null);
int h = image.getHeight(null);
BufferedImage bi2 = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bi2.createGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, w, h);
g.drawImage(image, 0, 0, null);
g.dispose();
return bi2;
}
public static Image toImage(BufferedImage bufferedImage) {
return Toolkit.getDefaultToolkit().createImage(bufferedImage.getSource());
}
private static ConvolveOp getBlurOp(int size) {
float[] data = new float[size * size];
float value = 1 / (float)(size * size);
for (int i = 0; i < data.length; i++) {
data[i] = value;
}
return new ConvolveOp(new Kernel(size, size, data));
}
public static BufferedImage convert(Image im) throws InterruptedException, IOException {
load(im);
BufferedImage bi = new BufferedImage(im.getWidth(null), im.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics bg = bi.getGraphics();
bg.drawImage(im, 0, 0, null);
bg.dispose();
return bi;
}
public static void load(Image image) throws InterruptedException, IOException {
MediaTracker tracker = new MediaTracker(new Label()); //any component will do
tracker.addImage(image, 0);
tracker.waitForID(0);
if (tracker.isErrorID(0))
throw new IOException("error loading image");
}
public static byte[] getBytesFromImage(Image image) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
ImageIO.write(convert(image), "PNG", baos);
}
catch (IOException e) {
Log.error(e);
}
catch (InterruptedException e) {
Log.error(e);
}
byte[] bytes = baos.toByteArray();
return bytes;
}
/**
* Returns a scaled down image if the height or width is smaller than
* the image size.
*
* @param icon the image icon.
* @param newHeight the preferred height.
* @param newWidth the preferred width.
* @return the icon.
*/
public static ImageIcon scaleImageIcon(ImageIcon icon, int newHeight, int newWidth) {
Image img = icon.getImage();
int height = icon.getIconHeight();
int width = icon.getIconWidth();
if (height > newHeight) {
height = newHeight;
}
if (width > newWidth) {
width = newWidth;
}
img = img.getScaledInstance(width, height, Image.SCALE_SMOOTH);
return new ImageIcon(img);
}
/**
* Returns a scaled down image if the height or width is smaller than
* the image size.
*
* @param icon the image icon.
* @param newHeight the preferred height.
* @param newWidth the preferred width.
* @return the icon.
*/
public static ImageIcon scale(ImageIcon icon, int newHeight, int newWidth) {
Image img = icon.getImage();
int height = icon.getIconHeight();
int width = icon.getIconWidth();
height = newHeight;
width = newWidth;
img = img.getScaledInstance(width, height, Image.SCALE_SMOOTH);
return new ImageIcon(img);
}
/**
* Returns the native icon, if one exists for the filetype, otherwise
* returns a default document icon.
*
* @param file the file to check icon type.
* @return the native icon, otherwise default document icon.
*/
public static Icon getIcon(File file) {
try {
sun.awt.shell.ShellFolder sf = sun.awt.shell.ShellFolder.getShellFolder(file);
// Get large icon
return new ImageIcon(sf.getIcon(true), sf.getFolderType());
}
catch (Exception e) {
try {
return new JFileChooser().getIcon(file);
}
catch (Exception e1) {
}
}
return SparkRes.getImageIcon(SparkRes.DOCUMENT_INFO_32x32);
}
public static BufferedImage getBufferedImage(File file) {
Icon icon = null;
try {
sun.awt.shell.ShellFolder sf = sun.awt.shell.ShellFolder.getShellFolder(file);
// Get large icon
icon = new ImageIcon(sf.getIcon(true), sf.getFolderType());
}
catch (Exception e) {
try {
icon = new JFileChooser().getIcon(file);
}
catch (Exception e1) {
}
}
icon = SparkRes.getImageIcon(SparkRes.DOCUMENT_INFO_32x32);
BufferedImage bi = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.OPAQUE);
Graphics bg = bi.getGraphics();
ImageIcon i = (ImageIcon)icon;
bg.drawImage(i.getImage(), 0, 0, null);
bg.dispose();
return bi;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?