// FractalGUI.java (Ver.1.00) // Mandelbrot Fractal Image Controler on Java Graphical User Interface // Copyright (C) 1997 Y.Nagatani // nagatani@eken.phys.nagoya-u.ac.jp import java.applet.Applet; import java.awt.*; import FractalCanvas; public class FractalGUI extends Applet implements Runnable { public int xSize = 170, // Fractal Screen Size ySize = 170; public FractalCanvas fractal; public Image imBuffer; // Image Buffer public Thread thread = null; private TextField tfNestLevel; private TextField tfStatus; private TextField tfReSt, tfImSt, tfReWid, tfImWid; private Checkbox ckbHighResolution; public void init () { super.init (); Color bg = getColorParameter ("background"); if (bg != null) setBackground (bg); // FractalCanvas Initialization imBuffer = createImage (xSize, ySize); fractal = new FractalCanvas (xSize, ySize, imBuffer); // Layout setting GridBagLayout gbLayout = new GridBagLayout (); Panel panelLeft = new Panel (); panelLeft.setLayout (gbLayout); tfReSt = new TextField ("TestA", 15); tfImSt = new TextField ("TestB", 15); tfReWid = new TextField ("TestC", 15); tfImWid = new TextField ("TestD", 15); constrain (panelLeft, new Label("Start:"), 0,0, 1,1); constrain (panelLeft, tfReSt, 0,1, 1,1); constrain (panelLeft, tfImSt, 0,2, 1,1); constrain (panelLeft, new Label("Width:"), 0,3, 1,1); constrain (panelLeft, tfReWid, 0,4, 1,1); constrain (panelLeft, tfImWid, 0,5, 1,1); constrain (panelLeft, new Label("Resolution:"), 0,6, 1,1); ckbHighResolution = new Checkbox ("High"); constrain (panelLeft, ckbHighResolution, 0,7, 1,1); CheckboxGroup ckbg = new CheckboxGroup (); Checkbox ckbAbsolute = new Checkbox ("Absolute", ckbg, true); Checkbox ckbPhase = new Checkbox ("Phase", ckbg, false); constrain (panelLeft, new Label("Mode:"), 0,8, 1,1); constrain (panelLeft, ckbAbsolute, 0,9, 1,1); constrain (panelLeft, ckbPhase, 0,10,1,1); Panel panelTop = new Panel (); panelTop.setLayout (gbLayout); tfStatus = new TextField ("Push [Draw] Button!", 30); constrain (panelTop, new Label ("Status:"), 0,0, 1,1); constrain (panelTop, tfStatus, 0,1, 1,1); Panel panelBottom = new Panel (); panelBottom.setLayout (gbLayout); tfNestLevel = new TextField ( Integer.toString (fractal.mandel.nNestLevel, 10), 4); constrain (panelBottom, new Label ("Nests=", Label.RIGHT), 0,0, 1,1, 13,0,10,0); constrain (panelBottom, tfNestLevel, 1,0, 1,1, 10,0,10,0); constrain (panelBottom, new Button ("Draw"), 2,0, 1,1, 10,10,10,10); this.setLayout (gbLayout); constrain (this, panelLeft, 0,0, 1,3, 0,0,0,10); constrain (this, panelTop, 1,0, 1,1); constrain (this, fractal, 1,1, 1,1, 10,10,10,10); constrain (this, panelBottom, 1,2, 1,1); tfReSt.setText (String.valueOf (fractal.mandel.realStart)); tfImSt.setText (String.valueOf (fractal.mandel.imagStart)); tfReWid.setText (String.valueOf (fractal.mandel.realWidth)); tfImWid.setText (String.valueOf (fractal.mandel.imagWidth)); } public boolean action (Event e, Object o) { if (e.target instanceof Button) { if ("Draw".equals (o)) { this.Draw (); return true; } } else if (e.target instanceof TextField) { fractal.mandel.setNestLevel ( Integer.parseInt ((String) o)); return true; } else if (e.target instanceof Checkbox) { String label = ((Checkbox) e.target).getLabel (); if (((Boolean) o).booleanValue ()) { if (label.equals ("Absolute")) { fractal.mandel.setAbsoluteMode (); } else if (label.equals ("Phase")) { fractal.mandel.setPhaseMode (); } } return true; } return false; } public boolean mouseDown (Event e, int x, int y){ if (!fractal.bMouseDowned) return false; int x1 = fractal.xMouse1, y1 = ySize - fractal.yMouse1, x2 = fractal.xMouse2, y2 = ySize - fractal.yMouse2; double reSt = fractal.mandel.realStart, imSt = fractal.mandel.imagStart, reWid = fractal.mandel.realWidth, imWid = fractal.mandel.imagWidth, reSt2, imSt2, reWid2, imWid2; reSt2 = reSt + reWid * x1 / xSize; imSt2 = imSt + imWid * y1 / ySize; reWid2 = reWid * (x2 - x1) / xSize; imWid2 = imWid * (y2 - y1) / ySize; tfReSt.setText (String.valueOf (reSt2)); tfImSt.setText (String.valueOf (imSt2)); if (!fractal.bFirstMouse) { // This logic is inverted. tfReWid.setText (" "); tfImWid.setText (" "); } else { tfReWid.setText (String.valueOf (reWid2)); tfImWid.setText (String.valueOf (imWid2)); } fractal.MouseDownedReset (); return true; } public void Draw () { tfStatus.setText ("[Draw] Pushed!"); try {Thread.sleep (500);} catch (InterruptedException e) {} fractal.mandel.setRegion ( Double.valueOf (tfReSt.getText () ).doubleValue (), Double.valueOf (tfImSt.getText () ).doubleValue (), Double.valueOf (tfReWid.getText ()).doubleValue (), Double.valueOf (tfImWid.getText ()).doubleValue ()); if (ckbHighResolution.getState ()) fractal.mandel.setHighResolution (); else fractal.mandel.setLowResolution (); fractal.mandel.setNestLevel ( Integer.parseInt (tfNestLevel.getText ())); fractal.mandel.SubPaintInit (); if (thread != null && thread.isAlive ()) { tfStatus.setText ("Stop old Thread."); thread.stop (); try {Thread.sleep (500);} catch (InterruptedException e) {} } thread = new Thread (this); thread.start(); } public void run () { // This is called when thread.start() is called. tfStatus.setText ("Now Calculating..."); boolean bContinue = true; while (thread.isAlive () && bContinue) { bContinue = fractal.SubCalculation (); fractal.repaint (); this.repaint (); try {Thread.sleep (100);} catch (InterruptedException e) {} } tfStatus.setText ("Completed."); } public void stop() { // This is called when Applet stop. if (thread != null) { thread.stop(); thread = null; } } boolean bFirstCall = true; public void paint (Graphics g) { super.paint (g); if (bFirstCall) { bFirstCall = false; // this.Draw (); } } public void update (Graphics g) { this.paint (g); } public Color getColorParameter (String name) { String value = getParameter (name); int nColor; try {nColor = Integer.parseInt (value, 16);} catch (NumberFormatException e) { return null; } return new Color (nColor); } public int getIntParameter (String Command) throws NumberFormatException { String Contents = getParameter (Command); return Integer.parseInt (Contents); } public void constrain (Container container, Component component, int grid_x, int grid_y, int grid_width, int grid_height, int fill, int anchor, double weight_x, double weight_y, int top, int left, int bottom, int right) { GridBagConstraints c = new GridBagConstraints (); c.gridx = grid_x; c.gridy = grid_y; c.gridwidth = grid_width; c.gridheight = grid_height; c.fill = fill; c.anchor = anchor; c.weightx = weight_x; c.weighty = weight_y; if (top+bottom+left+right > 0) c.insets = new Insets (top, left, bottom, right); ((GridBagLayout) container.getLayout ()) .setConstraints (component, c); container.add (component); } public void constrain (Container container, Component component, int grid_x, int grid_y, int grid_width, int grid_height, int top, int left, int bottom, int right) { constrain (container, component, grid_x, grid_y, grid_width, grid_height, GridBagConstraints.NONE, GridBagConstraints.NORTHWEST, 0.0, 0.0, top, left, bottom, right); } public void constrain (Container container, Component component, int grid_x, int grid_y, int grid_width, int grid_height) { constrain (container, component, grid_x, grid_y, grid_width, grid_height, GridBagConstraints.NONE, GridBagConstraints.NORTHWEST, 0.0, 0.0, 0, 0, 0, 0); } public String[][] getParameterInfo () { String info[][] = { {"background", "hexadecimal (RRGGBB)", "background color"} }; return info; } public String getAppletInfo () { return "Mandelbrot Fractal Contoroler (Ver.1.00)\n" + "(Mandelbrot.java FractalCanvas.java FractalGUI.java )\n" + "Copyright (C) 1997 Y.Nagatani\n" + "nagatani@eken.phys.nagoya-u.ac.jp"; } }