Path: blob/master/src/demo/share/jfc/J2Ddemo/java2d/demos/Clipping/Areas.java
41175 views
/*1*2* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7*8* - Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10*11* - Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* - Neither the name of Oracle nor the names of its16* contributors may be used to endorse or promote products derived17* from this software without specific prior written permission.18*19* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS20* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,21* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR22* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR23* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,24* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,25* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR26* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF27* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING28* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS29* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.30*/31package java2d.demos.Clipping;323334import java.awt.*;35import java.awt.event.*;36import java.awt.geom.Area;37import java.awt.geom.Ellipse2D;38import java.awt.geom.GeneralPath;39import javax.swing.*;40import java2d.ControlsSurface;41import java2d.CustomControls;42import static java.awt.Color.*;434445/**46* The Areas class demonstrates the CAG (Constructive Area Geometry)47* operations: Add(union), Subtract, Intersect, and ExclusiveOR.48*/49@SuppressWarnings("serial")50public class Areas extends ControlsSurface {5152protected String areaType = "nop";5354public Areas() {55setBackground(WHITE);56setControls(new Component[] { new DemoControls(this) });57}5859@Override60public void render(int w, int h, Graphics2D g2) {61GeneralPath p1 = new GeneralPath();62p1.moveTo(w * .25f, 0.0f);63p1.lineTo(w * .75f, h * .5f);64p1.lineTo(w * .25f, h);65p1.lineTo(0.0f, h * .5f);66p1.closePath();6768GeneralPath p2 = new GeneralPath();69p2.moveTo(w * .75f, 0.0f);70p2.lineTo(w, h * .5f);71p2.lineTo(w * .75f, h);72p2.lineTo(w * .25f, h * .5f);73p2.closePath();747576Area area = new Area(p1);77g2.setColor(YELLOW);78if (areaType.equals("nop")) {79g2.fill(p1);80g2.fill(p2);81g2.setColor(RED);82g2.draw(p1);83g2.draw(p2);84return;85} else if (areaType.equals("add")) {86area.add(new Area(p2));87} else if (areaType.equals("sub")) {88area.subtract(new Area(p2));89} else if (areaType.equals("xor")) {90area.exclusiveOr(new Area(p2));91} else if (areaType.equals("int")) {92area.intersect(new Area(p2));93} else if (areaType.equals("pear")) {9495double sx = w / 100;96double sy = h / 140;97g2.scale(sx, sy);98double x = w / sx / 2;99double y = h / sy / 2;100101// Creates the first leaf by filling the intersection of two Area102// objects created from an ellipse.103Ellipse2D leaf = new Ellipse2D.Double(x - 16, y - 29, 15.0, 15.0);104Area leaf1 = new Area(leaf);105leaf.setFrame(x - 14, y - 47, 30.0, 30.0);106Area leaf2 = new Area(leaf);107leaf1.intersect(leaf2);108g2.setColor(GREEN);109g2.fill(leaf1);110111// Creates the second leaf.112leaf.setFrame(x + 1, y - 29, 15.0, 15.0);113leaf1 = new Area(leaf);114leaf2.intersect(leaf1);115g2.fill(leaf2);116117// Creates the stem by filling the Area resulting from the118// subtraction of two Area objects created from an ellipse.119Ellipse2D stem = new Ellipse2D.Double(x, y - 42, 40.0, 40.0);120Area st1 = new Area(stem);121stem.setFrame(x + 3, y - 47, 50.0, 50.0);122st1.subtract(new Area(stem));123g2.setColor(BLACK);124g2.fill(st1);125126// Creates the pear itself by filling the Area resulting from the127// union of two Area objects created by two different ellipses.128Ellipse2D circle = new Ellipse2D.Double(x - 25, y, 50.0, 50.0);129Ellipse2D oval = new Ellipse2D.Double(x - 19, y - 20, 40.0, 70.0);130Area circ = new Area(circle);131circ.add(new Area(oval));132133g2.setColor(YELLOW);134g2.fill(circ);135return;136}137138g2.fill(area);139g2.setColor(RED);140g2.draw(area);141}142143public static void main(String[] argv) {144createDemoFrame(new Areas());145}146147148static final class DemoControls extends CustomControls implements149ActionListener {150151Areas demo;152JToolBar toolbar;153154public DemoControls(Areas demo) {155super(demo.name);156this.demo = demo;157add(toolbar = new JToolBar());158toolbar.setFloatable(false);159addTool("nop", "no area operation", true);160addTool("add", "add", false);161addTool("sub", "subtract", false);162addTool("xor", "exclusiveOr", false);163addTool("int", "intersection", false);164addTool("pear", "pear", false);165}166167public void addTool(String str, String tooltip, boolean state) {168JToggleButton b =169(JToggleButton) toolbar.add(new JToggleButton(str));170b.setFocusPainted(false);171b.setToolTipText(tooltip);172b.setSelected(state);173b.addActionListener(this);174int width = b.getPreferredSize().width;175Dimension prefSize = new Dimension(width, 21);176b.setPreferredSize(prefSize);177b.setMaximumSize(prefSize);178b.setMinimumSize(prefSize);179}180181@Override182public void actionPerformed(ActionEvent e) {183for (Component comp : toolbar.getComponents()) {184((JToggleButton) comp).setSelected(false);185}186JToggleButton b = (JToggleButton) e.getSource();187b.setSelected(true);188demo.areaType = b.getText();189demo.repaint();190}191192@Override193public Dimension getPreferredSize() {194return new Dimension(200, 40);195}196197@Override198@SuppressWarnings("SleepWhileHoldingLock")199public void run() {200try {201Thread.sleep(1111);202} catch (Exception e) {203return;204}205Thread me = Thread.currentThread();206while (thread == me) {207for (Component comp : toolbar.getComponents()) {208((AbstractButton) comp).doClick();209try {210Thread.sleep(4444);211} catch (InterruptedException e) {212return;213}214}215}216thread = null;217}218} // End DemoControls219} // End Areas220221222223