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
|
/*
* 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 fractals;
import com.camilstaps.mandelbrot.MandelbrotView;
import com.camilstaps.mandelbrot.ZoomFrame;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
* The window for week 11: a gridview for the fractal, and controls
* @author Sjaak Smetsers
* @author Camil Staps
*/
public class MainWindow implements MandelbrotView.MandelbrotProvider, ZoomFrame.MandelbrotTextFields {
// the size of the window
public static final int WIDTH = 650, HEIGHT = 650;
/**
* Initial values
*/
private final String INITIAL_CENTER_X = "0",
INITIAL_CENTER_Y = "0",
INITIAL_SCALE = "100",
INITIAL_REPETITIONS = "100";
// The grip panel
private final GridView grid;
/**
* Controls
*/
private final JTextField field_centerX = new JTextField(INITIAL_CENTER_X, 6);
private final JTextField field_centerY = new JTextField(INITIAL_CENTER_Y, 6);
private final JTextField field_scale = new JTextField(INITIAL_SCALE, 6);
private final JTextField field_repetitions = new JTextField(INITIAL_REPETITIONS, 6);
private final JButton button_redraw = new JButton("Redraw");
/**
* To allow redrawing
*/
private final MandelbrotView mandelbrotView;
public MainWindow () {
ZoomFrame mainFrame = new ZoomFrame ("Mandelbrot");
mainFrame.setLayout(new BorderLayout());
mainFrame.setSize (WIDTH, HEIGHT);
mainFrame.setLocationRelativeTo(null);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setResizable(false);
mainFrame.setVisible(true);
Insets insets = mainFrame.getInsets();
grid = new GridView(WIDTH - insets.left - insets.right, HEIGHT - insets.top - insets.bottom);
mainFrame.add(grid, BorderLayout.CENTER);
mandelbrotView = new MandelbrotView(this, grid);
mandelbrotView.redraw();
mainFrame.setMandelbrotView(mandelbrotView);
mainFrame.setMandelbrotTextFields(this);
mainFrame.add(getControlsPanel(), BorderLayout.EAST);
mainFrame.pack();
}
private JPanel getControlsPanel() {
JPanel right = new JPanel(new BorderLayout());
JPanel panel = new JPanel(new GridLayout(10,1));
button_redraw.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
if (ae.getActionCommand().equals("Redraw")) {
mandelbrotView.redraw();
}
}
});
panel.add(new JLabel("Center x:"));
panel.add(field_centerX);
panel.add(new JLabel("Center y:"));
panel.add(field_centerY);
panel.add(new JLabel("Scale:"));
panel.add(field_scale);
panel.add(new JLabel("Repetitions:"));
panel.add(field_repetitions);
panel.add(button_redraw);
JButton button_reset = new JButton("Reset");
button_reset.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
field_centerX.setText(INITIAL_CENTER_X);
field_centerY.setText(INITIAL_CENTER_Y);
field_scale.setText(INITIAL_SCALE);
field_repetitions.setText(INITIAL_REPETITIONS);
mandelbrotView.redraw();
}
});
panel.add(button_reset);
right.add(panel, BorderLayout.NORTH);
right.add(new JPanel(), BorderLayout.CENTER);
return right;
}
/**
* A getter for the grid
* @return the grid
*/
public Grid getGrid () {
return grid;
}
@Override
public double getCenterX() {
return Double.parseDouble(field_centerX.getText());
}
@Override
public double getCenterY() {
return Double.parseDouble(field_centerY.getText());
}
@Override
public double getScale() {
return Double.parseDouble(field_scale.getText());
}
@Override
public int getRepetitions() {
return Integer.parseInt(field_repetitions.getText());
}
@Override
public void setCenterX(double x) {
field_centerX.setText(String.format("%.5f", x));
}
@Override
public void setCenterY(double y) {
field_centerY.setText(String.format("%.5f", y));
}
@Override
public void setScale(double scale) {
field_scale.setText(String.format("%.5f", scale));
}
@Override
public void setRepetitions(int repetitions) {
field_repetitions.setText(Integer.toString(repetitions));
}
}
|