当前位置: 技术问答>java相关
如何通过Applet来显示一个静态的图片,但是显示形式是动态的
来源: 互联网 发布时间:2015-02-07
本文导语: 不管如何动态,从左至右,从上至下,等等 | import java.awt.*; import java.awt.event.*; import java.awt.image.ImageObserver; import java.awt.image.BufferedImage; import javax.swing.*; import java.net.URL; /** * The DukeAnim...
不管如何动态,从左至右,从上至下,等等
|
import java.awt.*;
import java.awt.event.*;
import java.awt.image.ImageObserver;
import java.awt.image.BufferedImage;
import javax.swing.*;
import java.net.URL;
/**
* The DukeAnim class displays an animated gif with a transparent background.
*/
public class DukeAnim extends JApplet implements ImageObserver {
private static Image agif, clouds;
private static int aw, ah, cw;
private int x;
private BufferedImage bimg;
public void init() {
setBackground(Color.white);
clouds = getDemoImage("clouds.jpg");
agif = getDemoImage("duke.running.gif");
aw = agif.getWidth(this) / 2;
ah = agif.getHeight(this) / 2;
cw = clouds.getWidth(this);
}
public Image getDemoImage(String name) {
URL url = DukeAnim.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;
}
public void drawDemo(int w, int h, Graphics2D g2) {
if ((x -= 3)
import java.awt.event.*;
import java.awt.image.ImageObserver;
import java.awt.image.BufferedImage;
import javax.swing.*;
import java.net.URL;
/**
* The DukeAnim class displays an animated gif with a transparent background.
*/
public class DukeAnim extends JApplet implements ImageObserver {
private static Image agif, clouds;
private static int aw, ah, cw;
private int x;
private BufferedImage bimg;
public void init() {
setBackground(Color.white);
clouds = getDemoImage("clouds.jpg");
agif = getDemoImage("duke.running.gif");
aw = agif.getWidth(this) / 2;
ah = agif.getHeight(this) / 2;
cw = clouds.getWidth(this);
}
public Image getDemoImage(String name) {
URL url = DukeAnim.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;
}
public void drawDemo(int w, int h, Graphics2D g2) {
if ((x -= 3)