aboutsummaryrefslogtreecommitdiff
path: root/Week11 Mandelbrot/src/fractals/MainWindow.java
blob: b6276af9163eff073de179bae19b5895787ca190 (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
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;

/**
 * 
 * @author Sjaak Smetsers
 * @version 1.0, 14-03-2013
 */

/**
 * creates a window to which a GridView panel is added
 * 
 */
public class MainWindow implements MandelbrotView.MandelbrotProvider, ZoomFrame.MandelbrotTextFields {
    // the size of the window
    public static final int WIDTH = 650, HEIGHT = 650;
    
    private final String INITIAL_CENTER_X = "0",
            INITIAL_CENTER_Y = "0",
            INITIAL_SCALE = "100",
            INITIAL_REPETITIONS = "100";
    
    // The grip panel
    private final GridView grid;
    
    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");
    
    private final MandelbrotView mandelbrotController;
    
    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);
        
        mandelbrotController = new MandelbrotView(this, grid);
        mandelbrotController.redraw();
        
        mainFrame.setMandelbrotView(mandelbrotController);
        mainFrame.setMandelbrotTextFields(this);
        
        JPanel left = 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")) {
                    mandelbrotController.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);
                mandelbrotController.redraw();
            }
        });
        panel.add(button_reset);
        
        left.add(panel, BorderLayout.NORTH);
        left.add(new JPanel(), BorderLayout.CENTER);
        
        mainFrame.add(left, BorderLayout.EAST);
        
        mainFrame.pack();
    }
    
    /**
     * 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));
    }
    
}