Path: blob/master/src/demo/share/jfc/J2Ddemo/java2d/demos/Clipping/ClipAnim.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.*;37import java.awt.image.BufferedImage;38import javax.swing.*;39import java2d.AnimatingControlsSurface;40import java2d.CustomControls;41import static java.lang.Math.random;42import static java.awt.Color.*;434445/**46* Animated clipping of an image & composited shapes.47*/48@SuppressWarnings("serial")49public class ClipAnim extends AnimatingControlsSurface {5051private static Image dimg, cimg;52static TexturePaint texturePaint;5354static {55BufferedImage bi = new BufferedImage(5, 5, BufferedImage.TYPE_INT_RGB);56Graphics2D big = bi.createGraphics();57big.setBackground(YELLOW);58big.clearRect(0, 0, 5, 5);59big.setColor(RED);60big.fillRect(0, 0, 3, 3);61texturePaint = new TexturePaint(bi, new Rectangle(0, 0, 5, 5));62}63private AnimVal[] animval = new AnimVal[3];64protected boolean doObjects = true;65private Font originalFont = new Font(Font.SERIF, Font.PLAIN, 12);66private Font font;67private GradientPaint gradient;68private int strX, strY;69private int dukeX, dukeY, dukeWidth, dukeHeight;7071public ClipAnim() {72cimg = getImage("clouds.jpg");73dimg = getImage("duke.png");74setBackground(WHITE);75animval[0] = new AnimVal(true);76animval[1] = new AnimVal(false);77animval[2] = new AnimVal(false);78setControls(new Component[] { new DemoControls(this) });79}8081@Override82public void reset(int w, int h) {83for (AnimVal a : animval) {84a.reset(w, h);85}86gradient = new GradientPaint(0, h / 2, RED, w * .4f, h * .9f, YELLOW);87double scale = 0.4;88dukeHeight = (int) (scale * h);89dukeWidth = (int) (dimg.getWidth(this) * scale * h / dimg.getHeight(this));90dukeX = (int) (w * .25 - dukeWidth / 2);91dukeY = (int) (h * .25 - dukeHeight / 2);92FontMetrics fm = getFontMetrics(originalFont);93double sw = fm.stringWidth("CLIPPING");94double sh = fm.getAscent() + fm.getDescent();95double sx = (w / 2 - 30) / sw;96double sy = (h / 2 - 30) / sh;97AffineTransform Tx = AffineTransform.getScaleInstance(sx, sy);98font = originalFont.deriveFont(Tx);99fm = getFontMetrics(font);100strX = (int) (w * .75 - fm.stringWidth("CLIPPING") / 2);101strY = (int) (h * .72 + fm.getAscent() / 2);102}103104@Override105public void step(int w, int h) {106for (AnimVal a : animval) {107if (a.isSelected) {108a.step(w, h);109}110}111}112113@Override114public void render(int w, int h, Graphics2D g2) {115116GeneralPath p1 = new GeneralPath();117GeneralPath p2 = new GeneralPath();118119for (AnimVal a : animval) {120if (a.isSelected) {121double x = a.x;122double y = a.y;123double ew = a.ew;124double eh = a.eh;125p1.append(new Ellipse2D.Double(x, y, ew, eh), false);126p2.append(new Rectangle2D.Double(x + 5, y + 5, ew - 10, eh - 10),127false);128}129}130if (animval[0].isSelected || animval[1].isSelected131|| animval[2].isSelected) {132g2.setClip(p1);133g2.clip(p2);134}135136if (doObjects) {137int w2 = w / 2;138int h2 = h / 2;139g2.drawImage(cimg, 0, 0, w2, h2, null);140g2.drawImage(dimg, dukeX, dukeY, dukeWidth, dukeHeight, null);141142g2.setPaint(texturePaint);143g2.fillRect(w2, 0, w2, h2);144145g2.setPaint(gradient);146g2.fillRect(0, h2, w2, h2);147148g2.setColor(LIGHT_GRAY);149g2.fillRect(w2, h2, w2, h2);150g2.setColor(RED);151g2.drawOval(w2, h2, w2 - 1, h2 - 1);152g2.setFont(font);153g2.drawString("CLIPPING", strX, strY);154} else {155g2.setColor(LIGHT_GRAY);156g2.fillRect(0, 0, w, h);157}158}159160public static void main(String[] argv) {161createDemoFrame(new ClipAnim());162}163164165public class AnimVal {166167double ix = 5.0;168double iy = 3.0;169double iw = 5.0;170double ih = 3.0;171double x, y;172double ew, eh; // ellipse width & height173boolean isSelected;174175public AnimVal(boolean isSelected) {176this.isSelected = isSelected;177}178179public void step(int w, int h) {180x += ix;181y += iy;182ew += iw;183eh += ih;184185if (ew > w / 2) {186ew = w / 2;187iw = random() * -w / 16 - 1;188}189if (ew < w / 8) {190ew = w / 8;191iw = random() * w / 16 + 1;192}193if (eh > h / 2) {194eh = h / 2;195ih = random() * -h / 16 - 1;196}197if (eh < h / 8) {198eh = h / 8;199ih = random() * h / 16 + 1;200}201202if ((x + ew) > w) {203x = (w - ew) - 1;204ix = random() * -w / 32 - 1;205}206if ((y + eh) > h) {207y = (h - eh) - 2;208iy = random() * -h / 32 - 1;209}210if (x < 0) {211x = 2;212ix = random() * w / 32 + 1;213}214if (y < 0) {215y = 2;216iy = random() * h / 32 + 1;217}218}219220public void reset(int w, int h) {221x = random() * w;222y = random() * h;223ew = (random() * w) / 2;224eh = (random() * h) / 2;225}226}227228229static final class DemoControls extends CustomControls implements230ActionListener {231232ClipAnim demo;233JToolBar toolbar;234235public DemoControls(ClipAnim demo) {236super(demo.name);237this.demo = demo;238add(toolbar = new JToolBar());239toolbar.setFloatable(false);240addTool("Objects", true);241addTool("Clip1", true);242addTool("Clip2", false);243addTool("Clip3", false);244}245246public void addTool(String str, boolean state) {247JToggleButton b =248(JToggleButton) toolbar.add(new JToggleButton(str));249b.setFocusPainted(false);250b.setSelected(state);251b.addActionListener(this);252int width = b.getPreferredSize().width;253Dimension prefSize = new Dimension(width, 21);254b.setPreferredSize(prefSize);255b.setMaximumSize(prefSize);256b.setMinimumSize(prefSize);257}258259@Override260public void actionPerformed(ActionEvent e) {261JToggleButton b = (JToggleButton) e.getSource();262if (b.getText().equals("Objects")) {263demo.doObjects = b.isSelected();264} else if (b.getText().equals("Clip1")) {265demo.animval[0].isSelected = b.isSelected();266} else if (b.getText().equals("Clip2")) {267demo.animval[1].isSelected = b.isSelected();268} else if (b.getText().equals("Clip3")) {269demo.animval[2].isSelected = b.isSelected();270}271if (!demo.animating.running()) {272demo.repaint();273}274}275276@Override277public Dimension getPreferredSize() {278return new Dimension(200, 40);279}280281@Override282public void run() {283try {284Thread.sleep(5000);285} catch (InterruptedException e) {286return;287}288((AbstractButton) toolbar.getComponentAtIndex(2)).doClick();289try {290Thread.sleep(5000);291} catch (InterruptedException e) {292return;293}294if (getSize().width > 400) {295((AbstractButton) toolbar.getComponentAtIndex(3)).doClick();296}297thread = null;298}299} // End DemoControls300} // End ClipAnim301302303304