• 在Applet和Application中加载图片


    先看看在Applet中加载图片把:

    package Pictures;
    
    /**
     * 在Applet中加载图片
     * */
    import java.awt.Graphics;
    import java.awt.Image;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    import javax.swing.JApplet;
    
    public class JAppletDemo extends JApplet{
    	@Override
    	public void init(){
    		try{
    			img = getImage(new URL(
    					"http://tp2.sinaimg.cn/1882500857/180/5609472576/1"),
    					"rollen");
    		}catch(MalformedURLException e){
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		height = img.getHeight(this);
    		weight = img.getWidth(this);
    	}
    
    	@Override
    	public void paint(Graphics g){
    		super.paint(g);
    		g.drawImage(img, 0, 0, weight, height, this);
    	}
    
    	private Image img;
    	int height;
    	int weight;
    }
    

     然后在Application中加载图片

    package Pictures;
    
    /**
     * 在Application中加载图片
     * */
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.Toolkit;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    import javax.swing.JFrame;
    
    public class ApplicationPicture extends JFrame{
    	public ApplicationPicture(){
    		Toolkit toolkit = Toolkit.getDefaultToolkit();
    		try{
    			img = toolkit.getImage(new URL(
    					"http://tp2.sinaimg.cn/1882500857/180/5609472576/1"));
    		}catch(MalformedURLException e){
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		validate();
    		setSize(100, 100);
    		setVisible(true);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
    
    	@Override
    	public void paint(Graphics g){
    		super.paint(g);
    		g.drawImage(img, 0, 0, this);
    	}
    
    	public static void main(String[] args){
    		new ApplicationPicture();
    	}
    
    	private Image img;
    }
    

      

  • 相关阅读:
    四则运算
    3.12----对potplayer的使用评价
    对软件工程的一点思考
    个人附加作业
    附加题
    个人最终总结
    结对编程总结
    修改后的四则运算
    阅读程序回答问题
    Visual studio 2013的安装和单元测试
  • 原文地址:https://www.cnblogs.com/rollenholt/p/2178884.html
Copyright © 2020-2023  润新知