在Applet中显示图片

//DisplayImage.java
//显示图片,在Graphics类drawImage方法可显示图像,但需要Image对象作参数
//Applet类 getImage方法可得Image对象,但需URL对象作参数。
//drawImage(Image对象<======getImage(URL对象<=======URL类))

//在D:/java/下有displayImage图片,没有可变更路径。


//<applet code=DisplayImage width=600 height=700>
//</applet>


import java.awt.*;
import java.applet.*;
import java.net.*;
import java.applet.Applet;

public class DisplayImage extends Applet
{
 public void paint(Graphics g)
 { 
  try
  {
   URL pairURL=new URL("file:/d:/java/displayimage.jpg");//注意URL的写法。
   Image myImage=getImage(pairURL);
   int w=myImage.getWidth(this);
   //获取图片宽度,这个this不了解?ImageObserve 应该是一个通知类接口啊,
   //为什么要用this呢?

   int h=myImage.getHeight(this);//获取图片高度。
   g.drawString("The width of the image is= "+w+"The height is= "+h,10,15);
   g.drawString("get File= "+pairURL.getFile(),10,30);//得到URL的地址。
   g.drawImage(myImage,w,h,this);   //this<===ImageObserve  ????
  }
  catch (MalformedURLException e)
  {
   g.drawString("Image not found",30,30);
  }

 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值