📄 transformanim.java
字号:
/* * @(#)TransformAnim.java 1.41 06/08/29 * * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * -Redistribution of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * -Redistribution in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * Neither the name of Sun Microsystems, Inc. or the names of contributors may * be used to endorse or promote products derived from this software without * specific prior written permission. * * 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 MIDROSYSTEMS, INC. ("SUN") * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS 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 THIS SOFTWARE, * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. * * You acknowledge that this software is not designed, licensed or intended * for use in the design, construction, operation or maintenance of any * nuclear facility. *//* * @(#)TransformAnim.java 1.41 06/08/29 */package java2d.demos.Transforms;import java.awt.*;import java.awt.event.*;import java.awt.geom.*;import java.awt.image.*;import javax.swing.*;import javax.swing.event.*;import javax.swing.border.*;import java.util.Vector;import java2d.AnimatingControlsSurface;import java2d.CustomControls;import javax.swing.plaf.metal.MetalBorders.ButtonBorder;import static java.awt.Color.*;import static java.awt.Font.*;/** * Animation of shapes, text and images rotating, scaling and translating * around a canvas. */public class TransformAnim extends AnimatingControlsSurface { private static TexturePaint texture; static { BufferedImage bi = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB); Graphics2D gi = bi.createGraphics(); gi.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); gi.setColor(RED); gi.fillOval(0,0,9,9); texture = new TexturePaint(bi,new Rectangle(0,0,10,10)); } private static BasicStroke bs = new BasicStroke(6); private static Font fonts[] = { new Font("Times New Roman", PLAIN, 48), new Font("serif", BOLD|ITALIC, 24), new Font("Courier", BOLD, 36), new Font("Arial", BOLD|ITALIC, 64), new Font("Helvetica", PLAIN, 52)}; private static String strings[] = { "Transformation", "Rotate", "Translate", "Shear", "Scale" }; private static String imgs[] = { "duke.gif" }; private static Paint paints[] = { RED, BLUE, texture, GREEN, MAGENTA, ORANGE, PINK, CYAN, new Color(0, 255, 0, 128), new Color(0, 0, 255, 128), YELLOW, LIGHT_GRAY, WHITE}; private Vector<ObjData> objDatas = new Vector<ObjData>(13); private int numShapes, numStrings, numImages; protected boolean doRotate = true; protected boolean doTranslate = true; protected boolean doScale = true; protected boolean doShear; public TransformAnim() { setBackground(BLACK); setStrings(1); setImages(2); setShapes(10); setControls(new Component[] { new DemoControls(this) }); setConstraints(new String[] { BorderLayout.EAST }); } public void setImages(int num) { if (num < numImages) { Vector<ObjData> v = new Vector<ObjData>(objDatas.size()); for (ObjData objData : objDatas) { if (objData.object instanceof Image) { v.addElement(objData); } } objDatas.removeAll(v); v.setSize(num); objDatas.addAll(v); } else { Dimension d = getSize(); for (int i = numImages; i < num; i++) { Object obj = getImage(imgs[i % imgs.length]); ObjData objData = new ObjData(obj, BLACK); objData.reset(d.width, d.height); objDatas.addElement(objData); } } numImages = num; } public void setStrings(int num) { if (num < numStrings) { Vector<ObjData> v = new Vector<ObjData>(objDatas.size()); for (ObjData objData : objDatas) { if (objData.object instanceof TextData) { v.addElement(objData); } } objDatas.removeAll(v); v.setSize(num); objDatas.addAll(v); } else { Dimension d = getSize(); for (int i = numStrings; i < num; i++) { int j = i % fonts.length; int k = i % strings.length; Object obj = new TextData(strings[k], fonts[j]); ObjData objData = new ObjData(obj, paints[i%paints.length]); objData.reset(d.width, d.height); objDatas.addElement(objData); } } numStrings = num; } public void setShapes(int num) { if (num < numShapes) { Vector<ObjData> v = new Vector<ObjData>(objDatas.size()); for (ObjData objData : objDatas) { if (objData.object instanceof Shape) { v.addElement(objData); } } objDatas.removeAll(v); v.setSize(num); objDatas.addAll(v); } else { Dimension d = getSize(); for (int i = numShapes; i < num; i++) { Object obj = null; switch (i % 7) { case 0 : obj = new GeneralPath(); break; case 1 : obj = new Rectangle2D.Double(); break; case 2 : obj = new Ellipse2D.Double(); break; case 3 : obj = new Arc2D.Double(); break; case 4 : obj = new RoundRectangle2D.Double(); break; case 5 : obj = new CubicCurve2D.Double(); break; case 6 : obj = new QuadCurve2D.Double(); break; } ObjData objData = new ObjData(obj, paints[i%paints.length]); objData.reset(d.width, d.height); objDatas.addElement(objData); } } numShapes = num; } public void reset(int w, int h) { for (ObjData objData : objDatas) { objData.reset(w, h); } } public void step(int w, int h) { for (ObjData objData : objDatas) { objData.step(w, h, this); } } public void render(int w, int h, Graphics2D g2) { for (ObjData objData : objDatas) { g2.setTransform(objData.at); g2.setPaint(objData.paint); if (objData.object instanceof Image) { g2.drawImage((Image) objData.object, 0, 0, this); } else if (objData.object instanceof TextData) { g2.setFont(((TextData) objData.object).font); g2.drawString(((TextData) objData.object).string, 0, 0); } else if (objData.object instanceof QuadCurve2D || objData.object instanceof CubicCurve2D) { g2.setStroke(bs); g2.draw((Shape) objData.object); } else if (objData.object instanceof Shape) { g2.fill((Shape) objData.object); } } } public static void main(String argv[]) { createDemoFrame(new TransformAnim()); } static class TextData extends Object { public String string; public Font font; public TextData(String str, Font font) { string = str; this.font = font; } } static class ObjData extends Object {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -