📄 ch14.htm
字号:
ImagineMax = 1.20;<BR> ImagineMin = -1.20;<BR> }<BR><BR> // Create zoom of Mandelbrot set<BR> public Mandelbrot(int width,int height,doubleRealMax,double RealMin,<BR> double ImagineMax,double ImagineMin) {<BR> this.width = width;<BR> this.height = height;<BR> this.RealMax = RealMax; //Default starting sizes...<BR> this.RealMin = RealMin;<BR> this.ImagineMax = ImagineMax;<BR> this.ImagineMin = ImagineMin;<BR> }<BR><BR> // Start producing the Mandelbrot set...<BR> public Image getImage() {<BR> img = new CalculatorImage(width,height,this);<BR> return img.getImage();<BR> }<BR><BR> // Start thread to produce data...<BR> public void start(int pix[],CalculateFilterNotifyfilter) {<BR> this.pix = pix;<BR> this.filter = filter;<BR> new Thread(this).start();<BR> }<BR><BR> // See if user wants to stop before completion...<BR> public void stop() {<BR> synchronized (stopCalc) {<BR> stopCalc= Boolean.TRUE;<BR> }<BR> System.out.println("GOTSTOP!");<BR> }<BR><BR> // Create data here...<BR> public void run() {<BR> // Establish Mandelbrot parameters...<BR> double Q[] = new double[height];<BR> // Pixdata is for image filterupdates...<BR> int pixdata[] = new int[height];<BR> double P,diffP,diffQ, x, y,x2, y2;<BR> int color, row, column,index;<BR><BR> System.out.println("RealMax= " + RealMax + " RealMin = " + RealMin +<BR> "ImagineMax = " + ImagineMax + " ImagineMin = "+ ImagineMin);<BR> // Setup calculation parameters...<BR> diffP = (RealMax - RealMin)/(width);<BR> diffQ = (ImagineMax - ImagineMin)/(height);<BR> Q[0] = ImagineMax;<BR> color = 0;<BR><BR> // Setup delta parameters...<BR> for (row = 1; row < height;row++)<BR> Q[row] =Q[row-1] - diffQ;<BR> P = RealMin;<BR><BR> // Start calculating!<BR> for (column = 0; column <width; column++) {<BR> for (row= 0; row < height; row++) {<BR> x= y = x2 = y2 = 0.0;<BR> color= 1;<BR> while((color < maxIterations) &&<BR> ((x2+ y2) < maxSize)) {<BR> x2= x * x;<BR> y2= y * y;<BR> y= (2*x*y) + Q[row];<BR> x= x2 - y2 + P;<BR> ++color;<BR> }<BR> //plot...<BR> index= (row * width) + column;<BR> pix[index]= (int)(color % numColors);<BR> pixdata[row]= pix[index];<BR> } // endrow<BR> // Updatecolumn after each iteration...<BR> filter.dataUpdateColumn(column,pixdata);<BR> P += diffP;<BR> // See ifwe were told to stop...<BR> synchronized(stopCalc) {<BR> if(stopCalc == Boolean.TRUE) {<BR> column= width;<BR> System.out.println("RUN:Got stop calc!");<BR> }<BR> } //end sync<BR> } // end col<BR><BR> // Tell filter that we'redone producing data...<BR> System.out.println("FILTER:Data Complete!");<BR> filter.setComplete();<BR> }<BR><BR> // Save the Mandelbrot set as a BMP file...<BR> public void saveBMP(String filename) {<BR> img.saveBMP(filename,pix);<BR> }<BR>}</TT></BLOCKQUOTE><HR><H2><A NAME="CalculateFilterNotifyInterface"><FONT SIZE=5 COLOR=#FF0000>CalculateFilterNotifyInterface</FONT></A></H2><P>The CalculateFilterNotify interface defines the methods neededto update an image filter that works with a calculation thread.As shown in Listing 14.2, the "data" methods are usedfor conveying a new batch of data to the filter. The <TT>setComplete()</TT>method indicates that the calculations are complete.<HR><BLOCKQUOTE><B>Listing 14.2. The CalculateFilterNotify interface.<BR></B></BLOCKQUOTE><BLOCKQUOTE><TT>/* Interface for defining methods forupdating a<BR> Calculator Filter... */<BR>public interface CalculateFilterNotify {<BR> public void dataUpdate(); //Update everything...<BR> public void dataUpdateRow(int row); // Updateone row...<BR> public void dataUpdateColumn(int col,int pixdata[]); //Update one column...<BR> public void setComplete();<BR>}</TT></BLOCKQUOTE><HR><H2><A NAME="CalculatorProducerInterface"><FONT SIZE=5 COLOR=#FF0000>CalculatorProducerInterface</FONT></A></H2><P>The CalculatorProducer interface, as shown in Listing 14.3, definesthe method called when a calculation filter is ready to kick offa thread that produces the data used to generate an image. TheCalculateFilterNotify object passed to the <TT>start()</TT>method is called by the producer whenever new data is yielded.<HR><BLOCKQUOTE><B>Listing 14.3. The CalculatorProducer interface.<BR></B></BLOCKQUOTE><BLOCKQUOTE><TT>// Interface for a large calculationto produce image...<BR>interface CalculatorProducer {<BR> public void start(int pix[],CalculateFilterNotifycf);<BR>}</TT></BLOCKQUOTE><HR><H2><A NAME="TheCalculatorFilterClass"><FONT SIZE=5 COLOR=#FF0000>TheCalculatorFilter Class</FONT></A></H2><P>The CalculatorFilter class in Listing 14.4 is a subclass of ImageFilter.Its purpose is to receive image data produced by some long calculation(like the Mandelbrot set) and update any consumer of the the newdata's image. The CalculatorProducer, indicated by variable <TT>cp</TT>,is what produces the data.<P>Since the ImageFilter class was explained in detail in Part III,issues related to this class are not repeated here. However, acouple of things should be pointed out. When the image is firstrequested, the filter gets the dimensions the consumer wants bya call of the <TT>setDimensions()</TT>method. At this point, the CalculatorFilter will allocate a largearray holding the color values for each pixel.<P>When the original ImageProducer is finished creating the originalimage, the filter's <TT>imageComplete()</TT>method will be called, but the filter needs to override this method.In this case, the CalculatorFilter will start the CalculatorProducerthread, passing it the pixel array to put in its updates. Wheneverthe CalculatorProducer has new data, it will call one of the fourmethods specified by the CalculateFilterNotify interface: <TT>dataUpdate()</TT>,<TT>dataUpdateRow()</TT>, <TT>dataUpdateColumn()</TT>,or <TT>setComplete()</TT>. (The <TT>dataUpdateColumn()</TT>method is called by the Mandelbrot calculation since it operateson a column basis.) In each of these cases, the filter updatesthe appropriate consumer pixels by using the <TT>setPixels()</TT>method, then calls the consumer's <TT>imageComplete()</TT>method to indicate the nature of the change. For the three "data"methods, the updates are only partial, so a SINGLEFRAMEDONE flagis sent. The <TT>setComplete()</TT>method, on the other hand, indicates that everything is complete,so it sets a STATICIMAGEDONE flag.<HR><BLOCKQUOTE><B>Listing 14.4. The CalculatorFilter class.<BR></B></BLOCKQUOTE><BLOCKQUOTE><TT>import java.awt.image.*;<BR>import java.awt.Image;<BR>import java.awt.Toolkit;<BR>import java.lang.*;<BR><BR>public class CalculatorFilter extends ImageFilter<BR> implements CalculateFilterNotify {<BR> private ColorModel defaultRGBModel;<BR> private int width, height;<BR> private int pix[];<BR> private boolean complete = false;<BR> private CalculatorProducer cp;<BR> private boolean cpStart = false;<BR><BR> public CalculatorFilter(ColorModel cm,CalculatorProducercp) {<BR> defaultRGBModel = cm;<BR> this.cp = cp;<BR> }<BR><BR> public void setDimensions(int width, int height){<BR> this.width = width;<BR> this.height = height;<BR> pix = new int[width * height];<BR> consumer.setDimensions(width,height);<BR> }<BR><BR> public void setColorModel(ColorModel model){<BR> consumer.setColorModel(defaultRGBModel);<BR> }<BR><BR> public void setHints(int hints) {<BR> consumer.setHints(ImageConsumer.RANDOMPIXELORDER);<BR> }<BR><BR> public void resendTopDownLeftRight(ImageProducerp) {<BR> }<BR><BR><BR> public void setPixels(int x, int y, int w, inth,<BR> ColorModel model, int pixels[],intoff,int scansize) {<BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -