个性化阅读
专注于IT技术分析

在小程序中显示图像

点击下载

本文概述

Applet主要用于游戏和动画。为此, 需要显示图像。 java.awt.Graphics类提供了drawImage()方法来显示图像。

drawImage()方法的语法

public abstract boolean drawImage(Image img, int x, int y, ImageObserver观察器):用于绘制指定的图像。

如何获得图像的对象

java.applet.Applet类提供getImage()方法, 该方法返回Image的对象。句法:
public Image getImage(URL u, String image){}

Applet类显示图像所需的其他方法

public URL getDocumentBase():用于返回嵌入了applet的文档的URL。 public URL getCodeBase():用于返回基本URL。

在applet中显示图像的示例

import java.awt.*;
import java.applet.*;


public class DisplayImage extends Applet {

  Image picture;

  public void init() {
    picture = getImage(getDocumentBase(), "sonoo.jpg");
  }
  
  public void paint(Graphics g) {
    g.drawImage(picture, 30, 30, this);
  }
    
  }
在上面的示例中, Graphics类的drawImage()方法用于显示图像。 drawImage()方法的第四个参数是ImageObserver对象。 Component类实现ImageObserver接口。因此, 当前类对象也将被视为ImageObserver, 因为Applet类间接扩展了Component类。

myapplet.html

<html>
<body>
<applet code="DisplayImage.class" width="300" height="300">
</applet>
</body>
</html>
赞(0)
未经允许不得转载:srcmini » 在小程序中显示图像

评论 抢沙发

评论前必须登录!