Search in sources :

Example 71 with GraphicsConfiguration

use of java.awt.GraphicsConfiguration in project jna by java-native-access.

the class BalloonTipManager method getBalloonTip.

/**
   * Returns the popup window of the balloon tip.
   * @param owner the owner component for the balloon tip
   * @param content the text string to display in the balloon tip
   * @param x the x coordinate for the origin for the balloon tip in relation
   * to the owner component
   * @param y the y coordinate for the origin for the balloon tip in relation
   * to the owner component
   * @param position the position for the balloon; either above or below the
     * owner component
   * @param duration the duration in milliseconds to display balloon tip
   * @param bordercolor the background color for the balloon tip
   * @param backgroundcolor the border color for the balloon tip
   * @param textcolor the text color for the balloon tip
   * @return the popup window of the balloon tip
   */
public static Popup getBalloonTip(final Component owner, final String content, int x, int y, final Integer duration, final Color bordercolor, final Color backgroundcolor, final Color textcolor) {
    final Point origin = owner == null ? new Point(0, 0) : owner.getLocationOnScreen();
    final Window parent = owner != null ? SwingUtilities.getWindowAncestor(owner) : null;
    final String text = content != null ? content : "";
    final Integer timerDuration = duration != null ? duration : 10000;
    origin.translate(x, y);
    vpos = VPOS_BELOW;
    hpos = HPOS_LEFT;
    return new Popup() {

        private BalloonTip bt = null;

        final ComponentEar componentEar = new ComponentEar();

        final MouseEar mouseEar = new MouseEar();

        final FocusEar focusEar = new FocusEar();

        /*
       * (non-Javadoc)
       * @see javax.swing.Popup#show()
       */
        public void show() {
            hidePopupTimer = new Timer(timerDuration, new TimerAction());
            bt = new BalloonTip(parent, text, origin, bordercolor, backgroundcolor, textcolor);
            bt.pack();
            Point pt = new Point(origin);
            pt.translate(10, owner.getHeight());
            bt.setLocation(getAdjustedOrigin(pt));
            bt.setWindowMask();
            bt.setVisible(true);
            owner.addFocusListener(focusEar);
            owner.addMouseListener(mouseEar);
            parent.addMouseListener(mouseEar);
            parent.addComponentListener(componentEar);
            hidePopupTimer.start();
            isShowing = true;
        }

        /*
       * (non-Javadoc)
       * @see javax.swing.Popup#hide()
       */
        public void hide() {
            if (bt != null) {
                isShowing = false;
                hidePopupTimer.stop();
                parent.removeComponentListener(componentEar);
                parent.removeMouseListener(mouseEar);
                owner.removeMouseListener(mouseEar);
                owner.removeFocusListener(focusEar);
                bt.setVisible(false);
                bt.dispose();
            }
        }

        /*
       * Adjust the location of the balloon popup so that is drawn completely
       * on the screen and specify the orientation.
       */
        private Point getAdjustedOrigin(Point pt) {
            Point ret = new Point(pt.x, pt.y);
            GraphicsConfiguration gc = owner.getGraphicsConfiguration();
            Rectangle sBounds = gc.getBounds();
            Insets sInsets = Toolkit.getDefaultToolkit().getScreenInsets(gc);
            sBounds.x += sInsets.left;
            sBounds.y += sInsets.top;
            sBounds.width -= (sInsets.left + sInsets.right);
            sBounds.height -= (sInsets.top + sInsets.bottom);
            if (ret.x < sBounds.x) {
                ret.x = sBounds.x;
            } else if (ret.x - sBounds.x + bt.getWidth() > sBounds.width) {
                ret.x = owner.getLocationOnScreen().x - bt.getWidth() + 43;
            }
            if (ret.x >= pt.x) {
                hpos = HPOS_LEFT;
            } else {
                hpos = HPOS_RIGHT;
            }
            if (ret.y < sBounds.y) {
                ret.y = sBounds.y;
            } else if (ret.y - sBounds.y + bt.getHeight() > sBounds.height) {
                ret.y = owner.getLocationOnScreen().y - bt.getHeight();
            }
            if (ret.y >= pt.y) {
                vpos = VPOS_BELOW;
            } else {
                vpos = VPOS_ABOVE;
            }
            return ret;
        }

        /*
       * This class handles actions from the balloon tip timer.
       */
        @SuppressWarnings("serial")
        final class TimerAction extends AbstractAction {

            /*
         * (non-Javadoc)
         * @see java.awt.event.ActionListener#actionPerformed(
         * java.awt.event.ActionEvent)
         */
            public void actionPerformed(ActionEvent e) {
                hide();
            }
        }

        /*
       * This class handles events spawned from moving the component.
       */
        final class ComponentEar extends ComponentAdapter {

            /*
         * (non-Javadoc)
         * @see java.awt.event.ComponentAdapter#componentMoved(
         * java.awt.event.ComponentEvent)
         */
            public void componentMoved(ComponentEvent e) {
                hide();
            }
        }

        /*
       * This class handles events spawned when a mouse button is pressed.
       */
        final class MouseEar extends MouseAdapter {

            /*
         * (non-Javadoc)
         * @see java.awt.event.MouseAdapter#mousePressed(
         * java.awt.event.MouseEvent)
         */
            public void mousePressed(MouseEvent e) {
                hide();
            }
        }

        /*
       * This class handles events spawned when the component loses focus.
       */
        final class FocusEar extends FocusAdapter {

            /*
         * (non-Javadoc)
         * @see java.awt.event.FocusAdapter#focusLost(
         * java.awt.event.FocusEvent)
         */
            public void focusLost(FocusEvent e) {
                hide();
            }
        }
    };
}
Also used : Window(java.awt.Window) JWindow(javax.swing.JWindow) Insets(java.awt.Insets) MouseEvent(java.awt.event.MouseEvent) ActionEvent(java.awt.event.ActionEvent) Rectangle(java.awt.Rectangle) Point(java.awt.Point) FocusEvent(java.awt.event.FocusEvent) GraphicsConfiguration(java.awt.GraphicsConfiguration) Timer(javax.swing.Timer) Popup(javax.swing.Popup) ComponentEvent(java.awt.event.ComponentEvent)

Example 72 with GraphicsConfiguration

use of java.awt.GraphicsConfiguration in project jna by java-native-access.

the class AlphaMaskDemo method run.

public void run() {
    // Must find a graphics configuration with a depth of 32 bits
    GraphicsConfiguration gconfig = WindowUtils.getAlphaCompatibleGraphicsConfiguration();
    frame = new JFrame("Alpha Mask Demo");
    alphaWindow = new JWindow(frame, gconfig);
    MouseInputAdapter handler = new MouseInputAdapter() {

        private Point offset;

        public void mousePressed(MouseEvent e) {
            if (SwingUtilities.isLeftMouseButton(e))
                offset = e.getPoint();
        }

        public void mouseReleased(MouseEvent e) {
            offset = null;
            // hit testing; not sure why it gets disabled
            if (System.getProperty("os.name").startsWith("Windows"))
                update(true, true);
        }

        public void mouseDragged(MouseEvent e) {
            if (offset != null) {
                Window w = (Window) e.getSource();
                Point where = e.getPoint();
                where.translate(-offset.x, -offset.y);
                Point loc = w.getLocationOnScreen();
                loc.translate(where.x, where.y);
                w.setLocation(loc.x, loc.y);
            }
        }
    };
    alphaWindow.addMouseListener(handler);
    alphaWindow.addMouseMotionListener(handler);
    JPanel p = new JPanel(new BorderLayout(8, 8));
    p.setBorder(new EmptyBorder(8, 8, 8, 8));
    p.setTransferHandler(new TransferHandler() {

        private static final long serialVersionUID = 1L;

        public boolean canImport(JComponent comp, DataFlavor[] transferFlavors) {
            List<DataFlavor> list = Arrays.asList(transferFlavors);
            if (list.contains(URL_FLAVOR) || list.contains(URI_LIST_FLAVOR) || list.contains(DataFlavor.imageFlavor) || list.contains(DataFlavor.javaFileListFlavor)) {
                return true;
            }
            if (DataFlavor.selectBestTextFlavor(transferFlavors) != null) {
                return true;
            }
            System.out.println("No acceptable flavor found in " + Arrays.asList(transferFlavors));
            return false;
        }

        public boolean importData(JComponent comp, Transferable t) {
            try {
                if (t.isDataFlavorSupported(URL_FLAVOR)) {
                    URL url = (URL) t.getTransferData(URL_FLAVOR);
                    setImage(Toolkit.getDefaultToolkit().getImage(url));
                    return true;
                }
                if (t.isDataFlavorSupported(URI_LIST_FLAVOR)) {
                    String s = (String) t.getTransferData(URI_LIST_FLAVOR);
                    String[] uris = s.split("[\r\n]");
                    if (uris.length > 0) {
                        URL url = new URL(uris[0]);
                        setImage(Toolkit.getDefaultToolkit().getImage(url));
                        return true;
                    }
                    return false;
                }
                if (t.isDataFlavorSupported(DataFlavor.imageFlavor)) {
                    Image image = (Image) t.getTransferData(DataFlavor.imageFlavor);
                    setImage(image);
                    return true;
                }
                if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
                    List<File> files = (List<File>) t.getTransferData(DataFlavor.javaFileListFlavor);
                    File f = files.get(0);
                    URL url = new URL("file://" + f.toURI().toURL().getPath());
                    Image image = Toolkit.getDefaultToolkit().getImage(url);
                    setImage(image);
                    return true;
                }
                DataFlavor flavor = DataFlavor.selectBestTextFlavor(t.getTransferDataFlavors());
                if (flavor != null) {
                    Reader reader = flavor.getReaderForText(t);
                    char[] buf = new char[512];
                    StringBuilder b = new StringBuilder();
                    int count;
                    // encoding wrong
                    while ((count = reader.read(buf)) > 0) {
                        for (int i = 0; i < count; i++) {
                            if (buf[i] != 0)
                                b.append(buf, i, 1);
                        }
                    }
                    String html = b.toString();
                    Pattern p = Pattern.compile("<img.*src=\"([^\\\"\">]+)\"", Pattern.CANON_EQ | Pattern.UNICODE_CASE);
                    Matcher m = p.matcher(html);
                    if (m.find()) {
                        URL url = new URL(m.group(1));
                        System.out.println("Load image from " + url);
                        Image image = Toolkit.getDefaultToolkit().getImage(url);
                        setImage(image);
                        return true;
                    }
                    System.out.println("Can't parse text: " + html);
                    return false;
                }
                System.out.println("No flavor available: " + Arrays.asList(t.getTransferDataFlavors()));
            } catch (UnsupportedFlavorException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Throwable e) {
                e.printStackTrace();
            }
            return false;
        }
    });
    p.add(new JLabel("<html><center>Drop an image with an alpha channel onto this window<br>" + "You may also adjust the overall transparency with the slider</center></html>"), BorderLayout.NORTH);
    final JSlider slider = new JSlider(0, 255, 255);
    slider.addChangeListener(new ChangeListener() {

        public void stateChanged(ChangeEvent e) {
            int value = slider.getValue();
            setAlpha(value / 255f);
        }
    });
    p.add(slider, BorderLayout.SOUTH);
    frame.getContentPane().add(p);
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    centerOnScreen(frame);
    frame.setVisible(true);
    p.addMouseListener(new MouseAdapter() {

        public void mousePressed(MouseEvent e) {
            update();
        }
    });
    try {
        URL url = getClass().getResource("tardis.png");
        if (url != null) {
            setImage(Toolkit.getDefaultToolkit().getImage(url));
        }
    } catch (Exception e) {
    }
}
Also used : JPanel(javax.swing.JPanel) Matcher(java.util.regex.Matcher) Reader(java.io.Reader) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) URL(java.net.URL) GraphicsConfiguration(java.awt.GraphicsConfiguration) DataFlavor(java.awt.datatransfer.DataFlavor) BorderLayout(java.awt.BorderLayout) JFrame(javax.swing.JFrame) JSlider(javax.swing.JSlider) List(java.util.List) ChangeListener(javax.swing.event.ChangeListener) EmptyBorder(javax.swing.border.EmptyBorder) Window(java.awt.Window) JWindow(javax.swing.JWindow) Pattern(java.util.regex.Pattern) MouseEvent(java.awt.event.MouseEvent) JWindow(javax.swing.JWindow) JComponent(javax.swing.JComponent) Transferable(java.awt.datatransfer.Transferable) MouseAdapter(java.awt.event.MouseAdapter) JLabel(javax.swing.JLabel) Point(java.awt.Point) IOException(java.io.IOException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) IOException(java.io.IOException) ChangeEvent(javax.swing.event.ChangeEvent) TransferHandler(javax.swing.TransferHandler) File(java.io.File) MouseInputAdapter(javax.swing.event.MouseInputAdapter)

Example 73 with GraphicsConfiguration

use of java.awt.GraphicsConfiguration in project screenbird by adamhub.

the class ScreenUtilTest method testGetScreenDimension.

/**
     * Test of getScreenDimension method, of class ScreenUtil.
     */
@Test
public void testGetScreenDimension() {
    log("getScreenDimension");
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsConfiguration gd = ge.getScreenDevices()[0].getDefaultConfiguration();
    GraphicsDevice device = gd.getDevice();
    Dimension result = ScreenUtil.getScreenDimension(device);
    assertNotNull(result);
    assertNotSame(0, result.height);
    assertNotSame(0, result.width);
}
Also used : GraphicsDevice(java.awt.GraphicsDevice) Dimension(java.awt.Dimension) GraphicsEnvironment(java.awt.GraphicsEnvironment) GraphicsConfiguration(java.awt.GraphicsConfiguration) Test(org.junit.Test)

Example 74 with GraphicsConfiguration

use of java.awt.GraphicsConfiguration in project processing by processing.

the class EditorState method update.

void update(Editor editor) {
    //    path = editor.getSketch().getMainFilePath();
    editorBounds = editor.getBounds();
    dividerLocation = editor.getDividerLocation();
    GraphicsConfiguration config = editor.getGraphicsConfiguration();
    //      GraphicsDevice device = config.getDevice();
    deviceBounds = config.getBounds();
//      deviceName = device.getIDstring();
}
Also used : GraphicsConfiguration(java.awt.GraphicsConfiguration)

Example 75 with GraphicsConfiguration

use of java.awt.GraphicsConfiguration in project adempiere by adempiere.

the class AEnv method positionScreen.

//  showScreen
/**
	 *	Position window in center of the screen
	 * 	@param window Window to position
	 * 	@param position SwingConstants
	 */
public static void positionScreen(Window window, int position) {
    window.pack();
    // take into account task bar and other adornments
    GraphicsConfiguration config = window.getGraphicsConfiguration();
    Rectangle bounds = config.getBounds();
    Dimension sSize = bounds.getSize();
    Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(config);
    sSize.width -= (insets.left + insets.right);
    sSize.height -= (insets.top + insets.bottom);
    Dimension wSize = window.getSize();
    //	fit on window
    if (wSize.height > sSize.height)
        wSize.height = sSize.height;
    if (wSize.width > sSize.width)
        wSize.width = sSize.width;
    window.setSize(wSize);
    //	Center
    int x = (sSize.width - wSize.width) / 2;
    int y = (sSize.height - wSize.height) / 2;
    if (position == SwingConstants.CENTER)
        ;
    else if (position == SwingConstants.NORTH_WEST) {
        x = 0;
        y = 0;
    } else if (position == SwingConstants.NORTH) {
        y = 0;
    } else if (position == SwingConstants.NORTH_EAST) {
        x = (sSize.width - wSize.width);
        y = 0;
    } else if (position == SwingConstants.WEST) {
        x = 0;
    } else if (position == SwingConstants.EAST) {
        x = (sSize.width - wSize.width);
    } else if (position == SwingConstants.SOUTH) {
        y = (sSize.height - wSize.height);
    } else if (position == SwingConstants.SOUTH_WEST) {
        x = 0;
        y = (sSize.height - wSize.height);
    } else if (position == SwingConstants.SOUTH_EAST) {
        x = (sSize.width - wSize.width);
        y = (sSize.height - wSize.height);
    }
    //
    window.setLocation(bounds.x + x + insets.left, bounds.y + y + insets.top);
}
Also used : Insets(java.awt.Insets) Rectangle(java.awt.Rectangle) Dimension(java.awt.Dimension) GraphicsConfiguration(java.awt.GraphicsConfiguration)

Aggregations

GraphicsConfiguration (java.awt.GraphicsConfiguration)134 GraphicsEnvironment (java.awt.GraphicsEnvironment)55 BufferedImage (java.awt.image.BufferedImage)46 Rectangle (java.awt.Rectangle)43 GraphicsDevice (java.awt.GraphicsDevice)38 Graphics2D (java.awt.Graphics2D)33 VolatileImage (java.awt.image.VolatileImage)31 Point (java.awt.Point)29 Dimension (java.awt.Dimension)21 Insets (java.awt.Insets)16 File (java.io.File)16 Graphics (java.awt.Graphics)13 IOException (java.io.IOException)13 Frame (java.awt.Frame)11 JFrame (javax.swing.JFrame)8 HeadlessException (java.awt.HeadlessException)7 Window (java.awt.Window)7 ImageIcon (javax.swing.ImageIcon)7 Color (java.awt.Color)6 AffineTransform (java.awt.geom.AffineTransform)6