练习60放大镜效果
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
练习60 放大镜效果
一、练习具体要求
本练习制作放大镜效果的实例。
如图60-1所示,程序执行后,创建一个能够加载图片的面板,用户可以利用鼠标对图片的细节进行放大观察。
放大镜的大小可以在magnifier.html 文件里通过参数进行设置。
二、程序及注释
(1)编程思路:首先,本练习因为要制作放大镜效果的实例,所以首先要生成程序界面:在函数public void init()中,首先通过语句getImage(getCodeBase(), getParameter("IMAGE")) 返回图片的路径,然后通过语句MediaTracker mediatracker = new MediaTracker(this)实例化MediaTracker对象,最后通过语句mediatracker.addImage(im, 0)加载图片。
然后,为了实现放大镜效果,所以要进行屏幕操作,首先通过函数public void preparepaint()做画屏准备工作,然后在public boolean mouseMove(Event event, int i, int j)函数里响应鼠标移动,通过调用函数preparepaint()和函数repaint()实现放大镜效果。
(2)程序实现及注释:
//magnifier.java
import java.applet.Applet;
import java.applet.AppletContext;
import java.awt.*;
import java.awt.image.MemoryImageSource;
import java.awt.image.PixelGrabber;
import java.io.PrintStream;
import .MalformedURLException;
import .URL;
public class magnifier extends Applet
{
//变量定义
Image resultimage = null;
Image im = null;
Image bgi = null;
int xfrom = 0;
int yfrom = 0;
int spot = 0;
int spot2 = 0;
PixelGrabber pg = null;
int pixs[] = null;
int pixr[] = null;
int w = 0;
int h = 0;
int spot2spot = 0;
MemoryImageSource ms = null;
Graphics bgg = null;
//初始化小程序
public void init()
{
im = getImage(getCodeBase(), getParameter("IMAGE"));
MediaTracker mediatracker = new MediaTracker(this);
mediatracker.addImage(im, 0);
showStatus("Loading image, please wait...");
try
{
mediatracker.waitForAll();
}
catch(InterruptedException interruptedexception)
{
System.out.println("InterruptedException:" + interruptedexception);
}
w = im.getWidth(this);
h = im.getHeight(this);
String s = getParameter("SIZE");
if(s != null)
try
{
spot = Integer.parseInt(s);
}
catch(NumberFormatException numberformatexception)
{
System.out.println("Check SIZE parameter, NumberFormatException " + numberformatexception);
}
spot2 = spot << 1;
spot2spot = spot * spot;
resultimage = createImage(spot2, spot2);
pixs = new int[w * h];
pixr = new int[spot2 * spot2];
pg = new PixelGrabber(im, 0, 0, w, h, pixs, 0, w);
try
{
pg.grabPixels();
}
catch(InterruptedException interruptedexception1)
{
System.out.println("InterruptedException in PixelGrabber"+ interruptedexception1);
}
ms = new MemoryImageSource(spot2, spot2, pixr, 0, spot2);
bgi = createImage(w, h);
bgg = bgi.getGraphics();
xfrom = (int)(Math.random() * (double)(w - spot2));
yfrom = (int)(Math.random() * (double)(w - spot2));
preparepaint();
}
//刷新屏幕
public void update(Graphics g)
{
paint(g);
}
//准备画屏函数
public void preparepaint()
{
if(xfrom < 0)
xfrom = 0;
if(xfrom > w - spot2)
xfrom = w - spot2;
if(yfrom < 0)
yfrom = 0;
if(yfrom > h - spot2)
yfrom = h - spot2;
for(int j = -spot; j < spot; j += 2)
{
int l = j * spot2 + spot + (spot2spot << 1);
int k = j * j;
int i1 = ((j >> 1) + spot + yfrom) * w + spot + xfrom;
int j1 = (int)Math.sqrt(spot2spot - k);
for(int i = -j1; i < j1; i += 2)
pixr[l + i + spot2] = pixr[l + i + 1 + spot2] = pixr[l + i] = pixr[l + i + 1] = pixs[i1 + (i >> 1)];
}
resultimage = createImage(ms);
}
//画屏函数
public void paint(Graphics g)
{
bgg.drawImage(im, 0, 0, this);
bgg.drawImage(resultimage, xfrom, yfrom, this);
g.drawImage(bgi, 0, 0, this);
}
//响应鼠标进入
public boolean mouseEnter(Event event, int i, int j)
{
showStatus(getParameter("LINK"));
return true;
}
//响应鼠标离开
public boolean mouseExit(Event event, int i, int j)
{
showStatus("");
return true;
}
//响应鼠标弹开
public boolean mouseUp(Event event, int i, int j)
{
String s = getParameter("LINK");
if(s != null)
try
{
URL url = new URL(getDocumentBase(), s);
getAppletContext().showDocument(url, "_self");
}
catch(MalformedURLException malformedurlexception)
{
System.out.println("Please, check LINK parameter"+ malformedurlexception);
}
return true;
}
//响应鼠标移动
public boolean mouseMove(Event event, int i, int j)
{
xfrom = i - spot;
yfrom = j - spot;
preparepaint();
repaint();
return true;
}
//构造函数
public magnifier()
{
spot = 30;
}
}
三、练习效果(如图60-1所示)
本实例代码编写完毕,存盘为:C: j2sdk1.4.0\javaprograms\ magnifier.java。
打开计算机的命令提示符窗口,然后在命令提示符窗口中,定位到javaprograms目录,输入javac magnifier.java 来编译程序,最后将程序加载到网页上。
在javaprograms文件夹下新建一个记事本文件,内容为
<HTML>
<HEAD><TITLE>放大镜效果</TITLE>
</HEAD><br><bR><Center>
<h2><font size="5">
<span style="mso-bidi-font-size: 12.0pt; mso-ascii-font-family: Arial;
mso-hansi-font-family: Arial">放大镜效果
</span>
</font>
<span lang="EN-US" style="font-size:14.0pt;mso-bidi-font-size:10.0pt"><o:p>
</o:p>
</span></h2>
<br><br>
<!--程序代码开始-->
<!--[Tip1]:"width"=显示的宽度;"height"=显示的高度-->
<APPLET CODE=magnifier.class WIDTH=400 HEIGHT=300
<!--[Tip2]:显示的图文件位置与名称--><PARAM NAME="IMAGE" VALUE="bhld.jpg">
<!--[Tip3]:图案链接的位置--><PARAM NAME="LINK" VALUE="链接文件.htm">
<!--[Tip4]:放大镜的大小,值越大放大镜越大-->
<PARAM NAME="SIZE" VALUE=20> </APPLET>
<!--程序代码到此结束--><br><br><br><hr width=60%>
</BODY></HTML>,然后保存为magnifier.html的网页文件。
本例制作完毕,用浏览器打开magnifier.html文件便可以看到制作效果。
图60-1 练习效果
四、总结提高
在本练习中,利用Java2的图像技术和消息响应机制完成了放大镜效果的制作。
通过图像技术,实现图片的载入。
通过响应鼠标动作,实现放大镜效果。