/*
* Created on 23.05.2005
*
* Copyright (c) 2006, Manuel Alabor
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer. Redistributions in binary
* form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided
* with the distribution. Neither the name of Manuel Alabor nor the names
* of its contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package net.msites.guilibrary.toolbox;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
/**
* Sammlung aus verschiedenen GUI-spezifischen Hilfsmethoden.
*
* @author Manuel Alabor
* @version 1.3
*/
public class GUIHelper {
/**
* Zentriert die Component toCenter auf owner.
* Praktisch, um z.B. einen JDialog auf einem JFrame
* zu zentrieren.
*
* @param toCenter
* @param owner
*/
public static void centerOnOwner(Component toCenter, Component owner) {
Dimension ownerSize = owner.getSize();
Point ownerLocation = owner.getLocation();
Dimension toCenterSize = toCenter.getSize();
int posX = new Double(((ownerSize.getWidth() - toCenterSize.getWidth()) / 2) + ownerLocation.getX()).intValue();
int posY = new Double(((ownerSize.getHeight() - toCenterSize.getHeight()) / 2) + ownerLocation.getY()).intValue();
toCenter.setLocation(posX, posY);
}
/**
* Zentriert die Component toCenter auf dem Bildschirm.
* Praktisch, um z.B. ein Fenster auf dem Bildschirm zu zentrieren.
*
* @param toCenter
*/
public static void centerOnScreen(Component toCenter) {
Dimension paneSize = toCenter.getSize();
Dimension screenSize = toCenter.getToolkit().getScreenSize();
int posX = (screenSize.width - paneSize.width) / 2;
int posY = (screenSize.height - paneSize.height) / 2;
toCenter.setLocation(posX, posY);
}
/**
* Erstellt aus einem BufferedImage ein Systemkompatibles
* BufferedImage. Performance solcher Bilder ist höher beim
* Zeichnen.
*
* @param image
* @return
*/
public static BufferedImage toCompatibleImage(BufferedImage image) {
GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice d = e.getDefaultScreenDevice();
GraphicsConfiguration c = d.getDefaultConfiguration();
BufferedImage compatibleImage = c.createCompatibleImage(
image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB_PRE);
Graphics g = compatibleImage.getGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
return compatibleImage;
}
/**
* Ersetzt die GlassPane von frame mit newGlassPane.
* Wird als newGlassPane null übergeben, wird lediglich die
* aktuelle GlassPane entfernt.
*
* @param frame
* @param newGlassPane
*/
public static void replaceGlassPane(JFrame frame, Component newGlassPane) {
Component oldGlassPane = frame.getGlassPane();
if(oldGlassPane != null) oldGlassPane.setVisible(false);
if(newGlassPane != null) {
frame.setGlassPane(newGlassPane);
newGlassPane.setVisible(true);
}
}
}