AWT 中加载和显示图像的方法[zz]

Java 提供大量加载和显示图像的方法,可以使用 ImageIO,Jimi 或 JAI 中提供的一些 API,在这里简单地说几种 AWT 中加载和显示图像的方法。

1、使用 java.applet.Applet 提供的 getImage() 方法

abstract  ImagegetImage(String filename)
          Returns an image which gets pixel data from the specified file, whose format can be either GIF, JPEG or PNG.
abstract  ImagegetImage(URL url)
          Returns an image which gets pixel data from the specified URL.

import java.net.URL;
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;

public   class  ImageTestAppletSimple
    extends Applet 
{
  
private Image im;

  
public void init() {
    
// 指定这个 applet 位置的 URL
    URL codebase = getCodeBase();

    im 
= getImage(codebase, "lena.jpg");
  }


  
public void paint(Graphics g) {
    g.drawImage(im, 
00this);
  }

}


2、使用 java.awt.Toolkit 提供的 getImage() 方法
abstract  ImagegetImage(String filename)
          Returns an image which gets pixel data from the specified file, whose format can be either GIF, JPEG or PNG.
abstract  ImagegetImage(URL url)
          Returns an image which gets pixel data from the specified URL.

import java.awt. * ;
import java.awt.
event . * ;

public   class  ImageTestApplication
    extends Frame 
{
  Insets insets;
  Image im;

  
static public void main(String args[]) {
    ImageTestApplication app 
= new ImageTestApplication();
    app.show();
  }


  
public ImageTestApplication() {
    super(
"Image Test");
    im 
= Toolkit.getDefaultToolkit().getImage("lena.jpg");

    addWindowListener(
new WindowAdapter() {
      
public void windowClosing(WindowEvent event{
        dispose();
        System.exit(
0);
      }

    }
);
  }


  
public void addNotify() {
    super.addNotify();
    insets 
= getInsets();
    setBounds(
100100300 + insets.left, 300 + insets.top);
  }


  
public void paint(Graphics g) {
    g.drawImage(im, insets.left, insets.top, 
this);
  }

}


3、将图像作为资源加载

·java.lang.Class 提供的下面2个方法加载资源

URLgetResource(String name)
          Finds a resource with a given name.
 InputStreamgetResourceAsStream(String name)
          Finds a resource with a given name.

·java.awt.Component 中提供方法来创建图像

ImagecreateImage(ImageProducer producer)
          Creates an image from the specified image producer.
 ImagecreateImage(int width, int height)
          Creates an off-screen drawable image to be used for double buffering.

·java.awt.Toolkit 中提供方法来创建图像

 ImagecreateImage(byte[] imagedata)
          Creates an image which decodes the image stored in the specified byte array.
abstract  ImagecreateImage(byte[] imagedata, int imageoffset, int imagelength)
          Creates an image which decodes the image stored in the specified byte array, and at the specified offset and length.
abstract  ImagecreateImage(ImageProducer producer)
          Creates an image with the specified image producer.
abstract  ImagecreateImage(String filename)
          Returns an image which gets pixel data from the specified file.
abstract  ImagecreateImage(URL url)
          Returns an image

 

import java.applet.Applet;
import java.net.URL;
import java.awt.
* ;
import java.awt.image.ImageProducer;
import java.awt.
event . * ;

public   class  Test
    extends Applet 
{
  Image im;

  
public void start() {
    URL url 
= getClass().getResource("lena.jpg");

    
try {
      im 
= createImage( (ImageProducer) url.getContent());

      
if (im == null{
        System.
out.println("null image");
      }


    }

    
catch (Exception e) {
      e.printStackTrace();
    }

  }


  
public void paint(Graphics g) {
    Insets insets 
= getInsets();

    g.drawImage(im, insets.left, insets.top, 
this);
  }


  
public void update(Graphics g) {
    paint(g);
  }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值