/* * 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 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)); } }