Example usage for java.awt.geom Area subtract

List of usage examples for java.awt.geom Area subtract

Introduction

In this page you can find the example usage for java.awt.geom Area subtract.

Prototype

public void subtract(Area rhs) 

Source Link

Document

Subtracts the shape of the specified Area from the shape of this Area .

Usage

From source file:org.jax.haplotype.analysis.visualization.SimplePhylogenyTreeImageFactory.java

/**
 * Paint the given tree layout//w  w w  .j  a  va  2s. c o m
 * @param graphics
 *          the graphics to paint with
 * @param treeLayout
 *          the layout to paint
 */
private void paintPhylogenyTree(Graphics2D graphics, VisualTreeNode treeLayout) {
    int childNodeCount = treeLayout.getChildNodes().size();
    for (int i = 0; i < childNodeCount; i++) {
        VisualTreeNode visualChild = treeLayout.getChildNodes().get(i);

        Shape branchShape = new Line2D.Double(treeLayout.getPosition(), visualChild.getPosition());
        Shape branchShadowShape = SHADOW_TRANSFORM.createTransformedShape(branchShape);

        graphics.setColor(SHADOW_COLOR);
        graphics.draw(branchShadowShape);

        if (this.paintScale != null) {
            PhylogenyTreeEdge phylogenyEdge = treeLayout.getPhylogenyTreeNode().getChildEdges().get(i);
            if (phylogenyEdge instanceof PhylogenyTreeEdgeWithRealValue) {
                PhylogenyTreeEdgeWithRealValue phylogenyEdgeWithValue = (PhylogenyTreeEdgeWithRealValue) phylogenyEdge;
                Paint paint = this.paintScale.getPaint(phylogenyEdgeWithValue.getRealValue());
                graphics.setPaint(paint);
            } else {
                graphics.setColor(FOREGROUND_COLOR);
            }
        } else {
            graphics.setColor(FOREGROUND_COLOR);
        }
        graphics.draw(branchShape);

        // recurse
        this.paintPhylogenyTree(graphics, visualChild);
    }

    if (!treeLayout.getPhylogenyTreeNode().getStrains().isEmpty()) {
        Shape textShape = this.getLabelShape(treeLayout, graphics.getFontRenderContext());
        Shape borderShape = this.getLabelBorder(textShape);

        graphics.setColor(BACKGROUND_COLOR);
        graphics.fill(borderShape);

        graphics.setColor(SHADOW_COLOR);
        Area borderShadowShape = new Area(SHADOW_TRANSFORM.createTransformedShape(borderShape));
        borderShadowShape.subtract(new Area(borderShape));
        graphics.draw(borderShadowShape);

        graphics.setColor(FOREGROUND_COLOR);
        graphics.draw(borderShape);

        graphics.fill(textShape);
    }
}

From source file:erigo.ctstream.CTstream.java

/**
 * Pop up the GUI/*from w  w w.  j av  a2  s.  co  m*/
 * 
 * This method should be run in the event-dispatching thread.
 * 
 * The GUI is created in one of two modes depending on whether Shaped
 * windows are supported on the platform:
 * 
 * 1. If Shaped windows are supported then guiPanel (the container to
 *    which all other components are added) is RED and capturePanel is
 *    inset a small amount to this panel so that the RED border is seen
 *    around the outer edge.  A componentResized() method is defined
 *    which creates the hollowed out region that was capturePanel.
 * 2. If Shaped windows are not supported then guiPanel is transparent
 *    and capturePanel is translucent.  In this case, the user can't
 *    "reach through" capturePanel to interact with GUIs on the other
 *    side.
 * 
 * @param  bShapedWindowSupportedI  Does the underlying GraphicsDevice support the
 *                            PERPIXEL_TRANSPARENT translucency that is
 *                            required for Shaped windows?
 */
private void createAndShowGUI(boolean bShapedWindowSupportedI) {

    // No window decorations for translucent/transparent windows
    // (see note below)
    // JFrame.setDefaultLookAndFeelDecorated(true);

    //
    // Create GUI components
    //
    GridBagLayout framegbl = new GridBagLayout();
    guiFrame = new JFrame("CTstream");
    // To support a translucent window, the window must be undecorated
    // See notes in the class header up above about this; also see
    // http://alvinalexander.com/source-code/java/how-create-transparenttranslucent-java-jframe-mac-os-x
    guiFrame.setUndecorated(true);
    // Use MouseMotionListener to implement our own simple "window manager" for moving and resizing the window
    guiFrame.addMouseMotionListener(this);
    guiFrame.setBackground(new Color(0, 0, 0, 0));
    guiFrame.getContentPane().setBackground(new Color(0, 0, 0, 0));
    GridBagLayout gbl = new GridBagLayout();
    guiPanel = new JPanel(gbl);
    // if Shaped windows are supported, make guiPanel red;
    // otherwise make it transparent
    if (bShapedWindowSupportedI) {
        guiPanel.setBackground(Color.RED);
    } else {
        guiPanel.setBackground(new Color(0, 0, 0, 0));
    }
    guiFrame.setFont(new Font("Dialog", Font.PLAIN, 12));
    guiPanel.setFont(new Font("Dialog", Font.PLAIN, 12));
    GridBagLayout controlsgbl = new GridBagLayout();
    // *** controlsPanel contains the UI controls at the top of guiFrame
    controlsPanel = new JPanel(controlsgbl);
    controlsPanel.setBackground(new Color(211, 211, 211, 255));
    startStopButton = new JButton("Start");
    startStopButton.addActionListener(this);
    startStopButton.setBackground(Color.GREEN);
    continueButton = new JButton("Continue");
    continueButton.addActionListener(this);
    continueButton.setEnabled(false);
    screencapCheck = new JCheckBox("screen", bScreencap);
    screencapCheck.setBackground(controlsPanel.getBackground());
    screencapCheck.addActionListener(this);
    webcamCheck = new JCheckBox("camera", bWebcam);
    webcamCheck.setBackground(controlsPanel.getBackground());
    webcamCheck.addActionListener(this);
    audioCheck = new JCheckBox("audio", bAudio);
    audioCheck.setBackground(controlsPanel.getBackground());
    audioCheck.addActionListener(this);
    textCheck = new JCheckBox("text", bText);
    textCheck.setBackground(controlsPanel.getBackground());
    textCheck.addActionListener(this);
    JLabel fpsLabel = new JLabel("images/sec", SwingConstants.LEFT);
    fpsCB = new JComboBox<Double>(FPS_VALUES);
    int tempIndex = Arrays.asList(FPS_VALUES).indexOf(new Double(framesPerSec));
    fpsCB.setSelectedIndex(tempIndex);
    fpsCB.addActionListener(this);
    // The popup doesn't display over the transparent region;
    // therefore, just display a few rows to keep it within controlsPanel
    fpsCB.setMaximumRowCount(3);
    JLabel imgQualLabel = new JLabel("image qual", SwingConstants.LEFT);
    // The slider will use range 0 - 1000
    imgQualSlider = new JSlider(JSlider.HORIZONTAL, 0, 1000, (int) (imageQuality * 1000.0));
    // NOTE: The JSlider's initial width was too large, so I'd like to set its preferred size
    //       to try and constrain it some; also need to set its minimum size at the same time,
    //       or else when the user makes the GUI frame smaller, the JSlider would pop down to
    //       a really small minimum size.
    imgQualSlider.setPreferredSize(new Dimension(120, 30));
    imgQualSlider.setMinimumSize(new Dimension(120, 30));
    imgQualSlider.setBackground(controlsPanel.getBackground());
    includeMouseCursorCheck = new JCheckBox("Include mouse cursor in screen capture", bIncludeMouseCursor);
    includeMouseCursorCheck.setBackground(controlsPanel.getBackground());
    includeMouseCursorCheck.addActionListener(this);
    changeDetectCheck = new JCheckBox("Change detect", bChangeDetect);
    changeDetectCheck.setBackground(controlsPanel.getBackground());
    changeDetectCheck.addActionListener(this);
    fullScreenCheck = new JCheckBox("Full Screen", bFullScreen);
    fullScreenCheck.setBackground(controlsPanel.getBackground());
    fullScreenCheck.addActionListener(this);
    previewCheck = new JCheckBox("Preview", bPreview);
    previewCheck.setBackground(controlsPanel.getBackground());
    previewCheck.addActionListener(this);
    // Specify a small size for the text area, so that we can shrink down the UI w/o the scrollbars popping up
    textArea = new JTextArea(3, 10);
    // Add a Document listener to the JTextArea; this is how we will listen for changes to the document
    docChangeListener = new DocumentChangeListener(this);
    textArea.getDocument().addDocumentListener(docChangeListener);
    textScrollPane = new JScrollPane(textArea);
    // *** capturePanel
    capturePanel = new JPanel();
    if (!bShapedWindowSupportedI) {
        // Only make capturePanel translucent (ie, semi-transparent) if we aren't doing the Shaped window option
        capturePanel.setBackground(new Color(0, 0, 0, 16));
    } else {
        capturePanel.setBackground(new Color(0, 0, 0, 0));
    }
    capturePanel.setPreferredSize(new Dimension(500, 400));
    boolean bMacOS = false;
    String OS = System.getProperty("os.name", "generic").toLowerCase();
    if ((OS.indexOf("mac") >= 0) || (OS.indexOf("darwin") >= 0)) {
        bMacOS = true;
    }
    // Only have the CTstream UI stay on top of all other windows
    // if bStayOnTop is true (set by command line flag).  This is needed
    // for the Mac, because on that platform the user can't "reach through"
    // the capture frame to windows behind it.
    if (bStayOnTop) {
        guiFrame.setAlwaysOnTop(true);
    }

    //
    // Add components to the GUI
    //

    int row = 0;

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.anchor = GridBagConstraints.WEST;
    gbc.fill = GridBagConstraints.NONE;
    gbc.weightx = 0;
    gbc.weighty = 0;

    //
    // First row: the controls panel
    //
    //  Add some extra horizontal padding around controlsPanel so the panel has some extra room
    // if we are running in web camera mode.
    gbc.insets = new Insets(0, 0, 0, 0);
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.weightx = 100;
    gbc.weighty = 0;
    Utility.add(guiPanel, controlsPanel, gbl, gbc, 0, row, 1, 1);
    gbc.insets = new Insets(0, 0, 0, 0);
    gbc.fill = GridBagConstraints.NONE;
    gbc.weightx = 0;
    gbc.weighty = 0;
    ++row;

    // Add controls to the controls panel
    int panelrow = 0;
    // (i) Start/Continue buttons
    GridBagLayout panelgbl = new GridBagLayout();
    JPanel subPanel = new JPanel(panelgbl);
    GridBagConstraints panelgbc = new GridBagConstraints();
    panelgbc.anchor = GridBagConstraints.WEST;
    panelgbc.fill = GridBagConstraints.NONE;
    panelgbc.weightx = 0;
    panelgbc.weighty = 0;
    subPanel.setBackground(controlsPanel.getBackground());
    panelgbc.insets = new Insets(0, 0, 0, 5);
    Utility.add(subPanel, startStopButton, panelgbl, panelgbc, 0, 0, 1, 1);
    panelgbc.insets = new Insets(0, 0, 0, 0);
    Utility.add(subPanel, continueButton, panelgbl, panelgbc, 1, 0, 1, 1);
    gbc.anchor = GridBagConstraints.CENTER;
    gbc.fill = GridBagConstraints.NONE;
    gbc.insets = new Insets(5, 0, 0, 0);
    Utility.add(controlsPanel, subPanel, controlsgbl, gbc, 0, panelrow, 1, 1);
    ++panelrow;
    gbc.anchor = GridBagConstraints.WEST;
    gbc.fill = GridBagConstraints.NONE;
    // (ii) select DataStreams to turn on
    panelgbl = new GridBagLayout();
    subPanel = new JPanel(panelgbl);
    panelgbc = new GridBagConstraints();
    panelgbc.anchor = GridBagConstraints.WEST;
    panelgbc.fill = GridBagConstraints.NONE;
    panelgbc.weightx = 0;
    panelgbc.weighty = 0;
    subPanel.setBackground(controlsPanel.getBackground());
    panelgbc.insets = new Insets(0, 0, 0, 0);
    Utility.add(subPanel, screencapCheck, panelgbl, panelgbc, 0, 0, 1, 1);
    Utility.add(subPanel, webcamCheck, panelgbl, panelgbc, 1, 0, 1, 1);
    Utility.add(subPanel, audioCheck, panelgbl, panelgbc, 2, 0, 1, 1);
    Utility.add(subPanel, textCheck, panelgbl, panelgbc, 3, 0, 1, 1);
    gbc.anchor = GridBagConstraints.CENTER;
    gbc.fill = GridBagConstraints.NONE;
    gbc.insets = new Insets(0, 0, 0, 0);
    Utility.add(controlsPanel, subPanel, controlsgbl, gbc, 0, panelrow, 1, 1);
    ++panelrow;
    // (iii) images/sec control
    panelgbl = new GridBagLayout();
    subPanel = new JPanel(panelgbl);
    panelgbc = new GridBagConstraints();
    panelgbc.anchor = GridBagConstraints.WEST;
    panelgbc.fill = GridBagConstraints.NONE;
    panelgbc.weightx = 0;
    panelgbc.weighty = 0;
    subPanel.setBackground(controlsPanel.getBackground());
    panelgbc.insets = new Insets(2, 0, 0, 0);
    Utility.add(subPanel, fpsLabel, panelgbl, panelgbc, 0, 0, 1, 1);
    panelgbc.insets = new Insets(2, 10, 0, 10);
    Utility.add(subPanel, fpsCB, panelgbl, panelgbc, 1, 0, 1, 1);
    gbc.insets = new Insets(0, 0, 0, 0);
    gbc.anchor = GridBagConstraints.CENTER;
    Utility.add(controlsPanel, subPanel, controlsgbl, gbc, 0, panelrow, 1, 1);
    ++panelrow;
    // (iv) image quality slider
    panelgbl = new GridBagLayout();
    subPanel = new JPanel(panelgbl);
    panelgbc = new GridBagConstraints();
    panelgbc.anchor = GridBagConstraints.WEST;
    panelgbc.fill = GridBagConstraints.NONE;
    panelgbc.weightx = 0;
    panelgbc.weighty = 0;
    subPanel.setBackground(controlsPanel.getBackground());
    JLabel sliderLabelLow = new JLabel("Low", SwingConstants.LEFT);
    JLabel sliderLabelHigh = new JLabel("High", SwingConstants.LEFT);
    panelgbc.insets = new Insets(-5, 0, 0, 0);
    Utility.add(subPanel, imgQualLabel, panelgbl, panelgbc, 0, 0, 1, 1);
    panelgbc.insets = new Insets(-5, 5, 0, 5);
    Utility.add(subPanel, sliderLabelLow, panelgbl, panelgbc, 1, 0, 1, 1);
    panelgbc.insets = new Insets(0, 0, 0, 0);
    Utility.add(subPanel, imgQualSlider, panelgbl, panelgbc, 2, 0, 1, 1);
    panelgbc.insets = new Insets(-5, 5, 0, 0);
    Utility.add(subPanel, sliderLabelHigh, panelgbl, panelgbc, 3, 0, 1, 1);
    gbc.insets = new Insets(0, 0, 0, 0);
    gbc.anchor = GridBagConstraints.CENTER;
    Utility.add(controlsPanel, subPanel, controlsgbl, gbc, 0, panelrow, 1, 1);
    ++panelrow;
    // (v) Include mouse cursor in screen capture image?
    gbc.anchor = GridBagConstraints.CENTER;
    gbc.fill = GridBagConstraints.NONE;
    gbc.weightx = 0;
    gbc.weighty = 0;
    gbc.insets = new Insets(0, 15, 5, 15);
    Utility.add(controlsPanel, includeMouseCursorCheck, controlsgbl, gbc, 0, panelrow, 1, 1);
    gbc.fill = GridBagConstraints.NONE;
    gbc.weightx = 0;
    gbc.weighty = 0;
    ++panelrow;
    // (vi) Change detect / Full screen / Preview checkboxes
    panelgbl = new GridBagLayout();
    subPanel = new JPanel(panelgbl);
    panelgbc = new GridBagConstraints();
    panelgbc.anchor = GridBagConstraints.WEST;
    panelgbc.fill = GridBagConstraints.NONE;
    panelgbc.weightx = 0;
    panelgbc.weighty = 0;
    subPanel.setBackground(controlsPanel.getBackground());
    panelgbc.insets = new Insets(0, 0, 0, 0);
    Utility.add(subPanel, changeDetectCheck, panelgbl, panelgbc, 0, 0, 1, 1);
    Utility.add(subPanel, fullScreenCheck, panelgbl, panelgbc, 1, 0, 1, 1);
    Utility.add(subPanel, previewCheck, panelgbl, panelgbc, 2, 0, 1, 1);
    gbc.anchor = GridBagConstraints.CENTER;
    gbc.fill = GridBagConstraints.NONE;
    gbc.insets = new Insets(-5, 0, 3, 0);
    Utility.add(controlsPanel, subPanel, controlsgbl, gbc, 0, panelrow, 1, 1);
    ++panelrow;
    // (vii) text field for the TextStream
    /*
    panelgbl = new GridBagLayout();
    subPanel = new JPanel(panelgbl);
    panelgbc = new GridBagConstraints();
    panelgbc.anchor = GridBagConstraints.CENTER;
    panelgbc.fill = GridBagConstraints.HORIZONTAL;
    panelgbc.weightx = 100;
    panelgbc.weighty = 100;
    subPanel.setBackground(controlsPanel.getBackground());
    panelgbc.insets = new Insets(0, 0, 0, 0);
    Utility.add(subPanel, textScrollPane, panelgbl, panelgbc, 1, 0, 1, 1);
    gbc.anchor = GridBagConstraints.CENTER;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.weightx = 100;
    gbc.weighty = 100;
    gbc.insets = new Insets(0, 15, 3, 15);
    Utility.add(controlsPanel, subPanel, controlsgbl, gbc, 0, panelrow, 2, 1);
    ++panelrow;
    */
    gbc.anchor = GridBagConstraints.CENTER;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.weightx = 100;
    gbc.weighty = 0;
    gbc.insets = new Insets(0, 15, 5, 15);
    Utility.add(controlsPanel, textScrollPane, controlsgbl, gbc, 0, panelrow, 1, 1);
    gbc.fill = GridBagConstraints.NONE;
    gbc.weightx = 0;
    gbc.weighty = 0;
    ++panelrow;

    //
    // Second row: the translucent/transparent capture panel
    //
    if (bShapedWindowSupportedI) {
        // Doing the Shaped window; set capturePanel inside guiPanel
        // a bit so the red from guiPanel shows at the edges
        gbc.insets = new Insets(5, 5, 5, 5);
    } else {
        // No shaped window; have capturePanel fill the area
        gbc.insets = new Insets(0, 0, 0, 0);
    }
    gbc.fill = GridBagConstraints.BOTH;
    gbc.weightx = 100;
    gbc.weighty = 100;
    Utility.add(guiPanel, capturePanel, gbl, gbc, 0, row, 1, 1);
    gbc.fill = GridBagConstraints.NONE;
    gbc.weightx = 0;
    gbc.weighty = 0;
    ++row;

    //
    // Add guiPanel to guiFrame
    //
    gbc.anchor = GridBagConstraints.CENTER;
    gbc.fill = GridBagConstraints.BOTH;
    gbc.weightx = 100;
    gbc.weighty = 100;
    gbc.insets = new Insets(0, 0, 0, 0);
    Utility.add(guiFrame, guiPanel, framegbl, gbc, 0, 0, 1, 1);

    //
    // Add menu
    //
    JMenuBar menuBar = createMenu();
    guiFrame.setJMenuBar(menuBar);

    //
    // If Shaped windows are supported, the region defined by capturePanel
    // will be "hollowed out" so that the user can reach through guiFrame
    // and interact with applications which are behind it.
    //
    // NOTE: This doesn't work on Mac OS (we've tried, but nothing seems
    //       to work to allow a user to reach through guiFrame to interact
    //       with windows behind).  May be a limitation or bug on Mac OS:
    //       https://bugs.openjdk.java.net/browse/JDK-8013450
    //
    if (bShapedWindowSupportedI) {
        guiFrame.addComponentListener(new ComponentAdapter() {
            // As the window is resized, the shape is recalculated here.
            @Override
            public void componentResized(ComponentEvent e) {
                // Create a rectangle to cover the entire guiFrame
                Area guiShape = new Area(new Rectangle(0, 0, guiFrame.getWidth(), guiFrame.getHeight()));
                // Create another rectangle to define the hollowed out region of capturePanel
                guiShape.subtract(new Area(new Rectangle(capturePanel.getX(), capturePanel.getY() + 23,
                        capturePanel.getWidth(), capturePanel.getHeight())));
                guiFrame.setShape(guiShape);
            }
        });
    }

    //
    // Final guiFrame configuration details and displaying the GUI
    //
    guiFrame.pack();

    guiFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

    guiFrame.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            exit(false);
        }
    });

    // Center on the screen
    guiFrame.setLocationRelativeTo(null);

    //
    // Set the taskbar/dock icon; note that Mac OS has its own way of doing it
    //
    if (bMacOS) {
        try {
            // JPW 2018/02/02: changed how to load images to work under Java 9
            InputStream imageInputStreamLarge = getClass().getClassLoader()
                    .getResourceAsStream("Icon_128x128.png");
            BufferedImage bufferedImageLarge = ImageIO.read(imageInputStreamLarge);
            /**
             *
             * Java 9 note: running the following code under Java 9 on a Mac will produce the following warning:
             *
             * WARNING: An illegal reflective access operation has occurred
             * WARNING: Illegal reflective access by erigo.ctstream.CTstream (file:/Users/johnwilson/CT_versions/compiled_under_V8/CTstream.jar) to method com.apple.eawt.Application.getApplication()
             * WARNING: Please consider reporting this to the maintainers of erigo.ctstream.CTstream
             * WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
             * WARNING: All illegal access operations will be denied in a future release
             *
             * This is because Java 9 has taken a step away from using reflection; see see the section titled
             * "Illegal Access To Internal APIs" at https://blog.codefx.org/java/java-9-migration-guide/.
             *
             * A good fix (but only available in Java 9+) is to use the following:
             *
             *     java.awt.Taskbar taskbar = java.awt.Taskbar.getTaskbar();
             *     taskbar.setIconImage(bufferedImageLarge);
             *
             * Could use reflection to make calls in class com.apple.eawt.Application; for example, see
             * Bertil Chapuis' "dockicon.java" example code at https://gist.github.com/bchapuis/1562406
             *
             * For now, we just won't do dock icons under Mac OS.
             *
             **/
        } catch (Exception excepI) {
            System.err.println("Exception thrown trying to set icon: " + excepI);
        }
    } else {
        // The following has been tested under Windows 10 and Ubuntu 12.04 LTS
        try {
            // JPW 2018/02/02: changed how to load images to work under Java 9
            InputStream imageInputStreamLarge = getClass().getClassLoader()
                    .getResourceAsStream("Icon_128x128.png");
            BufferedImage bufferedImageLarge = ImageIO.read(imageInputStreamLarge);
            InputStream imageInputStreamMed = getClass().getClassLoader().getResourceAsStream("Icon_64x64.png");
            BufferedImage bufferedImageMed = ImageIO.read(imageInputStreamMed);
            InputStream imageInputStreamSmall = getClass().getClassLoader()
                    .getResourceAsStream("Icon_32x32.png");
            BufferedImage bufferedImageSmall = ImageIO.read(imageInputStreamSmall);
            List<BufferedImage> iconList = new ArrayList<BufferedImage>();
            iconList.add(bufferedImageLarge);
            iconList.add(bufferedImageMed);
            iconList.add(bufferedImageSmall);
            guiFrame.setIconImages(iconList);
        } catch (Exception excepI) {
            System.err.println("Exception thrown trying to set icon: " + excepI);
        }
    }

    ctSettings = new CTsettings(this, guiFrame);

    guiFrame.setVisible(true);

}

From source file:net.technicpack.launcher.lang.ResourceLoader.java

public BufferedImage getCircleClippedImage(String imageName) {
    BufferedImage contentImage = getImage(imageName);

    // copy the picture to an image with transparency capabilities
    BufferedImage outputImage = new BufferedImage(contentImage.getWidth(), contentImage.getHeight(),
            BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = (Graphics2D) outputImage.getGraphics();
    g2.drawImage(contentImage, 0, 0, null);

    // Create the area around the circle to cut out
    Area cutOutArea = new Area(new Rectangle(0, 0, outputImage.getWidth(), outputImage.getHeight()));

    int diameter = (outputImage.getWidth() < outputImage.getHeight()) ? outputImage.getWidth()
            : outputImage.getHeight();//from   w w  w.  jav  a 2  s  .co  m
    cutOutArea.subtract(new Area(new Ellipse2D.Float((outputImage.getWidth() - diameter) / 2,
            (outputImage.getHeight() - diameter) / 2, diameter, diameter)));

    // Set the fill color to an opaque color
    g2.setColor(Color.WHITE);
    // Set the composite to clear pixels
    g2.setComposite(AlphaComposite.Clear);
    // Turn on antialiasing
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    // Clear the cut out area
    g2.fill(cutOutArea);

    // dispose of the graphics object
    g2.dispose();

    return outputImage;
}

From source file:net.technicpack.ui.lang.ResourceLoader.java

public BufferedImage getCircleClippedImage(BufferedImage contentImage) {
    // copy the picture to an image with transparency capabilities
    BufferedImage outputImage = new BufferedImage(contentImage.getWidth(), contentImage.getHeight(),
            BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = (Graphics2D) outputImage.getGraphics();
    g2.drawImage(contentImage, 0, 0, null);

    // Create the area around the circle to cut out
    Area cutOutArea = new Area(new Rectangle(0, 0, outputImage.getWidth(), outputImage.getHeight()));

    int diameter = (outputImage.getWidth() < outputImage.getHeight()) ? outputImage.getWidth()
            : outputImage.getHeight();//from   w ww  .jav a  2 s. c o m
    cutOutArea.subtract(new Area(new Ellipse2D.Float((outputImage.getWidth() - diameter) / 2,
            (outputImage.getHeight() - diameter) / 2, diameter, diameter)));

    // Set the fill color to an opaque color
    g2.setColor(Color.WHITE);
    // Set the composite to clear pixels
    g2.setComposite(AlphaComposite.Clear);
    // Turn on antialiasing
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    // Clear the cut out area
    g2.fill(cutOutArea);

    // dispose of the graphics object
    g2.dispose();

    return outputImage;
}

From source file:savant.view.swing.GraphPane.java

/**
 * Render the background of this GraphPane
 *
 * @param g The graphics object to use//www  .j  a  va  2s. co  m
 */
private void renderBackground(Graphics2D g2, boolean xGridOn, boolean yGridOn) {
    int h = getHeight();
    int w = getWidth();

    // Paint a gradient from top to bottom
    GradientPaint gp0 = new GradientPaint(0, 0, ColourSettings.getColor(ColourKey.GRAPH_PANE_BACKGROUND_TOP), 0,
            h, ColourSettings.getColor(ColourKey.GRAPH_PANE_BACKGROUND_BOTTOM));
    g2.setPaint(gp0);
    g2.fillRect(0, 0, w, h);

    // We don't want the axes stomping on our labels, so make sure the clip excludes them.
    Area clipArea = new Area(new Rectangle(0, 0, w, h));
    Color gridColor = ColourSettings.getColor(ColourKey.AXIS_GRID);

    if (yGridOn) {
        // Smallish font for tick labels.
        Font tickFont = g2.getFont().deriveFont(Font.PLAIN, 9);

        int[] yTicks = MiscUtils.getTickPositions(transformYPixel(getHeight()), transformYPixel(0.0));

        g2.setColor(gridColor);
        g2.setFont(tickFont);
        for (int t : yTicks) {
            double y = transformYPos(t);

            // Skip labels at the top or bottom of the window because they look stupid.
            if (y != 0.0 && y != getHeight()) {
                String s = Integer.toString(t);
                Rectangle2D labelRect = tickFont.getStringBounds(s, g2.getFontRenderContext());
                double baseline = y + labelRect.getHeight() * 0.5 - 2.0;
                g2.drawString(s, 4.0F, (float) baseline);
                clipArea.subtract(new Area(new Rectangle2D.Double(3.0, baseline - labelRect.getHeight() - 1.0,
                        labelRect.getWidth() + 2.0, labelRect.getHeight() + 2.0)));
            }
        }
        g2.setClip(clipArea);
        for (int t2 : yTicks) {
            double y = transformYPos(t2);
            g2.draw(new Line2D.Double(0.0, y, w, y));
        }
    }
    if (xGridOn) {
        Range r = LocationController.getInstance().getRange();
        int[] xTicks = MiscUtils.getTickPositions(r);

        g2.setColor(gridColor);
        for (int t : xTicks) {
            double x = transformXPos(t);
            g2.draw(new Line2D.Double(x, 0, x, h));
        }
    }
    g2.setClip(null);
}

From source file:VASSAL.build.module.map.MapShader.java

protected void checkPiece(Area area, GamePiece piece) {
    if (piece instanceof Stack) {
        Stack s = (Stack) piece;
        for (int i = 0; i < s.getPieceCount(); i++) {
            checkPiece(area, s.getPieceAt(i));
        }//from w ww.j  a  v a 2s.c  om
    } else {
        ShadedPiece shaded = (ShadedPiece) Decorator.getDecorator(piece, ShadedPiece.class);
        if (shaded != null) {
            Area shape = shaded.getArea(this);
            if (shape != null) {
                if (type.equals(FG_TYPE)) {
                    area.add(shape);
                } else {
                    area.subtract(shape);
                }
            }
        }
    }
}