在Applet和Application中加载图片
2011-09-16 16:13 Rollen Holt 阅读(1045) 评论(0) 编辑 收藏 举报先看看在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; }
==============================================================================
本博客已经废弃,不在维护。新博客地址:http://wenchao.ren
我喜欢程序员,他们单纯、固执、容易体会到成就感;面对压力,能够挑灯夜战不眠不休;面对困难,能够迎难而上挑战自我。他
们也会感到困惑与傍徨,但每个程序员的心中都有一个比尔盖茨或是乔布斯的梦想“用智慧开创属于自己的事业”。我想说的是,其
实我是一个程序员
==============================================================================