Path: blob/master/src/demo/share/jfc/J2Ddemo/java2d/demos/Paint/TextureAnim.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.Paint;323334import static java.awt.Color.BLACK;35import static java.awt.Color.GRAY;36import static java.awt.Color.LIGHT_GRAY;37import static java.awt.Color.WHITE;38import java.awt.Color;39import java.awt.Component;40import java.awt.Dimension;41import java.awt.Graphics;42import java.awt.Graphics2D;43import java.awt.Image;44import java.awt.Rectangle;45import java.awt.TexturePaint;46import java.awt.event.ActionEvent;47import java.awt.event.ActionListener;48import java.awt.image.BufferedImage;49import java2d.AnimatingControlsSurface;50import java2d.CustomControls;51import javax.swing.AbstractButton;52import javax.swing.Icon;53import javax.swing.JComboBox;54import javax.swing.JMenu;55import javax.swing.JMenuBar;56import javax.swing.JMenuItem;57import javax.swing.JToggleButton;58import javax.swing.JToolBar;59import javax.swing.plaf.metal.MetalBorders.ButtonBorder;606162/**63* TexturePaint animation with controls for transformations.64*/65@SuppressWarnings("serial")66public final class TextureAnim extends AnimatingControlsSurface {6768public static final Color colorblend = new Color(0f, 0f, 1f, .5f);69protected static BufferedImage textureImg;70protected int bNum;71protected int tilesize;72private boolean newtexture;73private TexturePaint texturePaint;74private Rectangle tilerect;75private boolean bouncesize = false;76private boolean bouncerect = true;77private boolean rotate = false;78private boolean shearx = false;79private boolean sheary = false;80private boolean showanchor = true;81private AnimVal w, h, x, y, rot, shx, shy;82private static Image[] img = new Image[2];8384public TextureAnim() {85img[0] = getImage("duke.gif"); // 8 bit gif86img[1] = getImage("duke.png"); // 24 bit png8788textureImg = makeImage(32, 0);89tilesize = textureImg.getWidth();90w = new AnimVal(0, 200, 3, 10, tilesize);91h = new AnimVal(0, 200, 3, 10, tilesize);92x = new AnimVal(0, 200, 3, 10, 0);93y = new AnimVal(0, 200, 3, 10, 0);94rot = new AnimVal(-360, 360, 5, 15, 0);95shx = new AnimVal(-50, 50, 3, 10, 0);96shy = new AnimVal(-50, 50, 3, 10, 0);97tilerect = new Rectangle(x.getInt(), y.getInt(),98w.getInt(), h.getInt());99texturePaint = new TexturePaint(textureImg, tilerect);100setControls(new Component[] { new DemoControls(this) });101}102103protected BufferedImage makeImage(int size, int num) {104newtexture = true;105switch (bNum = num) {106case 0:107return makeRGBImage(size);108case 1:109return makeGIFImage(size);110case 2:111return makePNGImage(size);112}113return null;114}115116private BufferedImage makeRGBImage(int size) {117BufferedImage bi = new BufferedImage(size, size,118BufferedImage.TYPE_INT_RGB);119Graphics2D big = bi.createGraphics();120big.setColor(WHITE);121big.fillRect(0, 0, size, size);122for (int j = 0; j < size; j++) {123float RED = j / (float) size;124for (int i = 0; i < size; i++) {125float GREEN = i / (float) size;126big.setColor(new Color(1.0f - RED, 1.0f - GREEN, 0.0f, 1.0f));127big.drawLine(i, j, i, j);128}129}130return bi;131}132133private BufferedImage makeGIFImage(int d) {134BufferedImage bi = new BufferedImage(d, d, BufferedImage.TYPE_INT_RGB);135Graphics2D big = bi.createGraphics();136big.drawImage(img[0], 0, 0, d, d, new Color(204, 204, 255), null);137return bi;138}139140private BufferedImage makePNGImage(int d) {141BufferedImage bi = new BufferedImage(d, d, BufferedImage.TYPE_INT_RGB);142Graphics2D big = bi.createGraphics();143big.drawImage(img[1], 0, 0, d, d, LIGHT_GRAY, null);144return bi;145}146147@Override148public void reset(int width, int height) {149x.newlimits(-width / 4, width / 4 - w.getInt());150y.newlimits(-height / 4, height / 4 - h.getInt());151}152153@Override154public void step(int width, int height) {155if (tilesize != textureImg.getWidth()) {156tilesize = textureImg.getWidth();157}158if (bouncesize) {159w.anim();160h.anim();161x.newlimits(-width / 4, width / 4 - w.getInt());162y.newlimits(-height / 4, height / 4 - h.getInt());163} else {164if (w.getInt() != tilesize) {165w.set(tilesize);166x.newlimits(-width / 4, width / 4 - w.getInt());167}168if (h.getInt() != tilesize) {169h.set(tilesize);170y.newlimits(-height / 4, height / 4 - h.getInt());171}172}173if (bouncerect) {174x.anim();175y.anim();176}177if (newtexture || x.getInt() != tilerect.x || y.getInt() != tilerect.y || w.178getInt() != tilerect.width || h.getInt() != tilerect.height) {179newtexture = false;180int X = x.getInt();181int Y = y.getInt();182int W = w.getInt();183int H = h.getInt();184tilerect = new Rectangle(X, Y, W, H);185texturePaint = new TexturePaint(textureImg, tilerect);186}187}188189@Override190public void render(int width, int height, Graphics2D g2) {191192g2.translate(width / 2, height / 2);193if (rotate) {194rot.anim();195g2.rotate(Math.toRadians(rot.getFlt()));196} else {197rot.set(0);198}199if (shearx) {200shx.anim();201g2.shear(shx.getFlt() / 100, 0.0f);202} else {203shx.set(0);204}205if (sheary) {206shy.anim();207g2.shear(0.0f, shy.getFlt() / 100);208} else {209shy.set(0);210}211g2.setPaint(texturePaint);212g2.fillRect(-1000, -1000, 2000, 2000);213if (showanchor) {214g2.setColor(BLACK);215g2.setColor(colorblend);216g2.fill(tilerect);217}218}219220public static void main(String[] argv) {221createDemoFrame(new TextureAnim());222}223224225static final class AnimVal {226227float curval;228float lowval;229float highval;230float currate;231float lowrate;232float highrate;233234public AnimVal(int lowval, int highval,235int lowrate, int highrate) {236this.lowval = lowval;237this.highval = highval;238this.lowrate = lowrate;239this.highrate = highrate;240this.curval = randval(lowval, highval);241this.currate = randval(lowrate, highrate);242}243244public AnimVal(int lowval, int highval,245int lowrate, int highrate,246int pos) {247this(lowval, highval, lowrate, highrate);248set(pos);249}250251public float randval(float low, float high) {252return (float) (low + Math.random() * (high - low));253}254255public float getFlt() {256return curval;257}258259public int getInt() {260return (int) curval;261}262263public void anim() {264curval += currate;265clip();266}267268public void set(float val) {269curval = val;270clip();271}272273public void clip() {274if (curval > highval) {275curval = highval - (curval - highval);276if (curval < lowval) {277curval = highval;278}279currate = -randval(lowrate, highrate);280} else if (curval < lowval) {281curval = lowval + (lowval - curval);282if (curval > highval) {283curval = lowval;284}285currate = randval(lowrate, highrate);286}287}288289public void newlimits(int lowval, int highval) {290this.lowval = lowval;291this.highval = highval;292clip();293}294} // End AnimVal class295296297final class DemoControls extends CustomControls implements ActionListener {298299TextureAnim demo;300JToolBar toolbar;301JComboBox<String> combo;302JMenu menu;303JMenuItem[] menuitems;304int iconSize = 20;305ButtonBorder buttonBorder = new ButtonBorder();306307@SuppressWarnings("LeakingThisInConstructor")308public DemoControls(TextureAnim demo) {309super(demo.name);310this.demo = demo;311menuitems = new JMenuItem[3];312add(toolbar = new JToolBar());313toolbar.setFloatable(false);314addTool("BO", "bounce", true);315addTool("SA", "show anchor", true);316addTool("RS", "resize", false);317addTool("RO", "rotate", false);318addTool("SX", "shear x", false);319addTool("SY", "shear y", false);320add(combo = new JComboBox<>());321combo.addActionListener(this);322combo.addItem("8");323combo.addItem("16");324combo.addItem("32");325combo.addItem("64");326combo.addItem("80");327combo.setSelectedIndex(2);328329JMenuBar menuBar = new JMenuBar();330menu = menuBar.add(new JMenu());331for (int i = 0; i < 3; i++) {332BufferedImage bimg = demo.makeImage(iconSize, i);333TexturedIcon icon = new TexturedIcon(bimg);334menuitems[i] = menu.add(new JMenuItem(icon));335menuitems[i].addActionListener(this);336}337menu.setIcon(menuitems[0].getIcon());338add(menuBar);339demo.bNum = 0;340}341342public void addTool(String str, String toolTip, boolean state) {343JToggleButton b =344(JToggleButton) toolbar.add(new JToggleButton(str));345b.setBorder(buttonBorder);346b.setFocusPainted(false);347b.setSelected(state);348b.setToolTipText(toolTip);349b.addActionListener(this);350int width = b.getPreferredSize().width+10;351Dimension prefSize = new Dimension(width, 21);352b.setPreferredSize(prefSize);353b.setMaximumSize(prefSize);354b.setMinimumSize(prefSize);355}356357@Override358public void actionPerformed(ActionEvent e) {359Object obj = e.getSource();360if (obj instanceof JComboBox) {361String selItem = (String) combo.getSelectedItem();362if (selItem != null) {363int size = Integer.parseInt(selItem);364TextureAnim.textureImg = demo.makeImage(size, demo.bNum);365}366} else if (obj instanceof JMenuItem) {367for (int i = 0; i < menuitems.length; i++) {368if (obj.equals(menuitems[i])) {369TextureAnim.textureImg =370demo.makeImage(demo.tilesize, i);371menu.setIcon(menuitems[i].getIcon());372break;373}374}375} else {376JToggleButton b = (JToggleButton) obj;377if (b.getText().equals("BO")) {378demo.bouncerect = b.isSelected();379} else if (b.getText().equals("SA")) {380demo.showanchor = b.isSelected();381} else if (b.getText().equals("RS")) {382demo.bouncesize = b.isSelected();383} else if (b.getText().equals("RO")) {384demo.rotate = b.isSelected();385} else if (b.getText().equals("SX")) {386demo.shearx = b.isSelected();387} else if (b.getText().equals("SY")) {388demo.sheary = b.isSelected();389}390}391if (!demo.animating.running()) {392demo.repaint();393}394}395396@Override397public Dimension getPreferredSize() {398return new Dimension(200, 41);399}400401@Override402@SuppressWarnings("SleepWhileHoldingLock")403public void run() {404Thread me = Thread.currentThread();405while (thread == me) {406for (int i = 2; i < toolbar.getComponentCount(); i++) {407try {408Thread.sleep(4444);409} catch (InterruptedException e) {410return;411}412((AbstractButton) toolbar.getComponentAtIndex(i)).doClick();413}414}415thread = null;416}417418419class TexturedIcon implements Icon {420421BufferedImage bi;422423public TexturedIcon(BufferedImage bi) {424this.bi = bi;425}426427@Override428public void paintIcon(Component c, Graphics g, int x, int y) {429Graphics2D g2 = (Graphics2D) g;430Rectangle r = new Rectangle(x, y, iconSize, iconSize);431g2.setPaint(new TexturePaint(bi, r));432g2.fillRect(x, y, iconSize, iconSize);433g2.setColor(GRAY);434g2.draw3DRect(x, y, iconSize - 1, iconSize - 1, true);435}436437@Override438public int getIconWidth() {439return iconSize;440}441442@Override443public int getIconHeight() {444return iconSize;445}446} // End TexturedIcon class447} // End DemoControls class448} // End TextureAnim class449450451452