Tayyab Hussain
Tayyab Hussain

Reputation: 43

Zooming using scrollwheel in java

I am trying to make a Mandelbrot program that allows zooming, but the zoom doesn't seem to be working, and i don't see what is wrong with the way i have implemented the zoom.I am using eclipse and the program doesn't return any errors. Here is my code:

import java.awt.Graphics; 
import java.awt.event.*; 
import java.awt.image.BufferedImage; 
import javax.swing.JFrame;   

public class Mandelbrot extends JFrame {

            private final int MAX_ITER = 570;
            private static double ZOOM = 200;
            private BufferedImage I;
            private double zx, zy, cX, cY, tmp;
            public static boolean zooming = false;
            public Mandelbrot() 
            {
                 super("MandelbrotSet");
                 setBounds(100, 100, 800, 600);
                 setResizable(false);
                 setDefaultCloseOperation(EXIT_ON_CLOSE);
                 I = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
                 for (int y = 0; y < getHeight(); y++) {
                     for (int x = 0; x < getWidth(); x++) {
                         zx = zy = 0;
                         cX = (x - 400) / ZOOM;
                         cY = (y - 300) / ZOOM;
                         int iter = MAX_ITER;
                         while (zx * zx + zy * zy < 4 && iter > 0) {
                             tmp = zx * zx - zy * zy + cX;
                             zy = 2.0 * zx * zy + cY;
                             zx = tmp;
                             iter--;
                         }
                         I.setRGB(x, y, iter | (iter << 8));
                     }
                }
             setVisible(true);
             while(1>0)
             {
                 if(zooming)
                 {
                     revalidate();
                     repaint();
                     System.out.println("zooming");
                     zooming = false;
                 }
             }    }

            @Override
            public void paint(Graphics g) {
                g.drawImage(I, 0, 0, this);
            }

            public static void main(String[] args) {
                new Mandelbrot().addMouseWheelListener(new MouseWheelListener(){   public void mouseWheelMoved(MouseWheelEvent
        e) {

                if (e.getWheelRotation() < 0) {
                   ZOOM=ZOOM+100;
                   zooming = true;
                } else 
                {
                    ZOOM=ZOOM-100;
                    zooming = true;

                } }    });

            } }

Upvotes: 1

Views: 302

Answers (1)

Thomas Kl&#228;ger
Thomas Kl&#228;ger

Reputation: 21435

  1. Your constructor contains an endless loop. It therefore never returns and your MouseWheelListener is never added to the frame.

  2. You calculate the BufferedImage exactly once (before the endless loop), so even if you would attach the MouseWheelListener before the loop it would have no effect.

I would move the calculation of the picture into its own method, call this method once from the constructor and once from your MouseWheelListener and remove the endless loop from the constructor.

        public Mandelbrot() 
        {
             super("MandelbrotSet");
             //...
             I = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
             calculatePicture();
             setVisible(true);
        }
        public void calculatePicture() {
             for (int y = 0; y < getHeight(); y++) {
                 //...
             }
             repaint();
        }
        public static void main(String[] args) {
            new Mandelbrot().addMouseWheelListener(new MouseWheelListener(){
                public void mouseWheelMoved(MouseWheelEvent
    e) {
                    //...
                    calculatePicture();
                }
            });
        }

Upvotes: 1

Related Questions