Search in sources :

Example 41 with Graphics

use of java.awt.Graphics 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 42 with Graphics

use of java.awt.Graphics 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 43 with Graphics

use of java.awt.Graphics in project DistributedFractalNetwork by Budder21.

the class RoundedButton method processMouseEvent.

/**
     * Paints the button and distribute an action event to all listeners.
     */
@Override
public void processMouseEvent(MouseEvent e) {
    Graphics g;
    switch(e.getID()) {
        case MouseEvent.MOUSE_PRESSED:
            // render myself inverted....
            pressed = true;
            // Repaint might flicker a bit. To avoid this, you can use
            // double buffering (see the Gauge example).
            repaint();
            break;
        case MouseEvent.MOUSE_RELEASED:
            if (actionListener != null) {
                actionListener.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, label));
            }
            // render myself normal again
            if (pressed == true) {
                pressed = false;
                // Repaint might flicker a bit. To avoid this, you can use
                // double buffering (see the Gauge example).
                repaint();
            }
            break;
        case MouseEvent.MOUSE_ENTERED:
            break;
        case MouseEvent.MOUSE_EXITED:
            if (pressed == true) {
                // Cancel! Don't send action event.
                pressed = false;
                // Repaint might flicker a bit. To avoid this, you can use
                // double buffering (see the Gauge example).
                repaint();
            // Note: for a more complete button implementation,
            // you wouldn't want to cancel at this point, but
            // rather detect when the mouse re-entered, and
            // re-highlight the button. There are a few state
            // issues that that you need to handle, which we leave
            // this an an excercise for the reader (I always
            // wanted to say that!)
            }
            break;
    }
    super.processMouseEvent(e);
}
Also used : Graphics(java.awt.Graphics) ActionEvent(java.awt.event.ActionEvent)

Example 44 with Graphics

use of java.awt.Graphics in project lwjgl by LWJGL.

the class AppletLoader method paint.

/*
	 * @see java.awt.Container#paint(java.awt.Graphics)
	 */
public void paint(Graphics g) {
    // don't paint loader if applet loaded
    if (state == STATE_DONE) {
        // clean up resources
        cleanUp();
        return;
    }
    // no drawing in headless mode
    if (headless)
        return;
    // create offscreen if missing
    if (offscreen == null) {
        offscreen = createImage(getWidth(), getHeight());
        // create buffers for animated gifs
        if (logo != null) {
            logoBuffer = createImage(logo.getWidth(null), logo.getHeight(null));
            // add image observer, it will notify when next animated gif frame is ready
            offscreen.getGraphics().drawImage(logo, 0, 0, this);
            // in case image is not animated fill image buffer once
            imageUpdate(logo, ImageObserver.FRAMEBITS, 0, 0, 0, 0);
        }
        if (progressbar != null) {
            progressbarBuffer = createImage(progressbar.getWidth(null), progressbar.getHeight(null));
            // add image observer, it will notify when next animated gif frame is ready
            offscreen.getGraphics().drawImage(progressbar, 0, 0, this);
            // in case image is not animated fill image buffer once
            imageUpdate(progressbar, ImageObserver.FRAMEBITS, 0, 0, 0, 0);
        }
    }
    // draw everything onto an image before drawing to avoid flicker
    Graphics og = offscreen.getGraphics();
    FontMetrics fm = og.getFontMetrics();
    // clear background color
    og.setColor(bgColor);
    og.fillRect(0, 0, offscreen.getWidth(null), offscreen.getHeight(null));
    og.setColor(fgColor);
    // if we had a failure of some sort, notify the user
    if (fatalError) {
        for (int i = 0; i < errorMessage.length; i++) {
            if (errorMessage[i] != null) {
                int messageX = (offscreen.getWidth(null) - fm.stringWidth(errorMessage[i])) / 2;
                int messageY = (offscreen.getHeight(null) - (fm.getHeight() * errorMessage.length)) / 2;
                og.drawString(errorMessage[i], messageX, messageY + i * fm.getHeight());
            }
        }
    } else {
        og.setColor(fgColor);
        painting = true;
        // get position at the middle of the offscreen buffer
        int x = offscreen.getWidth(null) / 2;
        int y = offscreen.getHeight(null) / 2;
        // draw logo
        if (logo != null) {
            og.drawImage(logoBuffer, x - logo.getWidth(null) / 2, y - logo.getHeight(null) / 2, this);
        }
        // draw message
        String message = getDescriptionForState();
        int messageX = (offscreen.getWidth(null) - fm.stringWidth(message)) / 2;
        int messageY = y + 20;
        if (logo != null)
            messageY += logo.getHeight(null) / 2;
        else if (progressbar != null)
            messageY += progressbar.getHeight(null) / 2;
        og.drawString(message, messageX, messageY);
        // draw subtaskmessage, if any
        if (subtaskMessage.length() > 0) {
            messageX = (offscreen.getWidth(null) - fm.stringWidth(subtaskMessage)) / 2;
            og.drawString(subtaskMessage, messageX, messageY + 20);
        }
        // draw loading progress bar, clipping it depending on percentage done
        if (progressbar != null) {
            int barSize = (progressbar.getWidth(null) * percentage) / 100;
            og.clipRect(x - progressbar.getWidth(null) / 2, 0, barSize, offscreen.getHeight(null));
            og.drawImage(progressbarBuffer, x - progressbar.getWidth(null) / 2, y - progressbar.getHeight(null) / 2, this);
        }
        painting = false;
    }
    og.dispose();
    // finally draw it all centred
    g.drawImage(offscreen, (getWidth() - offscreen.getWidth(null)) / 2, (getHeight() - offscreen.getHeight(null)) / 2, null);
}
Also used : Graphics(java.awt.Graphics) FontMetrics(java.awt.FontMetrics)

Example 45 with Graphics

use of java.awt.Graphics in project ChatGameFontificator by GlitchCog.

the class ControlWindow method saveScreenshot.

/**
     * Takes and saves a screenshot of the current chat window
     * 
     * @return whether the screenshot was saved
     */
private boolean saveScreenshot() {
    // Take the screenshot before the save file chooser is shown
    ChatPanel chat = chatWindow.getChatPanel();
    BufferedImage chatImage = new BufferedImage(chat.getWidth(), chat.getHeight(), screenshotOptions.isTransparencyEnabled() ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB);
    Graphics chatGraphics = chatImage.getGraphics();
    chat.paint(chatGraphics);
    final boolean chromaEnabled = Boolean.toString(true).equalsIgnoreCase(fProps.getProperty(FontificatorProperties.KEY_CHAT_CHROMA_ENABLED));
    if (screenshotOptions.isTransparencyEnabled() && chromaEnabled) {
        final int chromaKey = new Color(Integer.parseInt(fProps.getProperty(FontificatorProperties.KEY_COLOR_CHROMA_KEY), 16)).getRGB();
        final int transparentPixel = new Color(0, true).getRGB();
        for (int r = 0; r < chatImage.getHeight(); r++) {
            for (int c = 0; c < chatImage.getWidth(); c++) {
                if (chatImage.getRGB(c, r) == chromaKey) {
                    chatImage.setRGB(c, r, transparentPixel);
                }
            }
        }
    }
    File saveFile = getTargetSaveFile(screenshotSaver, DEFAULT_SCREENSHOT_FILE_EXTENSION);
    if (saveFile != null) {
        try {
            if (screenshotOptions.isMetadataEnabled()) {
                ImageWriter writer = ImageIO.getImageWritersByFormatName("PNG").next();
                ImageOutputStream stream = ImageIO.createImageOutputStream(saveFile);
                writer.setOutput(stream);
                IIOMetadata metadata = writer.getDefaultImageMetadata(ImageTypeSpecifier.createFromRenderedImage(chatImage), writer.getDefaultWriteParam());
                IIOMetadataNode title = generateMetadataNode("Title", "CGF Screenshot");
                IIOMetadataNode software = generateMetadataNode("Software", "Chat Game Fontificator");
                final String fontGameName = ControlPanelFont.getFontGameName(fProps.getProperty(FontificatorProperties.KEY_FONT_FILE_FONT));
                final String borderGameName = ControlPanelFont.getBorderGameName(fProps.getProperty(FontificatorProperties.KEY_FONT_FILE_BORDER));
                IIOMetadataNode description = generateMetadataNode("Description", fontGameName + " Font / " + borderGameName + " Border");
                IIOMetadataNode text = new IIOMetadataNode("tEXt");
                text.appendChild(title);
                text.appendChild(software);
                text.appendChild(description);
                final String metadataFormatStr = "javax_imageio_png_1.0";
                IIOMetadataNode root = new IIOMetadataNode(metadataFormatStr);
                root.appendChild(text);
                metadata.mergeTree(metadataFormatStr, root);
                writer.write(metadata, new IIOImage(chatImage, null, metadata), writer.getDefaultWriteParam());
                stream.close();
            } else {
                ImageIO.write(chatImage, "png", saveFile);
            }
            return true;
        } catch (Exception e) {
            logger.error("Unable to save screenshot", e);
            return false;
        }
    }
    return false;
}
Also used : Color(java.awt.Color) ImageWriter(javax.imageio.ImageWriter) ChatPanel(com.glitchcog.fontificator.gui.chat.ChatPanel) BufferedImage(java.awt.image.BufferedImage) IOException(java.io.IOException) IIOImage(javax.imageio.IIOImage) Graphics(java.awt.Graphics) IIOMetadata(javax.imageio.metadata.IIOMetadata) File(java.io.File) IIOMetadataNode(javax.imageio.metadata.IIOMetadataNode) ImageOutputStream(javax.imageio.stream.ImageOutputStream)

Aggregations

Graphics (java.awt.Graphics)217 BufferedImage (java.awt.image.BufferedImage)104 Dimension (java.awt.Dimension)35 Point (java.awt.Point)32 Graphics2D (java.awt.Graphics2D)31 Color (java.awt.Color)28 JPanel (javax.swing.JPanel)22 Insets (java.awt.Insets)21 Rectangle (java.awt.Rectangle)21 JLabel (javax.swing.JLabel)19 BorderLayout (java.awt.BorderLayout)18 File (java.io.File)18 FontMetrics (java.awt.FontMetrics)16 Frame (java.awt.Frame)16 MolecularComponentPattern (org.vcell.model.rbm.MolecularComponentPattern)16 MolecularTypePattern (org.vcell.model.rbm.MolecularTypePattern)16 Component (java.awt.Component)15 Font (java.awt.Font)15 GridBagLayout (java.awt.GridBagLayout)15 JScrollPane (javax.swing.JScrollPane)15