package graphics; import java.awt.*; import java.applet.*; public class mandelCompute extends Applet implements Runnable { float x, y, xdiv, ydiv; boolean flag = false; int i, bottomx, topx, bottomy, topy, tempit; Integer j; int xresolution, yresolution; Complex z, c, znought, tempz; float tempx; Image offscreen; Color col = new Color(0); float sat = (new Float(1)).floatValue(); float brill = (new Float(1)).floatValue(); Thread t = new Thread(this); float xstart, newxstart, newystart; float ystart; float xwidth; float ywidth; int xX=0; int yY=0; Window ourWindow; Frame ourFrame; Container ourCont; Panel ourPanel; public void init() { //check if they're already assigned! System.out.println("init called"); xstart = new Float(-2).floatValue(); ystart = new Float(-1.5).floatValue(); xwidth = new Float(2.5).floatValue(); ywidth = new Float(3).floatValue(); Graphics g = null; ourCont = null; if (ourFrame != null) { ourCont = ourFrame; } else { ourCont = this; } g = ourCont.getGraphics(); System.out.println(ourCont); xresolution = ourCont.getSize().width; yresolution = ourCont.getSize().height; z = new Complex(); tempx = 0; xdiv = xwidth/xresolution; ydiv = ywidth/yresolution; znought = new Complex(0,0); } public void start() { t.start(); } public void stop() { t.stop(); } public void run() { System.out.println("run - ourCont = "+ ourCont); System.out.println("x = " + xresolution + ", y = " + yresolution); ourCont.show(); //ourFrame.show(); //System.out.println("ourFrame.createImage(400,400) = " + ourFrame.createImage(400,400)); System.out.println("ourFrame.createImage(400,400) = " + ourCont.createImage(400,400)); if (offscreen == null) { offscreen = ourCont.createImage(xresolution, yresolution); } System.out.println("offscreen = " + offscreen); //---------------------------- Graphics gG = ourCont.getGraphics(); Graphics h = offscreen.getGraphics(); x = xstart; for (xX = 0; xX < xresolution; xX++) { y = ystart; for (yY = 0; yY < yresolution; yY++) { c = new Complex(x,y); z = znought; i=0; while (i < 127) { if ((z.x*z.x) + (z.y*z.y) > 4) { break; } else { tempx = (z.x * z.x) - (z.y * z.y); z.y = 2*(z.x * z.y); z.x = tempx; z = Complex.add(z, c); i++; } } col = new Color(16777216/(i+1)); //col = new Color(131072/(i+1)); //col = new Color(256-(i+1)*2, 256-(i+1)*2, 256-(i+1)*2); h.setColor(col); h.drawRect(xX, yY, 1,1); y = y+ydiv; } try {Thread.sleep(1);} catch (Exception e) { ; } gG.drawImage(offscreen, 0,0, ourCont); x = x+xdiv; } System.out.println("done"); //----------------------------------- } public void paint(Graphics g) { if (offscreen != null) { g.drawImage(offscreen, 0,0, ourCont); } } public boolean mouseDown(Event e, int mousex, int mousey) { if (flag == false) { flag = true; bottomx = mousex; bottomy = mousey; } else { flag = false; t.stop(); t = new Thread(this); if (mousex > bottomx) { topx = mousex; } else { topx = bottomx; bottomx = mousex; } if (mousey > bottomy) { topy = mousey; } else { topy = bottomy; bottomy = mousey; } //System.out.println("topx = " + topx + ", topy = " + topy); //System.out.println("bottomx = " + bottomx + ", bottomy = " + bottomy); //the nex four lines need work! xstart += ((bottomx*xwidth)/xresolution); ystart += ((bottomy*ywidth)/yresolution); xwidth = ((topx - bottomx)*xwidth)/xresolution; ywidth = ((topy - bottomy)*ywidth)/yresolution; z = new Complex(); tempx = 0; xdiv = xwidth/xresolution; ydiv = ywidth/yresolution; znought = new Complex(0,0); if (offscreen == null) { //? should be != null? offscreen = ourCont.createImage(xresolution, yresolution); } t.start(); } return true; } public static void main(String argv[]) { Frame f = new Frame(); f.show(); f.setSize(400,400); Window w = new Window(f); w.setSize(400,400); Panel p = new Panel(); w.add(p); p.show(); p.setSize(400,400); mandelCompute mC = new mandelCompute(); mC.ourWindow = w; mC.ourPanel = p; mC.ourFrame = f; System.out.println("w.getSize() = " + w.getSize()); mC.init(); mC.run(); } }