Java Graphics Draw getGraphicsDevice(Component comp)

Here you can find the source of getGraphicsDevice(Component comp)

Description

Tries to determine the GraphicsDevice that the specified component is located on.

License

Open Source License

Parameter

Parameter Description
comp the component to determine the graphics device for

Return

the device, null if failed to determine

Declaration

public static GraphicsDevice getGraphicsDevice(Component comp) 

Method Source Code

//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;
    }
}

Related

  1. drawWeightBox(Graphics2D g2d, int ycentre, int xcentre, int width)
  2. drawWinUpperLeft(Graphics g, int which, Color fill, int x, int y, int fmWidth, int fmHeight)
  3. drawXMajorScaleTick(Graphics g, int x, int y)
  4. fillVisible(final Graphics g, final JComponent c)
  5. fitToWidthAndHeight(Graphics2D g2, JComponent component, int width, int height)
  6. getTranslatedGraphics(Graphics g, PageFormat pf, int pageIndex, Component component)
  7. getUsableScreenBounds(GraphicsConfiguration gconf)
  8. paint(Graphics2D g)
  9. paint(Graphics2D g)