作業需求: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);
}
}