📄 ch14.htm
字号:
} // endif<BR> return false;<BR> }<BR>}</TT></BLOCKQUOTE><HR><H2><A NAME="TheMandelZoomAppClass"><FONT SIZE=5 COLOR=#FF0000>TheMandelZoomApp Class</FONT></A></H2><P>Listing 14.7 shows the MandelZoomApp class, which represents thischapter's main applet; its function was described earlier, inthe section "Using the Applets." See this section andTable 14.1 for how to use the applet.<P>The most interesting features in the code are the routines formarking the region to be highlighted. Each pixel on the displayedMandelbrot image maps an x-y value to a real-imaginary value ofthe <TT>c</TT> value of the Mandelbrotformula shown in Formula 14.1. Whenever you move the cursor, thecurrent real-imaginary values are shown in the browser's statusbar. When you highlight an area to zoom in on, you are reallypicking a range of <TT>c</TT> valuesto be explored. All the double variables are used for trackingthis range of values. These values are read in at initializationby the <TT>loadParameters()</TT> methodto match the bitmap that's displayed. You can specify other MandelbrotBMP files and corresponding data files by changing the <TT>filename</TT>parameter of the applet's <TT><APPLET></TT>tag.<P>The <TT>Zoom()</TT> method takes thecurrently highlighted range and brings up a new Mandelbrot imagethat corresponds to this range. It uses the same calculation-imagefiltering techniques that the MandelApp class does.<HR><BLOCKQUOTE><B>Listing 14.7. The MandelZoomApp class.<BR></B></BLOCKQUOTE><BLOCKQUOTE><TT>// This applet displays the Mandelbrotset bitmap specified<BR>// in the APPLET tag parameters. You can then zoomand in<BR>// and out of the bitmap by dragging a region to paint.<BR>// And then clicking on the appropriate option...<BR>// Z or z - Zoom<BR>// S or s - Save.<BR>public class MandelZoomApp extends Applet {<BR> Image img;<BR> boolean zoomOn = false;<BR> double XLeft,XRight,YTop,YBottom,XDelta,YDelta;<BR> double currentX,currentY;<BR> double startX,startY,endX,endY; //Zooming coordinates...<BR> Rectangle markingRectangle; // Zoomingrectangle...<BR> Mandelbrot m; // Creates the Mandelbrot image...<BR> int NUMCOLS = 640; // Dimensionsimage display...<BR> int NUMROWS = 350;<BR> boolean complete = false;<BR> // Array for keeping track of Mandelbrot entries...<BR> MandelEntry me[];<BR> int lastIndex; // Top of array...<BR> int currentIndex;<BR><BR> // Set up the Mandelbrot set specified in theparameters...<BR> public void init() {<BR> img = null;<BR> m = null;<BR> // Get parameter of bitmapto display...<BR> String filename;<BR> if ((filename = getParameter("filename"))== null)<BR> filename= "mandel1";<BR> // Load the bitmap...<BR> loadBitmap(filename);<BR> // Initialize Mandelbrot array...<BR> me = new MandelEntry[40];<BR> me[0] = new MandelEntry(null,img,XLeft,XRight,YTop,YBottom);<BR> lastIndex = 0;<BR> currentIndex = 0;<BR> }<BR><BR><BR> // ZOOM onto Mandelbrot set if all is good...<BR> void Zoom() {<BR> // No Zooming if off or norectangle...<BR> if ((!zoomOn) || (markingRectangle== null)) {<BR> showMsg("Nothingmarked or Zooming disable...");<BR> return;<BR> } // end if<BR><BR> // See if Mandelbrot tableis full...<BR> if ((lastIndex + 1) >=me.length) {<BR> showMsg("Mandelbrottable full. Clear with C before zooming");<BR> return;<BR> }<BR> showMsg("ZOOM: SX="+ startX + " SY=" + startY + " EX=" + endX+ " EY=" + endY);<BR> // Load new Mandelbrot...<BR> complete = false;<BR> zoomOn = false;<BR> markingRectangle = null; //Reset marking rectangle...<BR> m = new Mandelbrot(NUMCOLS,NUMROWS,endX,startX,<BR> endY,startY);<BR> img = m.getImage();<BR> // Store in Mandelbrot table...<BR> XLeft = startX;<BR> XRight = endX;<BR> YTop = startY;<BR> YBottom = endY;<BR> XDelta = Math.abs(XRight -XLeft);<BR> YDelta = Math.abs(YBottom- YTop);<BR> ++lastIndex;<BR> me[lastIndex] = new MandelEntry(m,img,startX,endX,startY,endY);<BR> currentIndex = lastIndex;<BR> showMsg("Calculating...");<BR> repaint();<BR> }<BR><BR> // Paint on update...<BR> public void update(Graphics g) {<BR> paint(g);<BR> }<BR> public synchronized void paint(Graphics g) {<BR> if (img == null)<BR> return;<BR> // Show image...<BR> g.drawImage(img,0,0,this);<BR> // Show marking rectangleif exists...<BR> if (markingRectangle != null){<BR> g.drawRect(markingRectangle.x,markingRectangle.y,<BR> markingRectangle.width,markingRectangle.height);<BR> } // end if<BR> }<BR><BR> // Will get updates as set is being created.<BR> // Repaint when they occur...<BR> public boolean imageUpdate(Image im,int flags,<BR> int x, int y, int w, int h){<BR> if ((flags & FRAMEBITS)!= 0) {<BR> repaint();<BR> return true;<BR> }<BR> if ((flags & ALLBITS)!= 0) {<BR> showMsg("ImageComplete!");<BR> repaint();<BR> complete= true;<BR> zoomOn =true;<BR> return false;<BR> }<BR> return true;<BR> }<BR><BR> // Load a bitmap and accompanying data file...<BR> void loadBitmap(String filename) {<BR> // Zoom is false unless bothsucceed...<BR> zoomOn = false;<BR> markingRectangle = null; //Reset marking rectangle...<BR> // Load the bitmap...<BR> try {<BR> showMsg("Loadimage...");<BR> ImageProducerproducer = BmpImage.getImageProducer(<BR> getDocumentBase(),filename + ".bmp");<BR> img = createImage(producer);<BR> showMsg("Imageloaded...");<BR> }<BR> catch (AWTException e){<BR> img = null;<BR> showMsg("Cannotopen file " + filename);<BR> return;<BR> }<BR><BR> // Load the zoom parameters.<BR> // Turn Zoom on if all works...<BR> try {<BR> loadParameters(filename);<BR> zoomOn =true;<BR> complete= true;<BR> }<BR> catch (IOException e){<BR> showMsg("Cannotload parameter data. " + e.getMessage());<BR> }<BR> }<BR><BR> // Load the parameters. Throw IOException...<BR> public void loadParameters(String filename)throws IOException {<BR> // Create URL for data...<BR> URL u;<BR> try {<BR> u = new URL(getDocumentBase(),filename+ ".dat");<BR> }<BR> catch (MalformedURLException e) {<BR> showMsg("Bad Data URL");<BR> throw new IOException("BadURL");<BR> }<BR> // Now load the data by opening up a stream<BR> // to the URL...<BR> DataInputStream dis = new DataInputStream(<BR> new BufferedInputStream(u.openStream()) );<BR> // Read only the first line...<BR> String param = dis.readLine();<BR> // Tokenize out the boundary values....<BR> StringTokenizer s = new StringTokenizer(param,",");<BR> try {<BR> XLeft = Double.valueOf(s.nextToken()).doubleValue();<BR> XRight = Double.valueOf(s.nextToken()).doubleValue();<BR> YTop = Double.valueOf(s.nextToken()).doubleValue();<BR> YBottom = Double.valueOf(s.nextToken()).doubleValue();<BR> XDelta = Math.abs(XRight- XLeft);<BR> &n
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -