第6周作业-图像缩放显示

图像缩放显示   Application版 和 Applet 版!!

/**    
 * 功能:缩放显示图像gif
 * 著:书中[例9-13]  P201 (Applet版)   
 * 作者:李家华   
 * 时间:2014-04-08    
 * 博客:blog.csdn.net/lhcapricorn  
 * */  

import java.awt.Graphics;
import java.awt.Image;
import javax.swing.*;

public class ShowImg extends JApplet{
	Image img1;
	public void init(){
		img1 = getImage(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,15,40,w/2,h/2,this);
		g.drawImage(img1, 5, 100, w*2,h*2,this);
	}

}
第6周作业-图像缩放显示_第1张图片


/**    
 * 功能:缩放显示图像gif
 * 说明:Application版 
 * 作者:李家华   
 * 时间:2014-04-08    
 * 博客:blog.csdn.net/lhcapricorn  
 * */  

import java.awt.Container;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import javax.swing.*;

public class ShowImgApp {
	// static Image img1;  
	public static void main(String arg[]){
		ImageFrame frame = new ImageFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
		frame.setLocationRelativeTo(null);
	}
}

class ImageFrame extends JFrame{
	public ImageFrame(){
		setSize(WIDTH,HEIGHT);
		setTitle("缩放显示图像");
		//JPanel panel = new JPanel();
		//this.add(panel);
		ImagePanel panel = new ImagePanel();
		Container contentPane =getContentPane();
		contentPane.add(panel);
	}
	public static final int WIDTH=400;
	public static final int HEIGHT=600;

	class ImagePanel extends JPanel{
		public ImagePanel(){
			image = Toolkit.getDefaultToolkit().getImage("img200.gif");
			MediaTracker tracker = new MediaTracker(this);
			tracker.addImage(image,1);
			try {
				tracker.waitForID(1);
			}catch (InterruptedException exception){}
		}
		public void paintComponent(Graphics g){
			super.paintComponent(g);
			int w = image.getWidth(this);
			int h = image.getHeight(this);
			g.drawImage(image,5,10,null );  
			g.drawImage(image,150,40,w/2,h/2,this);  
			g.drawImage(image,10,200,w*2,h*2,this);  
		}
		private Image image;
	}

}

第6周作业-图像缩放显示_第2张图片

你可能感兴趣的:(代码)