Search in sources :

Example 16 with GraphicsConfiguration

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

the class WindowUtilsTest method xtestReveal.

public void xtestReveal() throws Exception {
    final int SIZE = 200;
    System.setProperty("sun.java2d.noddraw", "true");
    GraphicsConfiguration gconfig = WindowUtils.getAlphaCompatibleGraphicsConfiguration();
    Window w;
    Container content;
    if (true) {
        JFrame frame = new JFrame(getName(), gconfig);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        content = frame.getContentPane();
        w = frame;
    } else {
        Frame frame = JOptionPane.getRootFrame();
        JWindow window = new JWindow(frame, gconfig);
        content = window.getContentPane();
        w = window;
    }
    final Window f = w;
    WindowUtils.setWindowTransparent(f, true);
    content.add(new JButton("Quit") {

        private static final long serialVersionUID = 1L;

        {
            addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }
            });
        }
    }, BorderLayout.SOUTH);
    content.add(new JComponent() {

        private static final long serialVersionUID = 1L;

        public Dimension getPreferredSize() {
            return new Dimension(SIZE, SIZE);
        }

        protected void paintComponent(Graphics graphics) {
            Graphics2D g = (Graphics2D) graphics.create();
            g.setComposite(AlphaComposite.Clear);
            g.fillRect(0, 0, SIZE, SIZE);
            g.dispose();
            g = (Graphics2D) graphics.create();
            Color[] colors = { new Color(0, 0, 0), new Color(0, 0, 0, 128), new Color(128, 128, 128), new Color(128, 128, 128, 128), new Color(255, 255, 255), new Color(255, 255, 255, 128) };
            for (int i = 0; i < colors.length; i++) {
                g.setColor(colors[i]);
                g.fillRect((SIZE * i) / colors.length, 0, (SIZE + colors.length - 1) / colors.length, SIZE);
            }
            g.setColor(Color.red);
            g.drawRect(0, 0, SIZE - 1, SIZE - 1);
            g.dispose();
            SwingUtilities.getWindowAncestor(this).toFront();
        }
    });
    f.pack();
    f.addMouseListener(handler);
    f.addMouseMotionListener(handler);
    f.setLocation(100, 100);
    f.setVisible(true);
    while (f.isVisible()) {
        Thread.sleep(1000);
    //f.repaint();
    }
}
Also used : Window(java.awt.Window) JWindow(javax.swing.JWindow) JFrame(javax.swing.JFrame) Frame(java.awt.Frame) ActionEvent(java.awt.event.ActionEvent) JWindow(javax.swing.JWindow) Color(java.awt.Color) JButton(javax.swing.JButton) JComponent(javax.swing.JComponent) Dimension(java.awt.Dimension) Point(java.awt.Point) GraphicsConfiguration(java.awt.GraphicsConfiguration) Graphics2D(java.awt.Graphics2D) Graphics(java.awt.Graphics) Container(java.awt.Container) ActionListener(java.awt.event.ActionListener) JFrame(javax.swing.JFrame)

Example 17 with GraphicsConfiguration

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

the class WindowUtilsTest method xtestWindowTransparency.

// Expect failure on windows and x11, since transparent pixels are not 
// properly captured by java.awt.Robot
public void xtestWindowTransparency() throws Exception {
    if (GraphicsEnvironment.isHeadless())
        return;
    System.setProperty("sun.java2d.noddraw", "true");
    GraphicsConfiguration gconfig = WindowUtils.getAlphaCompatibleGraphicsConfiguration();
    Frame root = JOptionPane.getRootFrame();
    final Window background = new Window(root);
    background.setBackground(Color.white);
    background.setLocation(X, Y);
    final JWindow transparent = new JWindow(root, gconfig);
    transparent.setLocation(X, Y);
    ((JComponent) transparent.getContentPane()).setOpaque(false);
    transparent.getContentPane().add(new JComponent() {

        private static final long serialVersionUID = 1L;

        public Dimension getPreferredSize() {
            return new Dimension(W, H);
        }

        protected void paintComponent(Graphics g) {
            g = g.create();
            g.setColor(Color.red);
            g.fillRect(getWidth() / 4, getHeight() / 4, getWidth() / 2, getHeight() / 2);
            g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
            g.dispose();
        }
    });
    transparent.addMouseListener(handler);
    transparent.addMouseMotionListener(handler);
    SwingUtilities.invokeAndWait(new Runnable() {

        public void run() {
            background.pack();
            background.setSize(new Dimension(W, H));
            background.setVisible(true);
            transparent.pack();
            transparent.setSize(new Dimension(W, H));
            transparent.setVisible(true);
            transparent.toFront();
        }
    });
    WindowUtils.setWindowTransparent(transparent, true);
    //robot.delay(60000);
    Color sample = robot.getPixelColor(X + W / 2, Y + H / 2);
    assertEquals("Painted pixel should be opaque", Color.red, sample);
    sample = robot.getPixelColor(X + 10, Y + 10);
    assertEquals("Unpainted pixel should be transparent", Color.white, sample);
}
Also used : Window(java.awt.Window) JWindow(javax.swing.JWindow) Graphics(java.awt.Graphics) JFrame(javax.swing.JFrame) Frame(java.awt.Frame) JWindow(javax.swing.JWindow) Color(java.awt.Color) JComponent(javax.swing.JComponent) Dimension(java.awt.Dimension) GraphicsConfiguration(java.awt.GraphicsConfiguration)

Example 18 with GraphicsConfiguration

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

the class WindowUtilsTest method xtestWindowAlpha.

// Expect failure on windows and x11, since transparent pixels are not 
// properly captured by java.awt.Robot
public void xtestWindowAlpha() throws Exception {
    if (GraphicsEnvironment.isHeadless())
        return;
    System.setProperty("sun.java2d.noddraw", "true");
    GraphicsConfiguration gconfig = WindowUtils.getAlphaCompatibleGraphicsConfiguration();
    Frame root = JOptionPane.getRootFrame();
    final Window background = new Window(root);
    background.setBackground(Color.white);
    background.setLocation(X, Y);
    final Window transparent = new Window(root, gconfig);
    transparent.setBackground(Color.black);
    transparent.setLocation(X, Y);
    WindowUtils.setWindowAlpha(transparent, .5f);
    transparent.addMouseListener(handler);
    transparent.addMouseMotionListener(handler);
    SwingUtilities.invokeAndWait(new Runnable() {

        public void run() {
            background.pack();
            background.setSize(new Dimension(W, H));
            background.setVisible(true);
            transparent.pack();
            transparent.setSize(new Dimension(W, H));
            transparent.setVisible(true);
        }
    });
    //robot.delay(60000);
    Point where = new Point(transparent.getX() + W / 2, transparent.getY() + H / 2);
    Color sample = robot.getPixelColor(where.x, where.y);
    // NOTE: w32 won't sample non-opaque windows
    if (System.getProperty("os.name").startsWith("Windows")) {
        assertFalse("Sample not transparent (w32)", sample.equals(transparent.getBackground()));
    } else {
        assertEquals("Sample should be 50% fg/bg", new Color(128, 128, 128), sample);
    }
    SwingUtilities.invokeAndWait(new Runnable() {

        public void run() {
            WindowUtils.setWindowAlpha(transparent, 1f);
        }
    });
    sample = robot.getPixelColor(where.x, where.y);
    assertEquals("Window should be opaque with alpha=1f", transparent.getBackground(), sample);
    SwingUtilities.invokeAndWait(new Runnable() {

        public void run() {
            WindowUtils.setWindowAlpha(transparent, 0f);
        }
    });
    sample = robot.getPixelColor(where.x, where.y);
    assertEquals("Window should be transparent with alpha=0f", transparent.getBackground(), sample);
}
Also used : Window(java.awt.Window) JWindow(javax.swing.JWindow) JFrame(javax.swing.JFrame) Frame(java.awt.Frame) Color(java.awt.Color) Dimension(java.awt.Dimension) Point(java.awt.Point) GraphicsConfiguration(java.awt.GraphicsConfiguration)

Example 19 with GraphicsConfiguration

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

the class AEnv method positionCenterWindow.

//  showCenterWindow
/**
	 *	Position in center of the parent window
	 *
	 * @param parent Parent Window
	 * @param window Window to position
	 */
public static void positionCenterWindow(Window parent, Window window) {
    if (parent == null) {
        positionCenterScreen(window);
        return;
    }
    window.pack();
    //
    Dimension sSize = Toolkit.getDefaultToolkit().getScreenSize();
    // take into account task bar and other adornments
    GraphicsConfiguration config = window.getGraphicsConfiguration();
    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 in parent
    Rectangle pBounds = parent.getBounds();
    //	Parent is in upper left corner
    if (pBounds.x == pBounds.y && pBounds.x == 0) {
        positionCenterScreen(window);
        return;
    }
    //  Find middle
    int x = pBounds.x + ((pBounds.width - wSize.width) / 2);
    if (x < 0)
        x = 0;
    int y = pBounds.y + ((pBounds.height - wSize.height) / 2);
    if (y < 0)
        y = 0;
    //	Is it on Screen?
    if (x + wSize.width > sSize.width)
        x = sSize.width - wSize.width;
    if (y + wSize.height > sSize.height)
        y = sSize.height - wSize.height;
    //
    //	System.out.println("Position: x=" + x + " y=" + y + " w=" + wSize.getWidth() + " h=" + wSize.getHeight()
    //		+ " - Parent loc x=" + pLoc.x + " y=" + y + " w=" + pSize.getWidth() + " h=" + pSize.getHeight());
    window.setLocation(x + insets.left, y + insets.top);
}
Also used : Insets(java.awt.Insets) Rectangle(java.awt.Rectangle) Dimension(java.awt.Dimension) GraphicsConfiguration(java.awt.GraphicsConfiguration)

Example 20 with GraphicsConfiguration

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

the class Java2dHelper method createCompatibleImage.

public static BufferedImage createCompatibleImage(int width, int height) {
    GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice screenDevice = environment.getDefaultScreenDevice();
    GraphicsConfiguration configuration = screenDevice.getDefaultConfiguration();
    return configuration.createCompatibleImage(width, height);
}
Also used : GraphicsDevice(java.awt.GraphicsDevice) GraphicsEnvironment(java.awt.GraphicsEnvironment) GraphicsConfiguration(java.awt.GraphicsConfiguration)

Aggregations

GraphicsConfiguration (java.awt.GraphicsConfiguration)136 GraphicsEnvironment (java.awt.GraphicsEnvironment)55 BufferedImage (java.awt.image.BufferedImage)46 Rectangle (java.awt.Rectangle)44 GraphicsDevice (java.awt.GraphicsDevice)38 Graphics2D (java.awt.Graphics2D)34 VolatileImage (java.awt.image.VolatileImage)31 Point (java.awt.Point)30 Dimension (java.awt.Dimension)21 Insets (java.awt.Insets)17 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