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
|
/*
* 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.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
*
* @author camilstaps
*/
public class Textfields extends JPanel implements Observer {
private final FractalModel fractalModel;
private final Grid grid;
private final String INITIAL_CENTER_X = "-0.46",
INITIAL_CENTER_Y = "0",
INITIAL_SCALE = "0.5";
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 JButton button_redraw = new JButton("Redraw");
private final JButton button_reset = new JButton("Reset");
private final JCheckBox checkbox_multiple_swingworkers = new JCheckBox("Multiple SwingWorkers");
public Textfields(FractalModel fractalModel, Grid grid) {
super(new BorderLayout());
this.fractalModel = fractalModel;
this.grid = grid;
fractalModel.addObserver(this);
setupControls();
button_redraw.doClick();
}
private void setupControls() {
JPanel panel = new JPanel(new GridLayout(9,1));
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);
button_redraw.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
if (ae.getActionCommand().equals("Redraw")) {
fractalModel.setBorders(getStartX(), getEndX(), getStartY(), getEndY());
}
}
});
panel.add(button_redraw);
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);
button_redraw.doClick();
}
});
panel.add(button_reset);
checkbox_multiple_swingworkers.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
grid.setUseMultipleSwingWorkers(checkbox_multiple_swingworkers.isSelected());
}
});
panel.add(checkbox_multiple_swingworkers);
add(panel, BorderLayout.NORTH);
add(new JPanel(), BorderLayout.CENTER);
}
@Override
public int getWidth() {
return 200;
}
protected double getStartX() {
double width = 1 / Double.parseDouble(field_scale.getText());
double center = Double.parseDouble(field_centerX.getText());
return center - width / 2;
}
protected double getEndX() {
double width = 1 / Double.parseDouble(field_scale.getText());
double center = Double.parseDouble(field_centerX.getText());
return center + width / 2;
}
protected double getStartY() {
double height = 1 / Double.parseDouble(field_scale.getText());
double center = Double.parseDouble(field_centerY.getText());
return center - height / 2;
}
protected double getEndY() {
double height = 1 / Double.parseDouble(field_scale.getText());
double center = Double.parseDouble(field_centerY.getText());
return center + height / 2;
}
@Override
public final void update(Observable o, Object o1) {
field_centerX.setText(Float.toString((float)
((fractalModel.getEndX() - fractalModel.getStartX()) / 2 + fractalModel.getStartX())));
field_centerY.setText(Float.toString((float)
((fractalModel.getEndY() - fractalModel.getStartY()) / 2 + fractalModel.getStartY())));
field_scale.setText(Float.toString((float)
(1 / (fractalModel.getEndX() - fractalModel.getStartX()))));
}
}
|