Path: blob/master/src/demo/share/jfc/J2Ddemo/java2d/demos/Clipping/Intersection.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*/313233package java2d.demos.Clipping;343536import java.awt.*;37import java.awt.event.*;38import java.awt.geom.*;39import java.awt.font.FontRenderContext;40import java.awt.font.TextLayout;41import javax.swing.*;42import java2d.AnimatingControlsSurface;43import java2d.CustomControls;44import static java.awt.Color.*;454647/**48* Animated intersection clipping of lines, an image and a textured rectangle.49*/50@SuppressWarnings("serial")51public class Intersection extends AnimatingControlsSurface {5253private static final int HEIGHT_DECREASE = 0;54private static final int HEIGHT_INCREASE = 1;55private static final int WIDTH_DECREASE = 2;56private static final int WIDTH_INCREASE = 3;5758private int xx, yy, ww, hh;59private int direction = HEIGHT_DECREASE;60private int angdeg;61private Shape textshape;62private double sw, sh;63private GeneralPath ovals;64private Rectangle2D rectshape;65protected boolean doIntersection = true;66protected boolean doOvals = true;67protected boolean doText;68protected boolean threeSixty;697071public Intersection() {72setBackground(WHITE);73setControls(new Component[] { new DemoControls(this) });74}757677@Override78public void reset(int w, int h) {79xx = yy = 0;80ww = w-1; hh = h;81direction = HEIGHT_DECREASE;82angdeg = 0;83FontRenderContext frc = new FontRenderContext(null, true, false);84Font f = new Font(Font.SERIF, Font.BOLD,32);85TextLayout tl = new TextLayout("J2D", f, frc);86sw = tl.getBounds().getWidth();87sh = tl.getBounds().getHeight();88int size = Math.min(w, h);89double sx = (size-40)/sw;90double sy = (size-100)/sh;91AffineTransform Tx = AffineTransform.getScaleInstance(sx, sy);92textshape = tl.getOutline(Tx);93rectshape = textshape.getBounds();94sw = rectshape.getWidth();95sh = rectshape.getHeight();96ovals = new GeneralPath();97ovals.append(new Ellipse2D.Double( 10, 10, 20, 20), false);98ovals.append(new Ellipse2D.Double(w-30, 10, 20, 20), false);99ovals.append(new Ellipse2D.Double( 10, h-30, 20, 20), false);100ovals.append(new Ellipse2D.Double(w-30, h-30, 20, 20), false);101}102103104@Override105public void step(int w, int h) {106if (direction == HEIGHT_DECREASE) {107yy+=2; hh-=4;108if (yy >= h/2) {109direction = HEIGHT_INCREASE;110}111} else if (direction == HEIGHT_INCREASE) {112yy-=2; hh+=4;113if (yy <= 0) {114direction = WIDTH_DECREASE;115hh = h-1; yy = 0;116}117}118if (direction == WIDTH_DECREASE) {119xx+=2; ww-=4;120if (xx >= w/2) {121direction = WIDTH_INCREASE;122}123} else if (direction == WIDTH_INCREASE) {124xx-=2; ww+=4;125if (xx <= 0) {126direction = HEIGHT_DECREASE;127ww = w-1; xx = 0;128}129}130if ((angdeg += 5) == 360) {131angdeg = 0;132threeSixty = true;133}134}135136137@Override138public void render(int w, int h, Graphics2D g2) {139140Rectangle rect = new Rectangle(xx, yy, ww, hh);141142AffineTransform Tx = new AffineTransform();143Tx.rotate(Math.toRadians(angdeg),w/2,h/2);144Tx.translate(w/2-sw/2, sh+(h-sh)/2);145146GeneralPath path = new GeneralPath();147if (doOvals) {148path.append(ovals, false);149}150if (doText) {151path.append(Tx.createTransformedShape(textshape), false);152} else {153path.append(Tx.createTransformedShape(rectshape), false);154}155156if (doIntersection) {157g2.clip(rect);158g2.clip(path);159}160161g2.setColor(GREEN);162g2.fill(rect);163164g2.setClip(new Rectangle(0, 0, w, h));165166g2.setColor(LIGHT_GRAY);167g2.draw(rect);168g2.setColor(BLACK);169g2.draw(path);170}171172173public static void main(String[] argv) {174createDemoFrame(new Intersection());175}176177178static final class DemoControls extends CustomControls implements ActionListener {179180Intersection demo;181JToolBar toolbar;182183public DemoControls(Intersection demo) {184super(demo.name);185this.demo = demo;186add(toolbar = new JToolBar());187toolbar.setFloatable(false);188addTool("Intersect", true );189addTool("Text", false);190addTool("Ovals", true );191}192193194public void addTool(String str, boolean state) {195JToggleButton b = (JToggleButton) toolbar.add(new JToggleButton(str));196b.setFocusPainted(false);197b.setSelected(state);198b.addActionListener(this);199int width = b.getPreferredSize().width;200Dimension prefSize = new Dimension(width, 21);201b.setPreferredSize(prefSize);202b.setMaximumSize( prefSize);203b.setMinimumSize( prefSize);204}205206207@Override208public void actionPerformed(ActionEvent e) {209JToggleButton b = (JToggleButton) e.getSource();210if (b.getText().equals("Intersect")) {211demo.doIntersection = b.isSelected();212} else if (b.getText().equals("Ovals")) {213demo.doOvals = b.isSelected();214} else if (b.getText().equals("Text")) {215demo.doText = b.isSelected();216}217if (!demo.animating.running()) {218demo.repaint();219}220}221222@Override223public Dimension getPreferredSize() {224return new Dimension(200,40);225}226227228@Override229@SuppressWarnings("SleepWhileHoldingLock")230public void run() {231Thread me = Thread.currentThread();232while (thread == me) {233if (demo.threeSixty) {234((AbstractButton) toolbar.getComponentAtIndex(1)).doClick();235demo.threeSixty = false;236}237try {238Thread.sleep(500);239} catch (InterruptedException e) { return; }240}241thread = null;242}243} // End DemoControls244} // End Intersection245246247