下面是一个Applet程序,程序的功能输出一个彩色窗体。请改正程序中的错误(有下划线的语句),使程序能输出正确结果。
注意:不改动程序的结构,不得增行或删行。
程序的输出结果为:
import java.awt.*;
import java.awt.image.*;
import java.applet.Applet;
public class MemoryImage extends Applet
Image img;
public void init()
Dimension d=getSize();
int w=d.width,h=d.height;
int pix[ ]=new int[w*h];
int index=0;
for(int y=0;y<h;y++)
for(int x=0;x<w;x++)
int red=(x|y)&&0xff;
int green=(x*2|y*2)&0xff;
int blue=(x*4|y*4)&0xff;
pix[index++]=(255<<24)|(red<<16)|(green<<8)|blue;
img=createImage(new FilteredImageSource(w,h,pix, 0,w));
public void paint(Graphics g)
g.drawImage(img, 0,0,super);
ex36_3.html:
<html>
<head>
<title>A Simple Program</title>
</head>
<body>
<applet code=" MemoryImage.class" width=800 height=400>
</applet>
</body>
</html>
参考答案:
int red=(x|y)&0xff;
img=createImage(new MemoryImageSource(w,h,pix,0,w));
g.drawImage(img,0,0,this);
解析:
本题主要考查Java Applet程序的设计。解答本题的关键是掌握MemoryImageSource(int width,int height,int pixel[],int offset,int scanLineWidth)方法的使用;其中width和height指明图像的大小,pixel中包含每个像素点的值,scanLineWidth指明图像中每行的像素数。在本题中,int red=(x|y)&0xff;语句的功能是定义变量red使其值为(x|y)&0xff,img=createImage(new MemoryImageSource(w,h,pix,0,w));语句的功能是生成图像,g.drawImage(img,0,0,this);语句的功能是在指定的Applet显示区内画出图像。