Example usage for java.awt GraphicsEnvironment getLocalGraphicsEnvironment

List of usage examples for java.awt GraphicsEnvironment getLocalGraphicsEnvironment

Introduction

In this page you can find the example usage for java.awt GraphicsEnvironment getLocalGraphicsEnvironment.

Prototype

public static GraphicsEnvironment getLocalGraphicsEnvironment() 

Source Link

Document

Returns the local GraphicsEnvironment .

Usage

From source file:com.jug.MotherMachine.java

/**
 * PROJECT MAIN//from  w  ww  .j  av  a 2s  .  c o m
 * ============
 *
 * @param args
 *            muh!
 */
public static void main(final String[] args) {
    try {

        final MotherMachine main = new MotherMachine();
        guiFrame = new JFrame("Interactive MotherMachine");
        main.initMainWindow(guiFrame);

        props = main.loadParams();
        BGREM_TEMPLATE_XMIN = Integer
                .parseInt(props.getProperty("BGREM_TEMPLATE_XMIN", Integer.toString(BGREM_TEMPLATE_XMIN)));
        BGREM_TEMPLATE_XMAX = Integer
                .parseInt(props.getProperty("BGREM_TEMPLATE_XMAX", Integer.toString(BGREM_TEMPLATE_XMAX)));
        BGREM_X_OFFSET = Integer
                .parseInt(props.getProperty("BGREM_X_OFFSET", Integer.toString(BGREM_X_OFFSET)));
        GL_OFFSET_BOTTOM = Integer
                .parseInt(props.getProperty("GL_OFFSET_BOTTOM", Integer.toString(GL_OFFSET_BOTTOM)));
        GL_OFFSET_TOP = Integer.parseInt(props.getProperty("GL_OFFSET_TOP", Integer.toString(GL_OFFSET_TOP)));
        GL_OFFSET_LATERAL = Integer
                .parseInt(props.getProperty("GL_OFFSET_LATERAL", Integer.toString(GL_OFFSET_LATERAL)));
        MIN_CELL_LENGTH = Integer
                .parseInt(props.getProperty("MIN_CELL_LENGTH", Integer.toString(MIN_CELL_LENGTH)));
        MIN_GAP_CONTRAST = Double
                .parseDouble(props.getProperty("MIN_GAP_CONTRAST", Double.toString(MIN_GAP_CONTRAST)));
        SIGMA_PRE_SEGMENTATION_X = Double.parseDouble(
                props.getProperty("SIGMA_PRE_SEGMENTATION_X", Double.toString(SIGMA_PRE_SEGMENTATION_X)));
        SIGMA_PRE_SEGMENTATION_Y = Double.parseDouble(
                props.getProperty("SIGMA_PRE_SEGMENTATION_Y", Double.toString(SIGMA_PRE_SEGMENTATION_Y)));
        SIGMA_GL_DETECTION_X = Double
                .parseDouble(props.getProperty("SIGMA_GL_DETECTION_X", Double.toString(SIGMA_GL_DETECTION_X)));
        SIGMA_GL_DETECTION_Y = Double
                .parseDouble(props.getProperty("SIGMA_GL_DETECTION_Y", Double.toString(SIGMA_GL_DETECTION_Y)));
        DEFAULT_PATH = props.getProperty("DEFAULT_PATH", DEFAULT_PATH);

        GUI_POS_X = Integer.parseInt(props.getProperty("GUI_POS_X", Integer.toString(DEFAULT_GUI_POS_X)));
        GUI_POS_Y = Integer.parseInt(props.getProperty("GUI_POS_Y", Integer.toString(DEFAULT_GUI_POS_X)));
        GUI_WIDTH = Integer.parseInt(props.getProperty("GUI_WIDTH", Integer.toString(GUI_WIDTH)));
        GUI_HEIGHT = Integer.parseInt(props.getProperty("GUI_HEIGHT", Integer.toString(GUI_HEIGHT)));
        GUI_CONSOLE_WIDTH = Integer
                .parseInt(props.getProperty("GUI_CONSOLE_WIDTH", Integer.toString(GUI_CONSOLE_WIDTH)));
        // Iterate over all currently attached monitors and check if sceen position is actually possible,
        // otherwise fall back to the DEFAULT values and ignore the ones coming from the properties-file.
        boolean pos_ok = false;
        final GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        final GraphicsDevice[] gs = ge.getScreenDevices();
        for (int i = 0; i < gs.length; i++) {
            final DisplayMode dm = gs[i].getDisplayMode();
            if (gs[i].getDefaultConfiguration().getBounds()
                    .contains(new java.awt.Point(GUI_POS_X, GUI_POS_Y))) {
                pos_ok = true;
            }
        }
        // None of the screens contained the top-left window coordinates --> fall back onto default values...
        if (!pos_ok) {
            GUI_POS_X = DEFAULT_GUI_POS_X;
            GUI_POS_Y = DEFAULT_GUI_POS_Y;
        }

        String path = props.getProperty("import_path", System.getProperty("user.home"));
        final File fPath = main.showStartupDialog(guiFrame, path);
        path = fPath.getAbsolutePath();
        props.setProperty("import_path", fPath.getAbsolutePath());

        // Setting up console window and window snapper...
        main.initConsoleWindow();
        main.showConsoleWindow();
        final JFrameSnapper snapper = new JFrameSnapper();
        snapper.addFrame(main.frameConsoleWindow);
        snapper.addFrame(guiFrame);

        // ---------------------------------------------------
        main.processDataFromFolder(path);
        // ---------------------------------------------------

        System.out.print("Build and show GUI...");
        // show loaded and annotated data
        ImageJFunctions.show(main.imgRaw, "Rotated & cropped raw data");
        ImageJFunctions.show(main.imgTemp, "Temporary");
        ImageJFunctions.show(main.imgAnnotated, "Annotated ARGB data");

        final MotherMachineGui gui = new MotherMachineGui(new MotherMachineModel(main));
        gui.setVisible(true);

        main.ij = new ImageJ();
        guiFrame.add(gui);
        guiFrame.setSize(GUI_WIDTH, GUI_HEIGHT);
        guiFrame.setLocation(GUI_POS_X, GUI_POS_Y);
        guiFrame.setVisible(true);

        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                snapper.snapFrames(main.frameConsoleWindow, guiFrame, JFrameSnapper.EAST);
            }
        });

        System.out.println(" done!");
    } catch (final UnsatisfiedLinkError ulr) {
        JOptionPane.showMessageDialog(MotherMachine.guiFrame,
                "Could initialize Gurobi.\n"
                        + "You might not have installed Gurobi properly or you miss a valid license.\n"
                        + "Please visit 'www.gurobi.com' for further information.\n\n" + ulr.getMessage(),
                "Gurobi Error?", JOptionPane.ERROR_MESSAGE);
    }
}

From source file:Main.java

/**
 * Centers a Window on the screen<p>
 * Sets the window on the top left corner if the window's
 * dimensions are bigger than the screen's.
 *
 * @param window a Window object// w ww.  ja  va2s .  co m
 */
public static void centerOnScreen(Window window) {
    Point center = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
    Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();

    double w = Math.min(window.getWidth(), screen.width);
    double h = Math.min(window.getHeight(), screen.height);
    int x = (int) (center.x - (w / 2));
    int y = (int) (center.y - (h / 2));

    Point corner = new Point((x >= 0 ? x : 0), (y >= 0 ? y : 0));

    window.setLocation(corner);
}

From source file:Main.java

public static BufferedImage toBufferedImage(Image image) {
    if (image instanceof BufferedImage) {
        return (BufferedImage) image;
    }//from w  ww .j  ava  2s .co m

    // This code ensures that all the pixels in the image are loaded
    image = new ImageIcon(image).getImage();

    // Determine if the image has transparent pixels; for this method's
    // implementation, see Determining If an Image Has Transparent Pixels
    boolean hasAlpha = true;

    // Create a buffered image with a format that's compatible with the screen
    BufferedImage bimage = null;
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    try {
        // Determine the type of transparency of the new buffered image
        int transparency = Transparency.OPAQUE;
        if (hasAlpha) {
            transparency = Transparency.BITMASK;
        }

        // Create the buffered image
        GraphicsDevice gs = ge.getDefaultScreenDevice();
        GraphicsConfiguration gc = gs.getDefaultConfiguration();
        bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);
    } catch (HeadlessException e) {
        // The system does not have a screen
    }

    if (bimage == null) {
        // Create a buffered image using the default color model
        int type = BufferedImage.TYPE_INT_RGB;
        if (hasAlpha) {
            type = BufferedImage.TYPE_INT_ARGB;
        }
        bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
    }

    // Copy image to buffered image
    Graphics g = bimage.createGraphics();

    // Paint the image onto the buffered image
    g.drawImage(image, 0, 0, null);
    g.dispose();

    return bimage;
}

From source file:ImageUtil.java

/**
 * Posted by alpha02 at http://www.dreamincode.net/code/snippet1076.htm
 *///from w  ww  . jav a2s  .  c  om
public static BufferedImage toBufferedImage(Image image) {
    if (image instanceof BufferedImage)
        return (BufferedImage) image;

    // This code ensures that all the pixels in the image are loaded
    image = new ImageIcon(image).getImage();

    // Determine if the image has transparent pixels
    boolean hasAlpha = hasAlpha(image);

    // Create a buffered image with a format that's compatible with the screen
    BufferedImage bimage = null;

    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();

    try {
        // Determine the type of transparency of the new buffered image
        int transparency = Transparency.OPAQUE;

        if (hasAlpha == true)
            transparency = Transparency.BITMASK;

        // Create the buffered image
        GraphicsDevice gs = ge.getDefaultScreenDevice();
        GraphicsConfiguration gc = gs.getDefaultConfiguration();

        bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);
    } catch (HeadlessException e) {
    } //No screen

    if (bimage == null) {
        // Create a buffered image using the default color model
        int type = BufferedImage.TYPE_INT_RGB;

        if (hasAlpha == true) {
            type = BufferedImage.TYPE_INT_ARGB;
        }
        bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
    }

    // Copy image to buffered image
    Graphics g = bimage.createGraphics();

    // Paint the image onto the buffered image
    g.drawImage(image, 0, 0, null);
    g.dispose();

    return bimage;
}

From source file:Main.java

/**
 * Get the full screen size recognizing multiple monitor.
 * /*w w w. j a va2 s. c  o  m*/
 * @return full screen size
 */
public static Dimension getFullScreenSize() {
    Rectangle2D result = new Rectangle2D.Double();
    GraphicsEnvironment localGE = GraphicsEnvironment.getLocalGraphicsEnvironment();
    for (GraphicsDevice gd : localGE.getScreenDevices()) {
        for (GraphicsConfiguration graphicsConfiguration : gd.getConfigurations()) {
            Rectangle2D.union(result, graphicsConfiguration.getBounds(), result);
        }
    }
    return new Dimension((int) result.getWidth(), (int) result.getHeight());
}

From source file:Main.java

public static Point getCurrentScreenLocation(Window window) {
    GraphicsDevice currentScreen = getCurrentScreen(window);
    if (currentScreen == GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()) {
        return GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().getLocation();
    }/*from w  w  w .j  a  v a2 s.  c o  m*/
    return currentScreen.getDefaultConfiguration().getBounds().getLocation();
}

From source file:Main.java

/**
 * @return ScreenDimantions excluding toolbar 
 *///from   ww  w.  ja v a  2 s .c  o m
public static Rectangle availableScreenSize() {

    GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();

    // get maximum window bounds
    return graphicsEnvironment.getMaximumWindowBounds();

}

From source file:Main.java

public Main() {
    GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
    this.setExtendedState(this.getExtendedState() | this.MAXIMIZED_BOTH);
}

From source file:Main.java

/**
 * Centers the frame on the screen and sets its bounds. THis method should be
 * used after you call frame.pack() or frame.setSize().
 * @param frame// www.  ja v a2 s .com
 */
public static void centerFrame(JFrame frame) {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    Point center = ge.getCenterPoint();
    Rectangle bounds = ge.getMaximumWindowBounds();
    int w = Math.max(bounds.width / 2, Math.min(frame.getWidth(), bounds.width));
    int h = Math.max(bounds.height / 2, Math.min(frame.getHeight(), bounds.height));
    int x = center.x - w / 2, y = center.y - h / 2;

    frame.setBounds(x, y, w, h);

    if ((w == bounds.width) && (h == bounds.height)) {
        frame.setExtendedState(Frame.MAXIMIZED_BOTH);
    }

    frame.validate();
}

From source file:Main.java

/**
 * Returns system font names array.//from w w  w. ja  v a 2 s . co m
 *
 * @return system font names array
 */
public static String[] getFontNames() {
    if (fontNames == null) {
        fontNames = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
    }
    return fontNames;
}