I want to know how to zoom in on a mandelbrot drawing with the mouse by right and left clicking
Thank you for your help


1 | private double zoomFactor = 100.0 ; |
1 2 3 4 5 6 | if (Greenfoot.mouseClicked( null )) { double dz += Greenfoot.getMouseInfo().getButton() == 1 ? 0.1 : - 0.1 ; if (zoomFactor + dz >= 0.5 ) zoomFactor += dz; updateImage(); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 | public void act() { if (Greenfoot.mouseClicked( null )) { double dz += Greenfoot.getMouseInfo().getButton() == 1 ? 0.1 : - 0.1 ; if (zoomFactor + dz > 0.5 ) zoomFactor += dz; updateImage(); } //GreenfootImage gfim = new GreenfootImage(900,600); drawMandelbrot(); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Mandelbrot here. * * @author (your name) * @version (a version number or a date) */ public class Mandelbrot5 extends Actor { /** * Act - do whatever the Mandelbrot wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ private final int MAX_ITER = 30 ; private final double ZOOM = 200 ; private double zx, zy, cx, cy, temp; public static final int WIDTH = 900 ; public static final int HEIGHT = 600 ; private double zoomFactor = 100.0 ; public Mandelbrot5() { } public void act() { if (Greenfoot.mouseClicked( null )) { double dz = Greenfoot.getMouseInfo().getButton() == 1 ? 0.1 : - 0.1 ; if (zoomFactor + dz > 0.5 ) zoomFactor += dz; updateImage(); } } public void updateImage() { GreenfootImage gfim = new GreenfootImage (WIDTH, HEIGHT); for ( int x = 0 ; x < WIDTH; x++) { for ( int y = 0 ; y < HEIGHT; y++) { zx = zy = 0 ; cx = (x - 400 ) / ZOOM; cy = (y - 300 ) / ZOOM; int iter = 0 ; while (zx * zx + zy * zy < 4 && iter < MAX_ITER) { temp = zx; zx = zx * zx - zy * zy + cx; zy = 2.0 * zy * temp + cy; iter++; } if (iter > MAX_ITER) gfim.setColorAt(x,y,Color.WHITE); else if (iter > 9 *MAX_ITER/ 10 ) gfim.setColorAt(x,y,Color.DARK_GRAY); else if (iter > 8 *MAX_ITER/ 10 ) gfim.setColorAt(x,y,Color.GRAY); else if (iter > 7 *MAX_ITER/ 10 ) gfim.setColorAt(x,y,Color.MAGENTA); else if (iter > 6 *MAX_ITER/ 10 ) gfim.setColorAt(x,y,Color.CYAN); else if (iter > 5 *MAX_ITER/ 10 ) gfim.setColorAt(x,y,Color.BLUE); else if (iter > 4 *MAX_ITER/ 10 ) gfim.setColorAt(x,y,Color.GREEN); else if (iter > 3 *MAX_ITER/ 10 ) gfim.setColorAt(x,y,Color.YELLOW); else if (iter > 2 *MAX_ITER/ 10 ) gfim.setColorAt(x,y,Color.ORANGE); else if (iter > 1 *MAX_ITER/ 10 ) gfim.setColorAt(x,y,Color.RED); else gfim.setColorAt(x,y,Color.BLACK); } } gfim.drawImage(gfim, 0 , 0 ); setImage(gfim); } } |