编程第九十八、九十九天

显示图片

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

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

ImageDemo.java 文件代码:

import java . applet .*;
import java . awt .*;
import java . net .*;
public class ImageDemo extends Applet {
private Image image ;
private AppletContext context ;
public void init ( ) {
context = this . getAppletContext ( ) ;
String imageURL = this . getParameter ( " image " ) ;
if ( imageURL == null )
{
imageURL = " java.jpg " ;
}
try {
URL url = new URL ( this . getDocumentBase ( ) , imageURL ) ;
image = context . getImage ( url ) ;
} catch ( MalformedURLException e ) {
e . printStackTrace ( ) ; // Display in browser status bar
context . showStatus ( " Could not load image! " ) ;
}
}
public void paint ( Graphics g ) {
context . showStatus ( " Displaying image " ) ;
g . drawImage ( image , 0 , 0 , 200 , 84 , null ) ;
g . drawString ( " www.javalicense.com " , 35 , 100 ) ;
}
}


<html>
<title>The ImageDemo applet </ title >
< hr >
< applet code = " ImageDemo.class " width = " 300 " height = " 200 " >
< param name = " image " value = " java.jpg " >
</ applet >
< hr >
</ html >

猜你喜欢

转载自blog.csdn.net/imezreal/article/details/72858530