Here you can find the source of getGraphicsDevice(Component comp)
Parameter | Description |
---|---|
comp | the component to determine the graphics device for |
public static GraphicsDevice getGraphicsDevice(Component comp)
//package com.java2s; /*/*w ww. ja v a 2 s. c o m*/ * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ import javax.swing.JInternalFrame; import java.awt.Component; import java.awt.Container; import java.awt.Dialog; import java.awt.Frame; import java.awt.GraphicsConfiguration; import java.awt.GraphicsDevice; public class Main { /** * Tries to determine the {@link GraphicsDevice} that the specified component * is located on. * * @param comp the component to determine the graphics device for * @return the device, null if failed to determine */ public static GraphicsDevice getGraphicsDevice(Component comp) { GraphicsConfiguration gc; gc = getGraphicsConfiguration(comp); if (gc == null) return null; else return gc.getDevice(); } /** * Tries to determine the {@link GraphicsConfiguration} that the specified component * is located on. * * @param comp the component to determine the graphics config for * @return the config, null if failed to determine */ public static GraphicsConfiguration getGraphicsConfiguration(Component comp) { if (comp == null) return null; if (getParentDialog(comp) != null) return getParentDialog(comp).getGraphicsConfiguration(); else if (getParentFrame(comp) != null) return getParentFrame(comp).getGraphicsConfiguration(); else if (getParentInternalFrame(comp) != null) return getParentInternalFrame(comp).getGraphicsConfiguration(); else return null; } /** * Tries to determine the dialog this container is part of. * * @param cont the container to get the dialog for * @return the parent dialog if one exists or null if not */ public static Dialog getParentDialog(Container cont) { return (Dialog) getParent(cont, Dialog.class); } /** * Tries to determine the dialog this component is part of. * * @param comp the component to get the dialog for * @return the parent dialog if one exists or null if not */ public static Dialog getParentDialog(Component comp) { if (comp instanceof Container) return (Dialog) getParent((Container) comp, Dialog.class); else return null; } /** * Tries to determine the frame the container is part of. * * @param cont the container to get the frame for * @return the parent frame if one exists or null if not */ public static Frame getParentFrame(Container cont) { return (Frame) getParent(cont, Frame.class); } /** * Tries to determine the frame the component is part of. * * @param comp the component to get the frame for * @return the parent frame if one exists or null if not */ public static Frame getParentFrame(Component comp) { if (comp instanceof Container) return (Frame) getParent((Container) comp, Frame.class); else return null; } /** * Tries to determine the internal frame this container is part of. * * @param cont the container to start with * @return the parent internal frame if one exists or null if not */ public static JInternalFrame getParentInternalFrame(Container cont) { return (JInternalFrame) getParent(cont, JInternalFrame.class); } /** * Tries to determine the internal frame this component is part of. * * @param comp the component to start with * @return the parent internal frame if one exists or null if not */ public static JInternalFrame getParentInternalFrame(Component comp) { if (comp instanceof Container) return (JInternalFrame) getParent((Container) comp, JInternalFrame.class); else return null; } /** * Tries to determine the parent this panel is part of. * * @param cont the container to get the parent for * @param parentClass the class of the parent to obtain * @return the parent if one exists or null if not */ public static Object getParent(Container cont, Class parentClass) { Container result; Container parent; result = null; parent = cont; while (parent != null) { if (parentClass.isInstance(parent)) { result = parent; break; } else { parent = parent.getParent(); } } return result; } }