/* * @(#)DukeAnim.java 1.7 98/06/29 * * 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. */ package demos.Images; import java.awt.*; import java.awt.event.*; import java.awt.image.ImageObserver; import J2DCanvas; /** * Animated gif with a transparent background. */ public class DukeAnim extends J2DCanvas implements ImageObserver { private Image agif, clouds; private int xx, x, y, aw, ah; public DukeAnim() { setBackground(Color.white); clouds = getImage("images/clouds.jpg"); agif = getImage("images/duke.running.gif"); aw = agif.getWidth(this) / 2; ah = agif.getHeight(this) / 2; isRunning = true; } public void drawDemo(Graphics2D g2) { x = w/2 - aw; y = h/2 - ah; if (xx <= -clouds.getWidth(this)) xx = w; g2.drawImage(clouds, xx, 10, clouds.getWidth(this), h-20, this); // animated gif uses background color during blit g2.drawImage(agif, x, y, this); xx -= 3; } public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) { Graphics g = getGraphics(); if (isRunning && (infoflags & ALLBITS) != 0) paint(g); if (isRunning && (infoflags & FRAMEBITS) != 0) paint(g); if (isShowing()) g.dispose(); return isShowing(); } public static void main(String s[]) { final DukeAnim demo = new DukeAnim(); WindowListener l = new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} public void windowDeiconified(WindowEvent e) { demo.start(); } public void windowIconified(WindowEvent e) { demo.stop(); } }; Frame f = new Frame("Java2D Demo - DukeAnim"); f.addWindowListener(l); f.add("Center", demo); f.pack(); f.setSize(new Dimension(400,300)); f.show(); } }