作业需求:http://blog.csdn.net/dyz1982/article/details/23155447
1:程序练手。教材P201 例9-13 缩放显示图像,先把代码敲入Eclipse,运行查看结果,理解代码意义;类名:ShowImg。然后尝试把Applet改写为Application,即含有main主方法的应用程序,类似于P199 例9-12;类名:ShowImgApp。
很简单 表示SoEasy
import java.awt.*;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
public class ShowImgApp extends JFrame{
static Image img1;
public ShowImgApp(){
//this.setSize(width, height)
this.setSize(300, 300);//设置大小
this.setVisible(true);//设置可见
this.setLocationRelativeTo(null);//设置位置
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//相应关闭
}
public static void main(String []args) throws IOException{
File Img = new File("img.gif");//读取照片文件
img1 = ImageIO.read(Img);//读取图片
ShowImgApp img = new ShowImgApp();//调用构造函数
//img1 = getImaget(getCodeBase(),"img.gif");
}
public void paint(Graphics g){
int w = img1.getWidth(this);//获取图片宽
int h = img1.getHeight(this);//获取图片高
//下面是在不同位置,以不同大小绘图
g.drawImage(img1,5,10,this );
g.drawImage(img1,150,40,w/2,h/2,this);
g.drawImage(img1,5,100,w*2,h*2,this);
}
}