📄 fadeanim.java
字号:
/* * @(#)FadeAnim.java 1.29 99/05/07 * * Copyright (c) 1998, 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.Composite;import java.awt.*;import java.awt.geom.*;import java.awt.image.*;import java.awt.event.*;import java.util.Vector;import javax.swing.*;import javax.swing.event.*;import javax.swing.border.*;import AnimatingContext;import DemoSurface;import DemoPanel;import CustomControls;/** * Animation of compositing shapes, text and images fading in and out. */public class FadeAnim extends DemoSurface implements AnimatingContext, CustomControls { private static TexturePaint texture; static { int w = 10; int h = 10; BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); Graphics2D gi = bi.createGraphics(); Color oc = Color.blue; Color ic = Color.green; gi.setPaint(new GradientPaint(0,0,oc,w*.35f,h*.35f,ic)); gi.fillRect(0, 0, w/2, h/2); gi.setPaint(new GradientPaint(w,0,oc,w*.65f,h*.35f,ic)); gi.fillRect(w/2, 0, w/2, h/2); gi.setPaint(new GradientPaint(0,h,oc,w*.35f,h*.65f,ic)); gi.fillRect(0, h/2, w/2, h/2); gi.setPaint(new GradientPaint(w,h,oc,w*.65f,h*.65f,ic)); gi.fillRect(w/2, h/2, w/2, h/2); texture = new TexturePaint(bi,new Rectangle(0,0,w,h)); } private static BasicStroke bs = new BasicStroke(6); private static Font fonts[] = { new Font("Times New Roman", Font.PLAIN, 64), new Font("serif", Font.BOLD + Font.ITALIC, 24), new Font("Courier", Font.BOLD, 36), new Font("Arial", Font.BOLD + Font.ITALIC, 48), new Font("Helvetica", Font.PLAIN, 52)}; private static String strings[] = { "Alpha", "Composite", "Src", "SrcOver", "SrcIn", "SrcOut", "Clear", "DstOver", "DstIn" }; private static String imgs[] = { "jumptojavastrip.gif", "duke.gif", "star7.gif" }; private static Paint paints[] = { Color.red, Color.blue, Color.green, Color.magenta, Color.orange, Color.pink, Color.cyan, texture, Color.yellow, Color.lightGray, Color.white}; private Vector vector = new Vector(20); private DemoControls controls; private int numShapes, numStrings, numImages; public FadeAnim() { setBackground(Color.black); setStrings(2); setImages(3); setShapes(8); controls = new DemoControls(this); } public String[] getCustomControlsConstraints() { return new String[] { BorderLayout.EAST }; } 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(); } } 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]); if (imgs[i % imgs.length].equals("jumptojavastrip.gif")) { int iw = ((Image) obj).getWidth(null); int ih = ((Image) obj).getHeight(null); BufferedImage bimg = new BufferedImage(iw, ih, BufferedImage.TYPE_INT_RGB); bimg.createGraphics().drawImage((Image) obj, 0, 0, null); obj = bimg; } ObjectData od = new ObjectData(obj, Color.black); od.reset(d.width, d.height); vector.addElement(od); } } numImages = num; } 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], this); ObjectData od = new ObjectData(obj, paints[i%paints.length]); od.reset(d.width, d.height); vector.addElement(od); } } numStrings = num; } 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; } public void reset(int w, int h) { for (int i = 0; i < vector.size(); i++) { ((ObjectData) vector.get(i)).reset(w, h); } } public void step(int w, int h) { for (int i = 0; i < vector.size(); i++) { ((ObjectData) vector.get(i)).step(w, h); } } public void drawDemo(int w, int h, Graphics2D g2) { for (int i = 0; i < vector.size(); i++) { ObjectData od = (ObjectData) vector.get(i); AlphaComposite ac = AlphaComposite.getInstance( AlphaComposite.SRC_OVER, od.alpha); g2.setComposite(ac); g2.setPaint(od.paint); g2.translate(od.x, od.y); 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); } g2.translate(-od.x, -od.y);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -