java图像文件的显示
titook的getimage方法只是将图像加载进来,交给image对象进行管理,至于显示的话则是通过paint方法的graphic对象的drawimage方法
代码示例
package jj;
import java.awt.Graphics;
import java.awt.Image;
import java.io.Console;import javax.swing.JFrame;
public class imageshow extends JFrame{
String filename;
public imageshow(String filename) {
setSize(570,350);
setVisible(true);
this.filename=filename;
// TODO Auto-generated constructor stub
}
@Override
public void paint(Graphics g) {
// TODO Auto-generated method stub
super.paint(g);
Image image=getToolkit().getImage(filename);
int w=image.getWidth(this);
int h=image.getHeight(this);
// System.out.println(h);
// g.drawImage(image, 20,80,this);
g.drawImage(image, 200,80,w/2,h/2,this);
// g.drawImage(image, 20, 50, w*2, h*2, this);
}public static void main(String[] args) {
// TODO Auto-generated method stub
new imageshow("D:\\programing\\jj\\src\\21.png");}
}