aboutsummaryrefslogtreecommitdiff
path: root/Week11 Mandelbrot/src/com/camilstaps/mandelbrot/ZoomFrame.java
blob: 25cd2f2302e23cd230299a722057d2a133192c12 (plain) (blame)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
 * The MIT License (MIT)
 * 
 * Copyright (c) 2015 Camil Staps <info@camilstaps.nl>
 * 
 * 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);
    }
    
}