This site requires JavaScript, please enable it in your browser!
Greenfoot back
ronald
ronald wrote ...

2020/4/9

Zoom in and out

1
2
ronald ronald

2020/4/9

#
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
Kostya20052011 Kostya20052011

2020/4/9

#
int h; int w; int k; if(getObjectsInRange(1000, ***.class).size()>0){ b =getObjectsInRange(1000, ***.class).get(0); } Sprite = b.getImage(); if(Greenfoot.mouseClicked()){ h=(int)(h*k); w=(int)(w*k); } Sprite.scale(w,h); I made a rough layout, then I think you can do it yourself. The only drawback is that this code must be in some foreign object (not in the world).If k>1 image increases, k<0 image decreases.
danpost danpost

2020/4/9

#
With:
1
private double zoomFactor = 100.0;
being a zoom percent value, you might have something like:
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();
}
where updateImage recalculates the color values for all pixels of the displayed image.
Super_Hippo Super_Hippo

2020/4/9

#
You can check how I once did it. https://www.greenfoot.org/scenarios/20786
ronald ronald

2020/4/9

#
I saw your super hippo mandelbrot, he is super cool, what does the variable dz mean with the danpost code?
Super_Hippo Super_Hippo

2020/4/10

#
It is set to 0.1 or -0.1 depending on which mouse button you pressed. It is then used to adjust the zoomFactor unless the zoomFactor would go below 0.5.
danpost danpost

2020/4/10

#
It is very common to use the letter d in front of a variable to represent a small change in its value (the z referring to the zoom amount).
ronald ronald

2020/4/10

#
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();
         
    }
the code does not work otherwise the code is good maybe you have an idea? I try to wedge it with my mandelbrot drawing that I did the last time
Kostya20052011 Kostya20052011

2020/4/10

#
int h; int w; int k; Sprite =getImage(); if(Greenfoot.mouseClicked(null)){ if(Greenfoot.getMouseInfo().getButton() == 1){ k=0,8; } if(Greenfoot.getMouseInfo().getButton() == 2){ k=1,2; } h=(int)(h*k); w=(int)(w*k); } Sprite.scale(w,h); Try my new code, it's a little easier, I think, and if there are errors you can fix them.
Kostya20052011 Kostya20052011

2020/4/10

#
Also don't forget to set the sprite parameters: w=800; h=600; , for example
Super_Hippo Super_Hippo

2020/4/10

#
ronald wrote...
the code does not work otherwise the code is good
Do you get an error message? How does your “updateImage” and “drawMandelbrot” methods look like? In theory, you should only need one of them and only call it when something like the zoomFactor changes.
ronald ronald

2020/4/10

#
dz is red (; expected) ? is red too (not a statement) I will also try the other method of kostya
Super_Hippo Super_Hippo

2020/4/10

#
Change the += to a = in line 5.
danpost danpost

2020/4/10

#
Super_Hippo wrote...
Change the += to a = in line 5.
My bad. I had edited it multiple times and had missed that when I was done. My apologies.
ronald ronald

2020/4/10

#
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);
     }
 }
no big deal danpost I give you the whole code, there are no errors, it does not zoom in, the image appears by clicking are there any modifications to be made?
There are more replies on the next page.
1
2