/* * The MIT License (MIT) * * Copyright (c) 2015 Camil Staps * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ package com.camilstaps.mandelbrot; import java.awt.Color; import java.awt.Graphics; import java.awt.event.InputEvent; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import javax.swing.JFrame; /** * A frame in which zooming is possible * @author Camil Staps, s4498062 */ public class ZoomFrame extends JFrame implements MouseListener, MouseMotionListener { private MandelbrotView mandelbrotView; private MandelbrotTextFields mandelbrotTextFields; private Graphics graphics; private int start_x, start_y, old_x, old_y; private boolean dragging = false; public ZoomFrame(String s) { super(s); addMouseListener(this); addMouseMotionListener(this); } public void setMandelbrotView(MandelbrotView mv) { mandelbrotView = mv; } public void setMandelbrotTextFields(MandelbrotTextFields mtf) { mandelbrotTextFields = mtf; } @Override public void mouseClicked(MouseEvent me) {} /** * Save the coordinates * @param me */ @Override public void mousePressed(MouseEvent me) { old_x = start_x = me.getX(); old_y = start_y = me.getY(); } /** * On shift-click, zoom out. * On click, zoom in. * On drag, zoom the box. * @param me */ @Override public void mouseReleased(MouseEvent me) { if (me.getX() == start_x && me.getY() == start_y) { if ((me.getModifiers() & InputEvent.SHIFT_MASK) != 0) { zoomOut(me); } else { zoomIn(me); } } else { zoomBox(me); } mandelbrotView.redraw(); dragging = false; } /** * Zoom in with factor 2 * @param me */ private void zoomIn(MouseEvent me) { if (mandelbrotView == null || mandelbrotTextFields == null) return; mandelbrotTextFields.setCenterX(mandelbrotView.getX(me.getX())); mandelbrotTextFields.setCenterY(mandelbrotView.getY(me.getY())); mandelbrotTextFields.setScale(mandelbrotTextFields.getScale() * 2); } /** * Zoom out with factor 2 * @param me */ private void zoomOut(MouseEvent me) { if (mandelbrotView == null || mandelbrotTextFields == null) return; mandelbrotTextFields.setCenterX(mandelbrotView.getX(me.getX())); mandelbrotTextFields.setCenterY(mandelbrotView.getY(me.getY())); mandelbrotTextFields.setScale(mandelbrotTextFields.getScale() / 2); } /** * Zoom to the selected box. * Intentionally chose to let the user only select squares, otherwise it's too easy to mess up the scale * @param me */ private void zoomBox(MouseEvent me) { double center_x = (mandelbrotView.getX(me.getX()) - mandelbrotView.getX(start_x)) / 2 + mandelbrotView.getX(start_x); double center_y = (mandelbrotView.getY(me.getY()) - mandelbrotView.getY(start_y)) / 2 + mandelbrotView.getY(start_y); mandelbrotTextFields.setCenterX(center_x); mandelbrotTextFields.setCenterY(center_y); double scale = mandelbrotTextFields.getScale() / (me.getX() - start_x) * getWidth(); mandelbrotTextFields.setScale(scale); } @Override public void mouseEntered(MouseEvent me) {} @Override public void mouseExited(MouseEvent me) {} /** * Erase the old box, draw the new box * @param me */ @Override public void mouseDragged(MouseEvent me) { Graphics g = getSafeGraphics(); if (g != null) { if (dragging) { eraseZoombox(); } g.drawRect(start_x, start_y, me.getX() - start_x, me.getX() - start_x); old_y = old_x = me.getX(); dragging = true; } } /** * Semi-singleton construction for graphics * @return */ private Graphics getSafeGraphics() { if (graphics == null) { graphics = getGraphics(); graphics.setXORMode(Color.white); } return graphics; } /** * Erase the old zoombox if it exists */ private void eraseZoombox() { if (start_x < 0 || start_y < 0 || old_x < 0 || old_y < 0) return; Graphics g = getSafeGraphics(); if (g != null) g.drawRect(start_x, start_y, old_x - start_x, old_y - start_x); } @Override public void mouseMoved(MouseEvent me) {} /** * Interface to get and set user input */ public interface MandelbrotTextFields { /** * Set the center X * @param x */ public void setCenterX(double x); /** * Set the center Y * @param y */ public void setCenterY(double y); /** * Get the scale factor * @return */ public double getScale(); /** * Set the scale factor * @param scale */ public void setScale(double scale); /** * Set the max amount of iterations for the mandel function * @param repetitions */ public void setRepetitions(int repetitions); } }