⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch14.htm

📁 JAVA Developing Professional JavaApplets
💻 HTM
📖 第 1 页 / 共 5 页
字号:
&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;public void imageComplete(int status) {<BR>&nbsp;&nbsp;&nbsp;&nbsp; if (!cpStart) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cpStart = true;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dataUpdate();&nbsp;&nbsp;//Show empty pixels...<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cp.start(pix,this);<BR>&nbsp;&nbsp;&nbsp;&nbsp; } // end if<BR>&nbsp;&nbsp;&nbsp;&nbsp; if (complete)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;consumer.imageComplete(ImageConsumer.STATICIMAGEDONE);<BR>&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;// Called externally to notify that more datahas been created<BR>&nbsp;&nbsp;&nbsp;// Notify consumer so they can repaint...<BR>&nbsp;&nbsp;&nbsp;public void dataUpdate() {<BR>&nbsp;&nbsp;&nbsp;&nbsp; consumer.setPixels(0,0,width,height,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;defaultRGBModel,pix,0,width);<BR>&nbsp;&nbsp;&nbsp;&nbsp; consumer.imageComplete(ImageConsumer.SINGLEFRAMEDONE);<BR>&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;// External call to update a specific pixelrow...<BR>&nbsp;&nbsp;&nbsp;public void dataUpdateRow(int row) {<BR>&nbsp;&nbsp;&nbsp;&nbsp; // The key thing here is the second tolast parameter (offset)<BR>&nbsp;&nbsp;&nbsp;&nbsp; // which states where to start gettingdata from the pix array...<BR>&nbsp;&nbsp;&nbsp;&nbsp; consumer.setPixels(0,row,width,1,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;defaultRGBModel,pix,(width* row),width);<BR>&nbsp;&nbsp;&nbsp;&nbsp; consumer.imageComplete(ImageConsumer.SINGLEFRAMEDONE);<BR>&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;// External call to update a specific pixelcolumn...<BR>&nbsp;&nbsp;&nbsp;public void dataUpdateColumn(int col,int pixdata[]){<BR>&nbsp;&nbsp;&nbsp;&nbsp; // The key thing here is the second tolast parameter (offset)<BR>&nbsp;&nbsp;&nbsp;&nbsp; // which states where to start gettingdata from the pix array...<BR>&nbsp;&nbsp;&nbsp;&nbsp; consumer.setPixels(col,0,1,height,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;defaultRGBModel,pixdata,0,1);<BR>&nbsp;&nbsp;&nbsp;&nbsp; consumer.imageComplete(ImageConsumer.SINGLEFRAMEDONE);<BR>&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;// Called from external calculating programwhen data has<BR>&nbsp;&nbsp;&nbsp;// finished being calculated...<BR>&nbsp;&nbsp;&nbsp;public void setComplete() {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;complete = true;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;consumer.setPixels(0,0,width,height,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;defaultRGBModel,pix,0,width);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;consumer.imageComplete(ImageConsumer.STATICIMAGEDONE);<BR>&nbsp;&nbsp;&nbsp;}<BR>}</TT></BLOCKQUOTE><HR><H2><A NAME="TheCalculatorImageClass"><FONT SIZE=5 COLOR=#FF0000>TheCalculatorImage Class</FONT></A></H2><P>The CalculatorImage class, shown in Listing 14.5, is the gluebetween the CalculatorProducer class that produces the image dataand the CalculatorFilter that manages it. When an image is requestedwith the <TT>getImage()</TT> method,the CalculatorImage creates a color palette through an instanceof the ImageColorModel class, then creates a MemoryImageSourceobject. This ImageProducer object produces an image initializedto all zeros (black). It is combined with an instance of the CalculatorFilterclass to produce a FilteredImageSource. When the <TT>createImage()</TT>method of the Toolkit is called, production of the calculatedimage begins.<P>The color palette is a randomly generated series of pixel values.Depending on your luck, these colors can be attractive or uninspiring.The <TT>createPalette()</TT> methodis a good place to create a custom set of colors for this applet,if you want to have some control over its appearance. You shouldreplace the random colors with hard-coded RGB values, and youmight want to download a URL file that specifies a special colormapping.<HR><BLOCKQUOTE><B>Listing 14.5. The CalculatorImage class.<BR></B></BLOCKQUOTE><BLOCKQUOTE><TT>// This class takes a CalculatorProducerand sets up the<BR>// environment for creating a calculated image.&nbsp;&nbsp;Tiesthe<BR>// producer to the CalculatorFilter so incremental updates can<BR>// be made...<BR>public class CalculatorImage {<BR>&nbsp;&nbsp;&nbsp;int width;&nbsp;&nbsp;// The dimensions of theimage...<BR>&nbsp;&nbsp;&nbsp;int height;<BR>&nbsp;&nbsp;&nbsp;CalculatorProducer cp;&nbsp;&nbsp;// What producesthe image data...<BR>&nbsp;&nbsp;&nbsp;IndexColorModel palette;&nbsp;&nbsp;// The colorsof the image...<BR>&nbsp;&nbsp;&nbsp;// Create Palette only once per session...<BR>&nbsp;&nbsp;&nbsp;static IndexColorModel prvPalette = null;<BR>&nbsp;&nbsp;&nbsp;int numColors = 256;&nbsp;&nbsp;// Number ofcolors in palette...<BR><BR>&nbsp;&nbsp;&nbsp;// Use defines how big of an image they want...<BR>&nbsp;&nbsp;&nbsp;public CalculatorImage(int width,int height,CalculatorProducercp) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.width = width;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.height = height;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.cp = cp;<BR>&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;// Start producing the Calculator image...<BR>&nbsp;&nbsp;&nbsp;public synchronized Image getImage() {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Hook into the filter...<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;createPalette();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ImageProducer p = new FilteredImageSource(<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; new MemoryImageSource(width,height,palette,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(newint[width * height]),0,width),<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;newCalculatorFilter(palette,cp));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Return the image...<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return Toolkit.getDefaultToolkit().createImage(p);<BR>&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;// Create a 256 color palette...<BR>&nbsp;&nbsp;&nbsp;// Use Default color model...<BR>&nbsp;&nbsp;&nbsp;void createPalette() {<BR>&nbsp;&nbsp;&nbsp;&nbsp; // Create palette only once per session...<BR>&nbsp;&nbsp;&nbsp;&nbsp; if (prvPalette != null) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;palette= prvPalette;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return;<BR>&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp; // Create a palette out of random RGBcombinations...<BR>&nbsp;&nbsp;&nbsp;&nbsp; byte blues[], reds[], greens[];<BR>&nbsp;&nbsp;&nbsp;&nbsp; reds = new byte[numColors];<BR>&nbsp;&nbsp;&nbsp;&nbsp; blues = new byte[numColors];<BR>&nbsp;&nbsp;&nbsp;&nbsp; greens = new byte[numColors];<BR>&nbsp;&nbsp;&nbsp;&nbsp; // First and last entries are black andwhite...<BR>&nbsp;&nbsp;&nbsp;&nbsp; blues[0] = reds[0] = greens[0] = (byte)0;<BR>&nbsp;&nbsp;&nbsp;&nbsp; blues[255] = reds[255] = greens[255]= (byte)255;<BR>&nbsp;&nbsp;&nbsp;&nbsp; // Fill in other entries...<BR>&nbsp;&nbsp;&nbsp;&nbsp; for ( int x = 1; x &lt; 254; x++ ){<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reds[x] = (byte)(255 * Math.random());<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;blues[x] = (byte)(255 * Math.random());<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;greens[x] = (byte)(255 * Math.random());<BR>&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp; // Create Index Color Model...<BR>&nbsp;&nbsp;&nbsp;&nbsp; palette = new IndexColorModel(8,256,reds,greens,blues);<BR>&nbsp;&nbsp;&nbsp;&nbsp; prvPalette = palette;<BR>&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;// Save the image set as a BMP file...<BR>&nbsp;&nbsp;&nbsp;public void saveBMP(String filename,int pix[]){<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BmpImage.saveBitmap(filename,palette,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pix,width,height);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;catch (IOException ioe) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(&quot;Errorsaving file!&quot;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;}<BR>}</TT></BLOCKQUOTE><HR><H2><A NAME="TheMandelAppClass"><FONT SIZE=5 COLOR=#FF0000>TheMandelApp Class</FONT></A></H2><P>The MandelApp class, shown in Listing 14.6, creates and displaysthe full Mandelbrot set; the end result is shown in Figure 14.2.An instance of the Mandelbrot class is created in the <TT>init()</TT>method. Whenever the Mandelbrot calculation has produced somenew data, it calls the ImageObserver-based method, <TT>imageUpdate()</TT>.This will probably result in the applet being repainted to showthe new data. If the image is complete, an internal flag is set.After this, if you click the mouse, the image will be saved toa BMP formatted file called mandel.bmp.<HR><BLOCKQUOTE><B>Listing 14.6. The MandelApp class.<BR></B></BLOCKQUOTE><BLOCKQUOTE><TT>import java.awt.*;<BR>import java.lang.*;<BR>import java.applet.Applet;<BR><BR>// This applet displays the Mandlebrot set through<BR>// use of the Mandelbrot class...<BR>public class MandelApp extends Applet&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;Image im;&nbsp;&nbsp;&nbsp;// Image that displaysMandelbrot set...<BR>&nbsp;&nbsp;&nbsp;Mandelbrot m; // Creates the Mandelbrot image...<BR>&nbsp;&nbsp;&nbsp;int NUMCOLS = 640;&nbsp;&nbsp;&nbsp;// Dimensionsimage display...<BR>&nbsp;&nbsp;&nbsp;int NUMROWS = 350;<BR>&nbsp;&nbsp;&nbsp;boolean complete = false;<BR><BR>&nbsp;&nbsp;&nbsp;// Set up the Mandelbrot set...<BR>&nbsp;&nbsp;&nbsp;public void init() {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m = new Mandelbrot(NUMCOLS,NUMROWS);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;im = m.getImage();<BR>&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;// Will get updates as set is being created.<BR>&nbsp;&nbsp;&nbsp;// Repaint when they occur...<BR>&nbsp;&nbsp;&nbsp;public boolean imageUpdate(Image im,int flags,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int x, int y, int w, int h){<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ((flags &amp; FRAMEBITS)!= 0) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;showStatus(&quot;Calculating...&quot;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;repaint();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return true;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ((flags &amp; ALLBITS)!= 0) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;showStatus(&quot;ImageComplete!&quot;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;repaint();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;complete= true;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return false;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return true;<BR>&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;// Paint on update...<BR>&nbsp;&nbsp;&nbsp;public void update(Graphics g) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint(g);<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;public synchronized void paint(Graphics g) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; g.drawImage(im,0,0,this);<BR>&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;// Save Bitmap on mouse down when image complete...<BR>&nbsp;&nbsp;&nbsp;public boolean mouseDown(Event evt,int x, inty) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (complete){<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;showStatus(&quot;SaveBitmap...&quot;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m.saveBMP(&quot;mandel.bmp&quot;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;showStatus(&quot;Bitmapsaved!&quot;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;returntrue;<BR>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -