//****************************************************************************** // Mandelbrot.java: Applet // //****************************************************************************** import java.applet.*; import java.awt.*; import MandelbrotFrame; //============================================================================== // Main Class for applet Mandelbrot // //============================================================================== public class Mandelbrot extends Applet implements Runnable { Rectangle currentRect=null; private boolean iterating = false; Image curimage = null; int w = 200; int h = 200; int pix[] = new int[w * h]; // THREAD SUPPORT: // m_Mandelbrot is the Thread object for the applet //-------------------------------------------------------------------------- Thread m_Mandelbrot = null; // STANDALONE APPLICATION SUPPORT: // m_fStandAlone will be set to true if applet is run standalone //-------------------------------------------------------------------------- boolean m_fStandAlone = false; // PARAMETER SUPPORT: // Parameters allow an HTML author to pass information to the applet; // the HTML author specifies them using the tag within the // tag. The following variables are used to store the values of the // parameters. //-------------------------------------------------------------------------- // Members for applet parameters // = //-------------------------------------------------------------------------- private double m_x0 = -2; private double m_x1 = 1; private double m_y0 = -1.5; private double m_y1 = 1.5; private int m_iters = 500; // Parameter names. To change a name of a parameter, you need only make // a single change. Simply modify the value of the parameter string below. //-------------------------------------------------------------------------- private final String PARAM_x0 = "x0"; private final String PARAM_x1 = "x1"; private final String PARAM_y0 = "y0"; private final String PARAM_y1 = "y1"; private final String PARAM_iters = "iters"; // STANDALONE APPLICATION SUPPORT // The GetParameter() method is a replacement for the getParameter() method // defined by Applet. This method returns the value of the specified parameter; // unlike the original getParameter() method, this method works when the applet // is run as a standalone application, as well as when run within an HTML page. // This method is called by GetParameters(). //--------------------------------------------------------------------------- String GetParameter(String strName, String args[]) { if (args == null) { // Running within an HTML page, so call original getParameter(). //------------------------------------------------------------------- return getParameter(strName); } // Running as standalone application, so parameter values are obtained from // the command line. The user specifies them as follows: // // JView Mandelbrot param1= param2=<"val with spaces"> ... //----------------------------------------------------------------------- int i; String strArg = strName + "="; String strValue = null; for (i = 0; i < args.length; i++) { if (strArg.equalsIgnoreCase(args[i].substring(0, strArg.length()))) { // Found matching parameter on command line, so extract its value. // If in double quotes, remove the quotes. //--------------------------------------------------------------- strValue= args[i].substring(strArg.length()); if (strValue.startsWith("\"")) { strValue = strValue.substring(1); if (strValue.endsWith("\"")) strValue = strValue.substring(0, strValue.length() - 1); } } } return strValue; } // STANDALONE APPLICATION SUPPORT // The GetParameters() method retrieves the values of each of the applet's // parameters and stores them in variables. This method works both when the // applet is run as a standalone application and when it's run within an HTML // page. When the applet is run as a standalone application, this method is // called by the main() method, which passes it the command-line arguments. // When the applet is run within an HTML page, this method is called by the // init() method with args == null. //--------------------------------------------------------------------------- void GetParameters(String args[]) { // Query values of all Parameters //-------------------------------------------------------------- String param; // x0: Lower limit of X //-------------------------------------------------------------- param = GetParameter(PARAM_x0, args); if (param != null) m_x0 = Double.valueOf(param).doubleValue(); // x1: Upper limit of X //-------------------------------------------------------------- param = GetParameter(PARAM_x1, args); if (param != null) m_x1 = Double.valueOf(param).doubleValue(); // y0: Lower limit of Y //-------------------------------------------------------------- param = GetParameter(PARAM_y0, args); if (param != null) m_y0 = Double.valueOf(param).doubleValue(); // y1: Upper limit of Y //-------------------------------------------------------------- param = GetParameter(PARAM_y1, args); if (param != null) m_y1 = Double.valueOf(param).doubleValue(); // iters: Iterations //-------------------------------------------------------------- param = GetParameter(PARAM_iters, args); if (param != null) m_iters = Integer.parseInt(param); } // STANDALONE APPLICATION SUPPORT // The main() method acts as the applet's entry point when it is run // as a standalone application. It is ignored if the applet is run from // within an HTML page. //-------------------------------------------------------------------------- public static void main(String args[]) { // Create Toplevel Window to contain applet Mandelbrot //---------------------------------------------------------------------- MandelbrotFrame frame = new MandelbrotFrame("Mandelbrot"); // Must show Frame before we size it so insets() will return valid values //---------------------------------------------------------------------- frame.show(); frame.hide(); frame.resize(frame.insets().left + frame.insets().right + 200, frame.insets().top + frame.insets().bottom + 240); // The following code starts the applet running within the frame window. // It also calls GetParameters() to retrieve parameter values from the // command line, and sets m_fStandAlone to true to prevent init() from // trying to get them from the HTML page. //---------------------------------------------------------------------- Mandelbrot applet_Mandelbrot = new Mandelbrot(); frame.add("Center", applet_Mandelbrot); applet_Mandelbrot.m_fStandAlone = true; applet_Mandelbrot.GetParameters(args); applet_Mandelbrot.init(); applet_Mandelbrot.start(); frame.show(); } // Mandelbrot Class Constructor //-------------------------------------------------------------------------- public Mandelbrot() { // TODO: Add constructor code here } // APPLET INFO SUPPORT: // The getAppletInfo() method returns a string describing the applet's // author, copyright date, or miscellaneous information. //-------------------------------------------------------------------------- public String getAppletInfo() { return "Name: Mandelbrot\r\n" + "Author: Martin Heller\r\n" + "Created with Microsoft Visual J++ Version 1.0"; } // PARAMETER SUPPORT // The getParameterInfo() method returns an array of strings describing // the parameters understood by this applet. // // Mandelbrot Parameter Information: // { "Name", "Type", "Description" }, //-------------------------------------------------------------------------- public String[][] getParameterInfo() { String[][] info = { { PARAM_x0, "double", "Lower limit of X" }, { PARAM_x1, "double", "Upper limit of X" }, { PARAM_y0, "double", "Lower limit of Y" }, { PARAM_y1, "double", "Upper limit of Y" }, { PARAM_iters, "int", "Iterations" }, }; return info; } // The init() method is called by the AWT when an applet is first loaded or // reloaded. Override this method to perform whatever initialization your // applet needs, such as initializing data structures, loading images or // fonts, creating frame windows, setting the layout manager, or adding UI // components. //-------------------------------------------------------------------------- public void init() { if (!m_fStandAlone) GetParameters(null); // If you use a ResourceWizard-generated "control creator" class to // arrange controls in your applet, you may want to call its // CreateControls() method from within this method. Remove the following // call to resize() before adding the call to CreateControls(); // CreateControls() does its own resizing. //---------------------------------------------------------------------- resize(200, 240); // TODO: Place additional initialization code here setLayout(new BorderLayout()); Panel p1 = new Panel(); p1.setLayout(new FlowLayout()); butRestart = new Button("Restart"); p1.add(butRestart); butZoom = new Button("Zoom"); p1.add(butZoom); add("South",p1); curimage = makeBlackImage(); repaint(); //start computing the current set //iterating=true; } public boolean action(Event evt, Object arg) { if(evt.target == butRestart) { clickedButtonRestart(); return true; } else if(evt.target == butZoom) { clickedButtonZoom(); return true; } return false; } Button butRestart; Button butZoom; public void clickedButtonRestart() { if(iterating) { iterating=false; try { Thread.sleep(1); } catch (InterruptedException e) { } } m_x0=-2.0; m_x1=1; m_y0=-1.5; m_y1=1.5; currentRect=null; iterating=true; } public void clickedButtonZoom() { if(currentRect==null) return; if(iterating) { iterating=false; try { Thread.sleep(1); } catch (InterruptedException e) { } } Dimension d = size(); Rectangle box = getDrawableRect(currentRect,d); double x0 = Xform((double)box.x, 0.0, (double)w, m_x0, m_x1); double x1 = Xform((double)box.x+box.width, 0.0, (double)w, m_x0, m_x1); double y0 = Xform((double)box.y, 0.0, (double)h, m_y0, m_y1); double y1 = Xform((double)box.y+box.height, 0.0, (double)h, m_y0, m_y1); m_x0=x0; m_x1=x1; m_y0=y0; m_y1=y1; currentRect=null; iterating=true; } // Place additional applet clean up code here. destroy() is called when // when you applet is terminating and being unloaded. //------------------------------------------------------------------------- public void destroy() { // TODO: Place applet cleanup code here } public void update(Graphics g) { paint(g); } // Mandelbrot Paint Handler //-------------------------------------------------------------------------- public void paint(Graphics g) { Dimension d = size(); g.drawImage(curimage, 0, 0, this); if(currentRect!=null) { Rectangle box=getDrawableRect(currentRect,d); g.setColor(Color.red); g.drawRect(box.x,box.y,box.width-1,box.height-1); } } Image makeBlackImage() { for(int i=0;i 4.0) break; }// for i if(i>m_iters) setpixel(m,n,0); else setpixel(m,n,i); if(!iterating) return false; } // for m curimage=createImage(new image.MemoryImageSource(w, h, pix, 0, w)); repaint(500L); } //for n curimage=createImage(new image.MemoryImageSource(w, h, pix, 0, w)); repaint(); return false; } // The start() method is called when the page containing the applet // first appears on the screen. The AppletWizard's initial implementation // of this method starts execution of the applet's thread. //-------------------------------------------------------------------------- public void start() { if (m_Mandelbrot == null) { m_Mandelbrot = new Thread(this); m_Mandelbrot.start(); iterating=true; } // TODO: Place additional applet start code here } // The stop() method is called when the page containing the applet is // no longer on the screen. The AppletWizard's initial implementation of // this method stops execution of the applet's thread. //-------------------------------------------------------------------------- public void stop() { if (m_Mandelbrot != null) { m_Mandelbrot.stop(); m_Mandelbrot = null; } // TODO: Place additional applet stop code here } // THREAD SUPPORT // The run() method is called when the applet's thread is started. If // your applet performs any ongoing activities without waiting for user // input, the code for implementing that behavior typically goes here. For // example, for an applet that performs animation, the run() method controls // the display of images. //-------------------------------------------------------------------------- public void run() { while (true) { if (iterating) { iterating=ComputeMandelBrotSet(); } try { Thread.sleep(50); } catch (InterruptedException e) { } } //while true } // MOUSE SUPPORT: // The mouseDown() method is called if the mouse button is pressed // while the mouse cursor is over the applet's portion of the screen. //-------------------------------------------------------------------------- public boolean mouseDown(Event evt, int x, int y) { currentRect = new Rectangle(x,y,0,0); repaint(); return false; } // MOUSE SUPPORT: // The mouseUp() method is called if the mouse button is released // while the mouse cursor is over the applet's portion of the screen. //-------------------------------------------------------------------------- public boolean mouseUp(Event evt, int x, int y) { currentRect.resize(x - currentRect.x, y - currentRect.y); repaint(); return false; } // MOUSE SUPPORT: // The mouseDrag() method is called if the mouse cursor moves over the // applet's portion of the screen while the mouse button is being held down. //-------------------------------------------------------------------------- public boolean mouseDrag(Event evt, int x, int y) { currentRect.resize(x - currentRect.x, y - currentRect.y); repaint(); return false; } // MOUSE SUPPORT: // The mouseMove() method is called if the mouse cursor moves over the // applet's portion of the screen and the mouse button isn't being held down. //-------------------------------------------------------------------------- public boolean mouseMove(Event evt, int x, int y) { // TODO: Place applet mouseMove code here return true; } // MOUSE SUPPORT: // The mouseEnter() method is called if the mouse cursor enters the // applet's portion of the screen. //-------------------------------------------------------------------------- public boolean mouseEnter(Event evt, int x, int y) { // TODO: Place applet mouseEnter code here return true; } // MOUSE SUPPORT: // The mouseExit() method is called if the mouse cursor leaves the // applet's portion of the screen. //-------------------------------------------------------------------------- public boolean mouseExit(Event evt, int x, int y) { // TODO: Place applet mouseExit code here return true; } // helper function from The Java Tutorial Rectangle getDrawableRect(Rectangle originalRect, Dimension drawingArea) { int x = originalRect.x; int y = originalRect.y; int width = originalRect.width; int height = originalRect.height; //Make sure rectangle width and height are positive. if (width < 0) { width = 0 - width; x = x - width + 1; if (x < 0) { width += x; x = 0; } } if (height < 0) { height = 0 - height; y = y - height + 1; if (y < 0) { height += y; y = 0; } } //The rectangle shouldn't extend past the drawing area. if ((x + width) > drawingArea.width) { width = drawingArea.width - x; } if ((y + height) > drawingArea.height) { height = drawingArea.height - y; } return new Rectangle(x, y, width, height); } }