- 论坛徽章:
- 0
|
示例一
import java.awt.*;
import javax.swing.*;
import java.applet.Applet;
public class DrawImage extends Applet {
Image chessboard;
public void init() {
chessboard = (new ImageIcon(DrawImage.class
.getResource("chessboard.JPG"))).getImage();
}
public void paint(Graphics g) {
g.drawImage(chessboard, 0, 0, this);
}
}
示例二
import java.awt.*;
import java.applet.Applet;
public class DrawImage extends Applet {
Image chessboard;
public void init() {
chessboard = getImage(getDocumentBase(), "chessboard.JPG");
}
public void paint(Graphics g) {
g.drawImage(chessboard, 0, 0, this);
}
}
示例三
import java.awt.*;
import java.applet.Applet;
public class DrawImage extends Applet {
Image chessboard;
public void init() {
chessboard = getImage(getCodeBase(), "chessboard.JPG");
}
public void paint(Graphics g) {
g.drawImage(chessboard, 0, 0, this);
}
}
注意:
JAVA源码打包成JAR后对大小写比较敏感,文件名和扩展名的大小写需与源文件严格保持一致,否则会找不会文件。示例一中若把"chessboard.JPG"改为"Chessboard.JPG"或"chessboard.jpg"之类,虽然在Eclipse等IDE中可以正确运行,但打包成JAR后却不能运行,在命令行用java -jar ***.jar会发现出现空指针的错误。
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/22520/showart_377407.html |
|