Java 提供大量加载和显示图像的方法,可以使用 ImageIO,Jimi 或 JAI 中提供的一些 API,在这里简单地说几种 AWT 中加载和显示图像的方法。
1、使用 java.applet.Applet 提供的 getImage() 方法
abstract Image |
getImage(String filename) Returns an image which gets pixel data from the specified file, whose format can be either GIF, JPEG or PNG. |
abstract Image |
getImage(URL url) Returns an image which gets pixel data from the specified URL. |
import java.net.URL;
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;
public class ImageTestAppletSimple
extends Applet {
private Image im;
public void init() {
// 指定这个 applet 位置的 URL
URL codebase = getCodeBase();
im = getImage(codebase, "lena.jpg");
}
public void paint(Graphics g) {
g.drawImage(im, 0, 0, this);
}
}
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;
public class ImageTestAppletSimple
extends Applet {
private Image im;
public void init() {
// 指定这个 applet 位置的 URL
URL codebase = getCodeBase();
im = getImage(codebase, "lena.jpg");
}
public void paint(Graphics g) {
g.drawImage(im, 0, 0, this);
}
}
2、使用 java.awt.Toolkit 提供的 getImage() 方法
abstract Image |
getImage(String filename) Returns an image which gets pixel data from the specified file, whose format can be either GIF, JPEG or PNG. |
abstract Image |
getImage(URL url) Returns an image which gets pixel data from the specified URL. |
import java.awt.*;
import java.awt.event.*;
public class ImageTestApplication
extends Frame {
Insets insets;
Image im;
static public void main(String args[]) {
ImageTestApplication app = new ImageTestApplication();
app.show();
}
public ImageTestApplication() {
super("Image Test");
im = Toolkit.getDefaultToolkit().getImage("lena.jpg");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent event) {
dispose();
System.exit(0);
}
});
}
public void addNotify() {
super.addNotify();
insets = getInsets();
setBounds(100, 100, 300 + insets.left, 300 + insets.top);
}
public void paint(Graphics g) {
g.drawImage(im, insets.left, insets.top, this);
}
}
import java.awt.event.*;
public class ImageTestApplication
extends Frame {
Insets insets;
Image im;
static public void main(String args[]) {
ImageTestApplication app = new ImageTestApplication();
app.show();
}
public ImageTestApplication() {
super("Image Test");
im = Toolkit.getDefaultToolkit().getImage("lena.jpg");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent event) {
dispose();
System.exit(0);
}
});
}
public void addNotify() {
super.addNotify();
insets = getInsets();
setBounds(100, 100, 300 + insets.left, 300 + insets.top);
}
public void paint(Graphics g) {
g.drawImage(im, insets.left, insets.top, this);
}
}
3、将图像作为资源加载
·java.lang.Class
URL |
getResource(String name) Finds a resource with a given name. |
InputStream |
getResourceAsStream(String name) Finds a resource with a given name. |
·java.awt.Component 中提供方法来创建图像
Image |
createImage(ImageProducer producer) Creates an image from the specified image producer. |
Image |
createImage(int width, int height) Creates an off-screen drawable image to be used for double buffering. |
·java.awt.Toolkit 中提供方法来创建图像
Image |
createImage(byte[] imagedata) Creates an image which decodes the image stored in the specified byte array. |
abstract Image |
createImage(byte[] imagedata, int imageoffset, int imagelength) Creates an image which decodes the image stored in the specified byte array, and at the specified offset and length. |
abstract Image |
createImage(ImageProducer producer) Creates an image with the specified image producer. |
abstract Image |
createImage(String filename) Returns an image which gets pixel data from the specified file. |
abstract Image |
createImage(URL url) Returns an image |
import java.applet.Applet;
import java.net.URL;
import java.awt.*;
import java.awt.image.ImageProducer;
import java.awt.event.*;
public class Test
extends Applet {
Image im;
public void start() {
URL url = getClass().getResource("lena.jpg");
try {
im = createImage( (ImageProducer) url.getContent());
if (im == null) {
System.out.println("null image");
}
}
catch (Exception e) {
e.printStackTrace();
}
}
public void paint(Graphics g) {
Insets insets = getInsets();
g.drawImage(im, insets.left, insets.top, this);
}
public void update(Graphics g) {
paint(g);
}
}
import java.net.URL;
import java.awt.*;
import java.awt.image.ImageProducer;
import java.awt.event.*;
public class Test
extends Applet {
Image im;
public void start() {
URL url = getClass().getResource("lena.jpg");
try {
im = createImage( (ImageProducer) url.getContent());
if (im == null) {
System.out.println("null image");
}
}
catch (Exception e) {
e.printStackTrace();
}
}
public void paint(Graphics g) {
Insets insets = getInsets();
g.drawImage(im, insets.left, insets.top, this);
}
public void update(Graphics g) {
paint(g);
}
}