📄 textureanim.java
字号:
/* * @(#)TextureAnim.java 1.7 99/06/22 * * Copyright (c) 1997-1999 by Sun Microsystems, Inc. All Rights Reserved. * * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use, * modify and redistribute this software in source and binary code form, * provided that i) this copyright notice and license appear on all copies of * the software; and ii) Licensee does not utilize the software in a manner * which is disparaging to Sun. * * This software is provided "AS IS," without a warranty of any kind. ALL * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE * POSSIBILITY OF SUCH DAMAGES. * * This software is not designed or intended for use in on-line control of * aircraft, air traffic, aircraft navigation or aircraft communications; or in * the design, construction, operation or maintenance of any nuclear * facility. Licensee represents and warrants that it will not use or * redistribute the Software for such purposes. */package demos.Paint; import java.awt.*;import java.awt.event.*;import java.awt.image.BufferedImage;import java.awt.geom.AffineTransform;import java.awt.font.TextLayout;import java.awt.font.FontRenderContext;import javax.swing.*;import AnimatingContext;import DemoSurface;import DemoPanel;import CustomControls;/** * TexturePaint animation with controls for transformations. */public class TextureAnim extends DemoSurface implements AnimatingContext, CustomControls { public static final Color colorblend = new Color(0f, 0f, 1f, .5f); protected static BufferedImage textureImg; protected int bNum; protected int tilesize; private boolean newtexture; private TexturePaint texture; private Rectangle tilerect; private boolean bouncesize = false; private boolean bouncerect = true; private boolean rotate = false; private boolean shearx = false; private boolean sheary = false; private boolean showanchor = true; private boolean quality = false; private AnimVal w, h, x, y, rot, shx, shy; private DemoControls controls; private static Image img[] = new Image[1]; public TextureAnim() { img[0] = getImage("duke.gif"); // 8 bit gif textureImg = makeImage(32, 0); tilesize = textureImg.getWidth(); w = new AnimVal(0, 200, 3, 10, tilesize); h = new AnimVal(0, 200, 3, 10, tilesize); x = new AnimVal(0, 200, 3, 10, 0); y = new AnimVal(0, 200, 3, 10, 0); rot = new AnimVal(-360, 360, 5, 15, 0); shx = new AnimVal(-50, 50, 3, 10, 0); shy = new AnimVal(-50, 50, 3, 10, 0); tilerect = new Rectangle(x.getInt(), y.getInt(), w.getInt(), h.getInt()); texture = new TexturePaint(textureImg, tilerect); controls = new DemoControls(this); } public String[] getCustomControlsConstraints() { return new String[] { BorderLayout.NORTH }; } public Component[] getCustomControls() { return new Component[] { (Component) controls }; } public void customControlsThread(int state) { if (state == CustomControls.START) { controls.start(); } else if (state == CustomControls.STOP) { controls.stop(); } } protected BufferedImage makeImage(int size, int num) { newtexture = true; switch (bNum = num) { case 0 : return makeBGRImage(size); case 1 : return makeGIFImage(size); } return null; } private BufferedImage makeBGRImage(int size) { BufferedImage bi = new BufferedImage(size, size, BufferedImage.TYPE_INT_BGR); Graphics2D big = bi.createGraphics(); big.setColor(Color.white); big.fillRect(0, 0, size, size); for (int j = 0; j < size; j++) { float red = j / (float) size; for (int i = 0; i < size; i++) { float green = i / (float) size; big.setColor(new Color(1.0f - red, 1.0f - green, 0.0f, 1.0f)); big.drawLine(i, j, i, j); } } return bi; } private BufferedImage makeGIFImage(int d) { BufferedImage bi = new BufferedImage(d, d, BufferedImage.TYPE_INT_RGB); Graphics2D big = bi.createGraphics(); big.drawImage(img[0], 0, 0, d, d, new Color(204, 204, 255), null); return bi; } public void reset(int width, int height) { x.newlimits(-width/4, width/4 - w.getInt()); y.newlimits(-height/4, height/4 - h.getInt()); } public void step(int width, int height) { if (tilesize != textureImg.getWidth()) { tilesize = textureImg.getWidth(); } if (bouncesize) { w.anim(); h.anim(); x.newlimits(-width/4, width/4 - w.getInt()); y.newlimits(-height/4, height/4 - h.getInt()); } else { if (w.getInt() != tilesize) { w.set(tilesize); x.newlimits(-width/4, width/4 - w.getInt()); } if (h.getInt() != tilesize) { h.set(tilesize); y.newlimits(-height/4, height/4 - h.getInt()); } } if (bouncerect) { x.anim(); y.anim(); } if (newtexture || x.getInt() != tilerect.x || y.getInt() != tilerect.y || w.getInt() != tilerect.width || h.getInt() != tilerect.height) { newtexture = false; int X = x.getInt(); int Y = y.getInt(); int W = w.getInt(); int H = h.getInt(); tilerect = new Rectangle(X, Y, W, H); texture = new TexturePaint(textureImg, tilerect); } } public void drawDemo(int width, int height, Graphics2D g2) { g2.translate(width/2, height/2); if (rotate) { rot.anim(); g2.rotate(Math.toRadians(rot.getFlt())); } else { rot.set(0); } if (shearx) { shx.anim(); g2.shear(shx.getFlt()/100, 0.0f); } else { shx.set(0); } if (sheary) { shy.anim(); g2.shear(0.0f, shy.getFlt()/100); } else { shy.set(0); } g2.setPaint(texture); g2.fillRect(-1000, -1000, 2000, 2000); if (showanchor) { g2.setColor(Color.black); g2.setColor(colorblend); g2.fill(tilerect); } } public static void main(String argv[]) { final DemoPanel dp = new DemoPanel(new TextureAnim()); JFrame f = new JFrame("Java2D Demo - TexureAnim"); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} public void windowDeiconified(WindowEvent e) { dp.surface.start(); } public void windowIconified(WindowEvent e) { dp.surface.stop(); } }); f.getContentPane().add("Center", dp);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -