Question
I have two classes below, one is mandelbrot and the other is a menu, which I want to use, to set the initial values for
I have two classes below, one is mandelbrot and the other is a menu, which I want to use, to set the initial values for mandelbrot and launch it, but I don't know how. Right now they both have a main method, and I want to know how to make the menu work to initialize mandelbrot.
import java.util.Scanner;
import java.awt.*;
import java.io.*;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Mandelbrot {
public static final int COLORDEPTH=256;
public static final int GRIDSIZE=768;
public static final boolean CONSOLEINPUT=false;
private static double centerRe;
private static double centerIm;
private static double sidelength;
private static double centerReMandel;
private static double centerImMandel;
private static double sidelengthMandel;
private static boolean mandelbrot = true;
private static Complex juliaConstant;
private static int[][] drawingData;
private static int maxIterations = 255;
private static Complex[][] complexGrid;
public static void main(String[] args) throws FileNotFoundException {
String filename = "";
centerRe = -0.5;
centerIm = 0;
sidelength = 2;
centerReMandel = centerRe;
centerImMandel = centerIm;
sidelengthMandel = sidelength;
int[][] colors = loadColors(filename);
redrawAll(colors);
//Entering endless while loop when the Mandelbrot set have been drawn the first time
while(true) {
char key = ' ';
boolean mousepressed=false;
while(!(StdDraw.mousePressed()|| StdDraw.hasNextKeyTyped() )); // Wait for mouse or keyboard press
if(StdDraw.mousePressed()){
int corX = (int)Math.round(StdDraw.mouseX()); // Read mouse position
int corY = (int)Math.round(StdDraw.mouseY());
if (corX >= 0 && corX < GRIDSIZE && corY >= 0 && corY < GRIDSIZE ){
centerRe = complexGrid[corX][corY].getRe();
centerIm = complexGrid[corX][corY].getIm();
mousepressed=true;
}
}
if (StdDraw.hasNextKeyTyped()){
key = Character.toLowerCase(StdDraw.nextKeyTyped());
}
while(StdDraw.mousePressed() || StdDraw.hasNextKeyTyped()); // Wait for release
if (mousepressed){ //Sets a new center
redrawAll(colors);
}else if(key=='i'){ //Prompts the user for a new number of iterations
int iterations=dialogInputIterations();
if (iterations!=0)
maxIterations=iterations;
else
JOptionPane.showMessageDialog(null,"Input is not an integer. "+maxIterations+" iterations is used","Input is not an integer",JOptionPane.WARNING_MESSAGE);
redrawAll(colors);
}else if(key=='w'){ //Tells the user the complex coordinates of the center of the drawing panel
JOptionPane.showMessageDialog(null,"Coordinates of the center is: "+complexGrid[GRIDSIZE/2][GRIDSIZE/2].toString(),"Center coordinates",JOptionPane.INFORMATION_MESSAGE);
}else if (key=='c'){ //Promts the user for a new name for a color file
colors=loadColors(dialogInputFilename());
drawMandelbrot(drawingData, colors);
}else if(key=='+'){ //Zooms to double the current size
sidelength /= Math.sqrt(2);
redrawAll(colors);
}else if(key=='-'){ //Zooms to half the current size
sidelength *= Math.sqrt(2);
redrawAll(colors);
}else if(key=='j'){ //Draws the Juliaset corresponding to the coordinates of the center
if (mandelbrot){
juliaConstant = new Complex(centerRe , centerIm);
sidelengthMandel = sidelength;
centerReMandel = centerRe;
centerImMandel = centerIm;
mandelbrot = false;
sidelength = 4;
centerRe = 0;
centerIm = 0;
redrawAll(colors);
}
} else if(key=='m'){ //Draws the Mandelbrot set
if(!mandelbrot){
sidelength = sidelengthMandel;
centerRe = centerReMandel;
centerIm = centerImMandel;
mandelbrot = true;
redrawAll(colors);
}
} else if(key=='g'){ //Prompts the user for a place and zoom-level to go to
centerRe = dialogInputDouble("Enter the real coordinate of the center","New center");
centerIm = dialogInputDouble("Enter the imaginary coordinate of the center","New center");
sidelength = dialogInputDouble("Enter the sidelength","Sidelength");
redrawAll(colors);
} else if(key=='h'){ //Display help box
JOptionPane.showMessageDialog(null,"Click mouse to set new center Keys: 'i' - iterations - sets iterations 'w' - where am I? - tells you the coordintes of the center 'c' - colors - loads new color file 'g' - go to - Enter coordinates and zoom level to go to '+' - zooms to double size '-' - zooms to half size 'j' - draws the Julia set for the center 'm' - draws the Mandelbrot set","Help",JOptionPane.PLAIN_MESSAGE);
}
}
}
/**
* Recalculates everything and redraws the canvas
* @param colors the array containing the color values
*/
public static void redrawAll(int[][] colors){
initializeCanvas();
Complex center = new Complex(centerRe,centerIm);
complexGrid = createComplexGrid(center, sidelength);
drawingData = getDrawingData();
drawMandelbrot(drawingData, colors);
}
/**
* Initializes the canvas. After first initilization it is only used to show the message "Please wait..."
*/
public static void initializeCanvas(){
StdDraw.setCanvasSize(GRIDSIZE,GRIDSIZE);
StdDraw.setXscale(0,GRIDSIZE-1);
StdDraw.setYscale(0,GRIDSIZE-1);
StdDraw.setPenRadius(1.0/GRIDSIZE);
StdDraw.text(GRIDSIZE/2,GRIDSIZE/2, "Please wait..."); //"Please wait..." is shown on the canvas until the data has been calculated and drawn
StdDraw.show(0);
}
/**
* Creates an array of the complex numbers on the canvas
* @param centrum is used to make the complex grid
* @param sidelength is used to make the complex grid
*/
public static Complex[][] createComplexGrid(Complex centrum, double sidelength){
Complex[][] cGrid = new Complex[GRIDSIZE][GRIDSIZE];
for (int i=0; i
for (int j=0; j
double re = centrum.getRe()-(sidelength/2)+((sidelength*i)/(GRIDSIZE-1));
double im = centrum.getIm()-(sidelength/2)+((sidelength*j)/(GRIDSIZE-1));
cGrid[i][j]= new Complex(re,im);
}
}
return cGrid;
}
/**
* Creates an array of the iterations for the corresponding point by iterating the points
* @return returns an array of the number of iterations
*/
public static int[][] getDrawingData(){
int[][] drawingData = new int[GRIDSIZE][GRIDSIZE];
for (int i=0; i
for (int j=0; j
drawingData[i][j]=(int)(iterate(complexGrid[i][j])/((double)maxIterations/((double)COLORDEPTH-1)));
}
}
return drawingData;
}
/**
* Draws the Mandelbrot set (or Julia set)
* @param drawingData an array with numbers of iterations
* @param colors an array with the colors use to draw this
*/
public static void drawMandelbrot(int drawingData[][], int colors[][]){
StdDraw.clear();
for (int i=0; i
for (int j=0; j
if(colors!=null){
StdDraw.setPenColor(new Color(colors[drawingData[i][j]][0], colors[drawingData[i][j]][1], colors[drawingData[i][j]][2]));
} else {
StdDraw.setPenColor(new Color(drawingData[i][j], drawingData[i][j], drawingData[i][j])); //Equal amounts of R, G and B gives a grayscale
}
StdDraw.point(i, j);
}
}
StdDraw.show(0);
}
/**
* Iterates the complex number passed to it.
* The number can either be iterated by the Mandelbrot algorithm or the Julia algorithm
* @param z0 the complex number to iterate
*/
public static int iterate(Complex z0) {
Complex z = new Complex(z0);
if (mandelbrot){
//Iterates the passed complex z after the formula z_next = z^2 +z0
for (int i = 0; i < maxIterations; i++) {
if (z.abs() > 2.0) {
return i;
}
z = z.times(z).plus(z0);
}
return maxIterations;
} else {
//Iterates the passed complex z after the formula z_next = z^2 + c
//c is the complex number for which the Julia set is found
for (int i = 0; i < maxIterations; i++) {
if (z.abs() > 2.0) {
return i;
}
z = z.times(z).plus(juliaConstant);
}
return maxIterations;
}
}
/**
* Prompts the user for a filename for the color file
*/
private static String dialogInputFilename() {
JFrame parent = new JFrame();
String filename = JOptionPane.showInputDialog(parent,"Please write filename (in directory src" + File.separator + "mnd" + File.separator +" )","Filename",JOptionPane.OK_CANCEL_OPTION);
if (filename == null)
return "";
return filename;
}
/**
* Prompts the user for a new value for iterations
* @return Returns an integer typed by the user.
*/
private static int dialogInputIterations() {
JFrame parent = new JFrame();
String input= JOptionPane.showInputDialog(parent,maxIterations+" iterations is currently used Please type a new number of iterations","Iterations",JOptionPane.OK_CANCEL_OPTION);
if (input == null)
return 0;
input = input.replaceAll("[^(0-9)]", ""); //Uses a regular expression to remove alle non-numerical characters from the input
if (input.equals(""))
return 0;
return Integer.parseInt(input);
}
/**
* Prompts the user for a new value for iterations
* @param message What the user is told to type
* @param title The title of the inputdialog
* @return Returns a double typed by the user.
*/
private static double dialogInputDouble(String message, String title) {
JFrame parent = new JFrame();
String input= JOptionPane.showInputDialog(parent,message,title,JOptionPane.OK_CANCEL_OPTION);
return doubleFromString(input);
}
/**
* Finds a double in a string if a such exists. If not, 0 is returned
* @param input String to find a double in
* @return Returns a double found in the string passed. Returns 0 if the input is invalid
*/
private static double doubleFromString(String input){
if (input == null)
return 0;
input = input.replaceAll(",", "."); //Regex replacing ","" with the proper decimal point
input = input.replaceAll("[^(0-9)][.|-]", ""); //Regex removing everything that can not be parsed to a double
if (input.equals(""))
return 0;
double output;
try{
output=Double.parseDouble(input);
} catch(NumberFormatException e) {
JOptionPane.showMessageDialog(null,"Invalid input. 0.0 is used.","Not a valid number!",JOptionPane.WARNING_MESSAGE);
return 0; //Returning 0 if the string can not be parsed to a double (eg. it has more than one decimal point)
}
return output;
}
public static int[][] loadColors(String filename) throws FileNotFoundException{
int[][] colors = new int[COLORDEPTH][3];
if (filename.equals("")){
return null;
}
File colorFile = new File("src" + File.separator + "mnd" + File.separator + filename);
if(!colorFile.exists()){
System.out.print("Invalid filename! Mandelbrot is drawn without colors");
JOptionPane.showMessageDialog(null,"Invalid filename! Mandelbrot is drawn without colors","No such file!",JOptionPane.WARNING_MESSAGE);
return null;
}
Scanner fileScanner = new Scanner(colorFile);
//The following code assumes a properly formatted color file
for (int i=0; i
for (int j=0; j<3; j++){
if (fileScanner.hasNextInt()){
colors[i][j]=fileScanner.nextInt();
}
}
} fileScanner.close();
return colors;
}
}
************************************************************
************************************************************
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Color;
public class MainMenu {
private JFrame frame;
private JTextField textX;
private JTextField textY;
private JTextField textLength;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainMenu window = new MainMenu();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public MainMenu() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblNewLabel = new JLabel("Please enter values to start");
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel.setBounds(6, 20, 438, 16);
frame.getContentPane().add(lblNewLabel);
JLabel lblX = new JLabel("x");
lblX.setBounds(100, 71, 61, 16);
frame.getContentPane().add(lblX);
JLabel lblY = new JLabel("y");
lblY.setBounds(100, 111, 61, 16);
frame.getContentPane().add(lblY);
JLabel lblLength = new JLabel("Length");
lblLength.setBounds(100, 151, 61, 16);
frame.getContentPane().add(lblLength);
textX = new JTextField();
textX.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
char c = e.getKeyChar();
if (!(Character.isDigit(c) || (c == KeyEvent.VK_PERIOD) || c == KeyEvent.VK_MINUS)) {
e.consume();
}
}
});
textX.setBounds(222, 66, 130, 26);
frame.getContentPane().add(textX);
textX.setColumns(10);
textY = new JTextField();
textY.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
char c = e.getKeyChar();
if (!(Character.isDigit(c) || (c == KeyEvent.VK_PERIOD) || c == KeyEvent.VK_MINUS)) {
e.consume();
}
}
});
textY.setBounds(222, 106, 130, 26);
frame.getContentPane().add(textY);
textY.setColumns(10);
textLength = new JTextField();
textLength.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
char c = e.getKeyChar();
if (!(Character.isDigit(c) || (c == KeyEvent.VK_PERIOD) || c == KeyEvent.VK_MINUS)) {
e.consume();
}
}
});
textLength.setBounds(222, 146, 130, 26);
frame.getContentPane().add(textLength);
textLength.setColumns(10);
JButton btnStart = new JButton("Start");
btnStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if ((textX.getText().toString().compareTo("") == 0) || (textY.getText().toString().compareTo("") == 0) || (textLength.getText().toString().compareTo("") == 0) ) {
JOptionPane.showMessageDialog(null, "Invalid input. Please type the missing value(s)");
} else {
String xString = textX.getText();
double centerRe = Double.parseDouble(xString);
String yString = textY.getText();
double centerIm = Double.parseDouble(yString);
String LengthString = textLength.getText();
double sidelength = Double.parseDouble(LengthString);
}
}
});
btnStart.setBounds(167, 221, 130, 29);
frame.getContentPane().add(btnStart);
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started