⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 transformanim.java

📁 please read it,you can
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * @(#)TransformAnim.java	1.6  98/12/03 * * Copyright 1998 by Sun Microsystems, Inc., * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A. * All rights reserved. * * This software is the confidential and proprietary information * of Sun Microsystems, Inc. ("Confidential Information").  You * shall not disclose such Confidential Information and shall use * it only in accordance with the terms of the license agreement * you entered into with Sun. */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 java.net.URL;/** * The TransformAnim class performs animation of shapes, text and images  * rotating, scaling, shearing and translating around a surface. */public class TransformAnim extends JApplet {    Demo demo;    public void init() {        getContentPane().add(demo = new Demo());        getContentPane().add("East", new DemoControls(demo));    }    public void start() {        demo.start();    }      public void stop() {        demo.stop();    }    /**     * The Demo class performs the transformations and the painting.     */    static class Demo extends JPanel implements Runnable {        private static TexturePaint texture;        // creates the TexturePaint pattern        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(Color.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", Font.PLAIN, 48),                    new Font("serif", Font.BOLD + Font.ITALIC, 24),                    new Font("Courier", Font.BOLD, 36),                    new Font("Arial", Font.BOLD + Font.ITALIC, 64),                    new Font("Helvetica", Font.PLAIN, 52)};        private static String strings[] = {                    "Transformation", "Rotate", "Translate",                    "Shear", "Scale" };        private static String imgs[] = { "duke.gif" };        private static Paint paints[] = {                     Color.red, Color.blue, texture, Color.green, Color.magenta,                     Color.orange, Color.pink, Color.cyan,                     new Color(0, 255, 0, 128), new Color(0, 0, 255, 128),                     Color.yellow, Color.lightGray, Color.white};        private Vector vector = new Vector(13);        private int numShapes, numStrings, numImages;        private Thread thread;        private BufferedImage bimg;        protected boolean doRotate = true;        protected boolean doTranslate = true;        protected boolean doScale = true;        protected boolean doShear;                public Demo() {            setBackground(Color.black);            // initializes numbers of Strings, Images and Shapes            setStrings(1);            setImages(2);            setShapes(10);        }            public Image getImage(String name) {            URL url = TransformAnim.class.getResource(name);            Image img = getToolkit().getImage(url);            try {                MediaTracker tracker = new MediaTracker(this);                tracker.addImage(img, 0);                tracker.waitForID(0);            } catch (Exception e) {}            return img;        }            // adds images to a Vector        public void setImages(int num) {                       if (num < numImages) {                Vector v = new Vector(vector.size());                for (int i = 0; i < vector.size(); i++) {                    if (((ObjectData) vector.get(i)).object instanceof Image) {                        v.addElement(vector.get(i));                    }                }                vector.removeAll(v);                v.setSize(num);                vector.addAll(v);            } else {                Dimension d = getSize();                for (int i = numImages; i < num; i++) {                    Object obj = getImage(imgs[i % imgs.length]);                    ObjectData od = new ObjectData(obj, Color.black);                    od.reset(d.width, d.height);                    vector.addElement(od);                }            }            numImages = num;        }                    // adds Strings to a Vector        public void setStrings(int num) {                if (num < numStrings) {                Vector v = new Vector(vector.size());                for (int i = 0; i < vector.size(); i++) {                    if (((ObjectData) vector.get(i)).object instanceof TextData) {                        v.addElement(vector.get(i));                    }                }                vector.removeAll(v);                v.setSize(num);                vector.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]);                     ObjectData od = new ObjectData(obj, paints[i%paints.length]);                    od.reset(d.width, d.height);                    vector.addElement(od);                }            }            numStrings = num;        }                    // adds Shapes to a Vector        public void setShapes(int num) {                if (num < numShapes) {                Vector v = new Vector(vector.size());                for (int i = 0; i < vector.size(); i++) {                    if (((ObjectData) vector.get(i)).object instanceof Shape) {                        v.addElement(vector.get(i));                    }                }                vector.removeAll(v);                v.setSize(num);                vector.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;                    }                    ObjectData od = new ObjectData(obj, paints[i%paints.length]);                    od.reset(d.width, d.height);                    vector.addElement(od);                }            }             numShapes = num;        }                    // calls ObjectData.reset for each item in vector        public void reset(int w, int h) {            for (int i = 0; i < vector.size(); i++) {                ((ObjectData) vector.get(i)).reset(w, h);            }        }            // calls ObjectData.step for each item in vector        public void step(int w, int h) {            for (int i = 0; i < vector.size(); i++) {                ((ObjectData) vector.get(i)).step(w, h, this);            }        }                public void drawDemo(int w, int h, Graphics2D g2) {                for (int i = 0; i < vector.size(); i++) {                ObjectData od = (ObjectData) vector.get(i);                g2.setTransform(od.at);                g2.setPaint(od.paint);                if (od.object instanceof Image) {                    g2.drawImage((Image) od.object, 0, 0, this);                } else if (od.object instanceof TextData) {                    g2.setFont(((TextData) od.object).font);                    g2.drawString(((TextData) od.object).string, 0, 0);                } else if (od.object instanceof QuadCurve2D                         || od.object instanceof CubicCurve2D)                 {                    g2.setStroke(bs);                    g2.draw((Shape) od.object);                } else if (od.object instanceof Shape) {                    g2.fill((Shape) od.object);                }            }        }                public Graphics2D createGraphics2D(int w, int h) {            Graphics2D g2 = null;            if (bimg == null || bimg.getWidth() != w || bimg.getHeight() != h) {                bimg = (BufferedImage) createImage(w, h);                reset(w, h);            }             g2 = bimg.createGraphics();            g2.setBackground(getBackground());            g2.clearRect(0, 0, w, h);            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,                                RenderingHints.VALUE_ANTIALIAS_ON);            g2.setRenderingHint(RenderingHints.KEY_RENDERING,                                RenderingHints.VALUE_RENDER_QUALITY);            return g2;        }                public void paint(Graphics g) {            Dimension d = getSize();            step(d.width, d.height);            Graphics2D g2 = createGraphics2D(d.width, d.height);            drawDemo(d.width, d.height, g2);            g2.dispose();            g.drawImage(bimg, 0, 0, this);        }                public void start() {            thread = new Thread(this);            thread.setPriority(Thread.MIN_PRIORITY);            thread.start();        }                public synchronized void stop() {            thread = null;        }                public void run() {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -