Search in sources :

Example 1 with GraphicsConfiguration

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

the class AlphaMaskDemo2 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);
    icon = new JLabel();
    icon.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    alphaWindow.getContentPane().add(icon);
    JButton quit = new JButton("Quit");
    JLabel label = new JLabel("Drag this window by its image");
    label.setHorizontalAlignment(SwingConstants.CENTER);
    alphaWindow.getContentPane().add(label, BorderLayout.NORTH);
    alphaWindow.getContentPane().add(quit, BorderLayout.SOUTH);
    quit.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    });
    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;
        }

        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.err.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.err.println("Can't parse text: " + html);
                    return false;
                }
                System.err.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);
    p.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    final JSlider slider = new JSlider(0, 255, 255);
    slider.addChangeListener(new ChangeListener() {

        public void stateChanged(ChangeEvent e) {
            int value = slider.getValue();
            WindowUtils.setWindowAlpha(alphaWindow, value / 255f);
        }
    });
    p.add(slider, BorderLayout.SOUTH);
    frame.getContentPane().add(p);
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    centerOnScreen(frame);
    frame.setVisible(true);
    WindowUtils.setWindowTransparent(alphaWindow, true);
    alphaWindow.setLocation(frame.getX() + frame.getWidth() + 4, frame.getY());
    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) ActionEvent(java.awt.event.ActionEvent) JButton(javax.swing.JButton) Reader(java.io.Reader) Image(java.awt.Image) 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) 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) ActionListener(java.awt.event.ActionListener) ChangeEvent(javax.swing.event.ChangeEvent) TransferHandler(javax.swing.TransferHandler) File(java.io.File) MouseInputAdapter(javax.swing.event.MouseInputAdapter)

Example 2 with GraphicsConfiguration

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

the class DragHandler method dragGestureRecognized.

/** Called when a user drag gesture is recognized.  This method is 
     * responsible for initiating the drag operation.
     * @param e event
     */
public void dragGestureRecognized(DragGestureEvent e) {
    if ((e.getDragAction() & supportedActions) != 0 && canDrag(e)) {
        setModifiers(e.getTriggerEvent().getModifiersEx() & KEY_MASK);
        Transferable transferable = getTransferable(e);
        if (transferable == null)
            return;
        try {
            Point srcOffset = new Point(0, 0);
            Icon icon = getDragIcon(e, srcOffset);
            Point origin = e.getDragOrigin();
            // offset of the image origin from the cursor
            imageOffset = new Point(srcOffset.x - origin.x, srcOffset.y - origin.y);
            Icon dragIcon = scaleDragIcon(icon, imageOffset);
            Cursor cursor = null;
            if (dragIcon != null && DragSource.isDragImageSupported()) {
                GraphicsConfiguration gc = e.getComponent().getGraphicsConfiguration();
                e.startDrag(cursor, createDragImage(gc, dragIcon), imageOffset, transferable, this);
            } else {
                if (dragIcon != null) {
                    Point screen = dragSource.getLocationOnScreen();
                    screen.translate(origin.x, origin.y);
                    Point cursorOffset = new Point(-imageOffset.x, -imageOffset.y);
                    ghost = new GhostedDragImage(dragSource, dragIcon, getImageLocation(screen), cursorOffset);
                    ghost.setAlpha(ghostAlpha);
                }
                e.startDrag(cursor, transferable, this);
            }
            dragStarted(e);
            moved = false;
            e.getDragSource().addDragSourceMotionListener(this);
            DragHandler.transferable = transferable;
        } catch (InvalidDnDOperationException ex) {
            if (ghost != null) {
                ghost.dispose();
                ghost = null;
            }
        }
    }
}
Also used : InvalidDnDOperationException(java.awt.dnd.InvalidDnDOperationException) Transferable(java.awt.datatransfer.Transferable) Point(java.awt.Point) Icon(javax.swing.Icon) Cursor(java.awt.Cursor) GraphicsConfiguration(java.awt.GraphicsConfiguration)

Example 3 with GraphicsConfiguration

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

the class RecorderPanelTest method testGetScreen.

/**
     * Test of getScreen method, of class RecorderPanel.
     */
@Test
public void testGetScreen() {
    System.out.println("getScreen");
    GraphicsConfiguration gd = instance.getGraphicsConfiguration();
    GraphicsDevice expResult = gd.getDevice();
    GraphicsDevice result = instance.getScreen();
    assertNotNull(result);
    assertEquals(expResult, result);
}
Also used : GraphicsDevice(java.awt.GraphicsDevice) GraphicsConfiguration(java.awt.GraphicsConfiguration) Test(org.junit.Test)

Example 4 with GraphicsConfiguration

use of java.awt.GraphicsConfiguration in project jdk8u_jdk by JetBrains.

the class FillTexturePaint method main.

public static void main(final String[] args) {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsConfiguration gc = ge.getDefaultScreenDevice().getDefaultConfiguration();
    VolatileImage vi = gc.createCompatibleVolatileImage(size, size);
    while (true) {
        vi.validate(gc);
        Graphics2D g2d = vi.createGraphics();
        g2d.setComposite(AlphaComposite.Src);
        g2d.setPaint(shape);
        g2d.fill(new Rectangle(0, 0, size, size));
        g2d.dispose();
        if (vi.validate(gc) != VolatileImage.IMAGE_OK) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException ignored) {
            }
            continue;
        }
        BufferedImage bi = vi.getSnapshot();
        if (vi.contentsLost()) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException ignored) {
            }
            continue;
        }
        for (int x = 0; x < size; ++x) {
            for (int y = 0; y < size; ++y) {
                if (bi.getRGB(x, y) != Color.GREEN.getRGB()) {
                    throw new RuntimeException("Test failed.");
                }
            }
        }
        break;
    }
}
Also used : VolatileImage(java.awt.image.VolatileImage) Rectangle(java.awt.Rectangle) GraphicsEnvironment(java.awt.GraphicsEnvironment) BufferedImage(java.awt.image.BufferedImage) TexturePaint(java.awt.TexturePaint) GraphicsConfiguration(java.awt.GraphicsConfiguration) Graphics2D(java.awt.Graphics2D)

Example 5 with GraphicsConfiguration

use of java.awt.GraphicsConfiguration in project jdk8u_jdk by JetBrains.

the class FlipDrawImage method main.

public static void main(final String[] args) {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsConfiguration gc = ge.getDefaultScreenDevice().getDefaultConfiguration();
    VolatileImage vi = gc.createCompatibleVolatileImage(width, height);
    final BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    while (true) {
        vi.validate(gc);
        Graphics2D g2d = vi.createGraphics();
        g2d.setColor(Color.red);
        g2d.fillRect(0, 0, width, height);
        g2d.setColor(Color.green);
        g2d.fillRect(0, 0, width / 2, height / 2);
        g2d.dispose();
        if (vi.validate(gc) != VolatileImage.IMAGE_OK) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException ignored) {
            }
            continue;
        }
        Graphics2D g = bi.createGraphics();
        g.setComposite(AlphaComposite.Src);
        g.setColor(Color.BLUE);
        g.fillRect(0, 0, width, height);
        // destination width and height are flipped and scale is used.
        g.drawImage(vi, width / 2, height / 2, -width / 2, -height / 2, null, null);
        g.dispose();
        if (vi.contentsLost()) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException ignored) {
            }
            continue;
        }
        for (int x = 0; x < width; ++x) {
            for (int y = 0; y < height; ++y) {
                if (x < width / 2 && y < height / 2) {
                    if (x >= width / 4 && y >= height / 4) {
                        if (bi.getRGB(x, y) != Color.green.getRGB()) {
                            throw new RuntimeException("Test failed.");
                        }
                    } else if (bi.getRGB(x, y) != Color.red.getRGB()) {
                        throw new RuntimeException("Test failed.");
                    }
                } else {
                    if (bi.getRGB(x, y) != Color.BLUE.getRGB()) {
                        throw new RuntimeException("Test failed.");
                    }
                }
            }
        }
        break;
    }
}
Also used : VolatileImage(java.awt.image.VolatileImage) GraphicsEnvironment(java.awt.GraphicsEnvironment) BufferedImage(java.awt.image.BufferedImage) GraphicsConfiguration(java.awt.GraphicsConfiguration) Graphics2D(java.awt.Graphics2D)

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