
案例三:放大镜 设计要求: 能对指定图片进行局部放大,鼠标所经过的地方能显示放大效果。要放大的图片由网页 传递,放大的倍数也由网页传递。 实例效果 [查看源文件] import java.applet.Applet; import java.applet.AppletContext; import java.awt.*; import java.awt.image.MemorylmageSource; import java.awt.image.PixelGrabber; import java.io.PrintStream; import java.net.MalformedURLException; import java.net.URL; public class magnifier extends Applet { Image resultimage; Image im; Image clipimage; Image bgi; int xfrom; int yfrom; int spot; int spot2; PixelGrabber pg; int pixs[; int pixr int w;
案例三:放大镜 设计要求: 能对指定图片进行局部放大,鼠标所经过的地方能显示放大效果。要放大的图片由网页 传递,放大的倍数也由网页传递。 实例效果 [查看源文件] 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 java.net.MalformedURLException; import java.net.URL; public class magnifier extends Applet { Image resultimage; Image im; Image clipimage; Image bgi; int xfrom; int yfrom; int spot; int spot2; PixelGrabber pg; int pixs[]; int pixr[]; int w;

int h; int dx; int dy; int delay; int spot2spot; MemorylmageSource ms; Graphics bgg; int x1; int y1; int x2; int y2; double xx; double yy; double dxf; double dyf; public void init() { im getlmage(getCodeBase(),getParameter("IMAGE")) MediaTracker mediatracker new MediaTracker(this); mediatracker.addlmage(im,0); showStatus("Loading image,please wait..."); try { mediatracker.waitForAll(); } catch(InterruptedException interruptedexception) System.out.println("InterruptedException:"+interruptedexception); w=im.getWidth(this);
int h; int dx; int dy; int delay; int spot2spot; MemoryImageSource ms; Graphics bgg; int x1; int y1; int x2; int y2; double xx; double yy; double dxf; double dyf; 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.parselnt(s); catch(NumberFormatException numberformatexception) { System.out.println("Check SIZE parameter,NumberFormatException"+ numberformatexception); } spot2 spot <1; spot2spot spot spot; clipimage createlmage(spot2,spot2); resultimage createlmage(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.printIn("InterruptedException in PixelGrabber "interruptedexception1); } ms new MemorylmageSource(spot2,spot2,pixr,0,spot2); bgi=createlmage(w,h); bgg bgi.getGraphics();
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; clipimage = createImage(spot2, spot2); 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)); xx xfrom; yy yfrom; preparepaint(); } public void update(Graphics g) { paint(g); } public void preparepaint() 人 if(xfrom >1)+spot yfrom)*w+spot +xfrom; int j1 =(int)Math.sqrt(spot2spot-k); for(int i=-1;i>1)]; resultimage=createlmage(ms);
xfrom = (int)(Math.random() * (double)(w -spot2)); yfrom = (int)(Math.random() * (double)(w - spot2)); xx = xfrom; yy = yfrom; preparepaint(); } public void update(Graphics g) { paint(g); } public void preparepaint() { if(xfrom w - spot2) xfrom = w - spot2; if(yfrom h - spot2) yfrom = h -spot2; for(int j = -spot; j > 1) + spot + yfrom) * w + spot + xfrom; int j1 = (int)Math.sqrt(spot2spot - k); for(int i = -j1; i > 1)]; } resultimage = createImage(ms);

} public void paint(Graphics g) { bgg.drawlmage(im,0,0,this); bgg.drawlmage(resultimage,xfrom,yfrom,this); g.drawlmage(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.printIn("Please,check LINK parameter"+malformedurlexception);
} 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; dy=2; dy=2; delay=200; } }
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; dx = 2; dy = 2; delay = 200; } }