DukeAnim.java
Upload User: cdlibang
Upload Date: 2016-07-17
Package Size: 774k
Code Size: 3k
Category:

2D Graphic

Development Platform:

Java

  1. /*
  2.  * @(#)DukeAnim.java 1.11 99/04/23
  3.  *
  4.  * Copyright (c) 1998, 1999 by Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
  7.  * modify and redistribute this software in source and binary code form,
  8.  * provided that i) this copyright notice and license appear on all copies of
  9.  * the software; and ii) Licensee does not utilize the software in a manner
  10.  * which is disparaging to Sun.
  11.  * 
  12.  * This software is provided "AS IS," without a warranty of any kind. ALL
  13.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  14.  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  15.  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
  16.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  17.  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
  18.  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
  19.  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
  20.  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
  21.  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
  22.  * POSSIBILITY OF SUCH DAMAGES.
  23.  * 
  24.  * This software is not designed or intended for use in on-line control of
  25.  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
  26.  * the design, construction, operation or maintenance of any nuclear
  27.  * facility. Licensee represents and warrants that it will not use or
  28.  * redistribute the Software for such purposes.
  29.  */
  30. package demos.Images;
  31. import java.awt.*;
  32. import java.awt.event.*;
  33. import javax.swing.JButton;
  34. import java.awt.image.ImageObserver;
  35. import DemoSurface;
  36. import DemoPanel;
  37. import AnimatingContext;
  38. /**
  39.  * Animated gif with a transparent background.
  40.  */
  41. public class DukeAnim extends DemoSurface implements AnimatingContext, ImageObserver {
  42.     private static Image agif, clouds;
  43.     private static int aw, ah, cw;
  44.     private int x;
  45.     private JButton b;
  46.     public DukeAnim() {
  47.         setBackground(Color.white);
  48.         clouds = getImage("clouds.jpg");
  49.         agif = getImage("duke.running.gif");
  50.         aw = agif.getWidth(this) / 2;
  51.         ah = agif.getHeight(this) / 2;
  52.         cw = clouds.getWidth(this);
  53.         dontThread = true;
  54.     }
  55.     public void reset(int w, int h) { 
  56.         b = ((DemoPanel) getParent()).tools.startStopB;
  57.     }
  58.     public void step(int w, int h) { }
  59.     public void drawDemo(int w, int h, Graphics2D g2) {
  60.         if ((x -= 3) <= -cw) {
  61.             x = w;
  62.         }
  63.         g2.drawImage(clouds, x, 10, cw, h-20, this);
  64.         g2.drawImage(agif, w/2-aw, h/2-ah, this);
  65.     }
  66.     public boolean imageUpdate(Image img, int infoflags,
  67.                 int x, int y, int width, int height)
  68.     {
  69.         if (b.isSelected() && (infoflags & ALLBITS) != 0)
  70.             repaint();
  71.         if (b.isSelected() && (infoflags & FRAMEBITS) != 0)
  72.             repaint();
  73.         return isShowing();
  74.     }
  75.     public static void main(String s[]) {
  76.         WindowListener l = new WindowAdapter() {
  77.             public void windowClosing(WindowEvent e) {System.exit(0);}
  78.         };
  79.         Frame f = new Frame("Java2D Demo - DukeAnim");
  80.         f.addWindowListener(l);
  81.         f.add("Center", new DemoPanel(new DukeAnim()));
  82.         f.pack();
  83.         f.setSize(new Dimension(400,300));
  84.         f.show();
  85.     }
  86. }