Java高级教程:显示图片

来源:互联网 发布:科比历年季后赛数据 编辑:程序博客网 时间:2024/04/30 01:39

applet能显示GIF,JPEG,BMP等其他格式的图片。为了在applet中显示图片,你需要使用java.awt.Graphics类的drawImage()方法。

  



如下实例演示了显示图片的所有步骤:
 

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
importjava.applet.*;
importjava.awt.*;
importjava.net.*;
publicclass ImageDemo extendsApplet
{
  privateImage image;
  privateAppletContext context;
  publicvoid init()
  {
      context = this.getAppletContext();
      String imageURL = this.getParameter("image");
      if(imageURL == null)
      {
         imageURL = "java.jpg";
      }
      try
      {
         URL url = newURL(this.getDocumentBase(), imageURL);
         image = context.getImage(url);
      }catch(MalformedURLException e)
      {
         e.printStackTrace();
         // Display in browser status bar
         context.showStatus("Could not load image!");
      }
   }
   publicvoid paint(Graphics g)
   {
      context.showStatus("Displaying image");
      g.drawImage(image, 0020084null);
      g.drawString("www.javalicense.com"35100);
   }
}






之前在部落QQ群:202250194    每天有大神更新最新的  资料 等等      每天解决各种奇葩问题  

0 0