I want to have several EventHandler in just one class, but I don\'t know how to
ID: 3847271 • Letter: I
Question
I want to have several EventHandler in just one class, but I don't know how to works, I have the code as the following, please check them out. I want to when I click exit on the JMenuItem, the jfram should exit, when I click Mandelbrot Set on the JMenuItem, it should output Mandelbrot Set text message in the console.
package code;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
//import edu.buffalo.fractal.FractalPanel;
public class UI implements Runnable {
// private FractalPanel panel;
private JMenuBar menu = new JMenuBar();
JMenuItem quit = new JMenuItem("exit");
JMenuItem items = new JMenuItem("Mandelbrot Set");
public void run() {
JFrame jframe = new JFrame("fractal");
// panel = new FractalPanel();
jframe.setVisible(true);
jframe.setSize(500, 500);
jframe.setResizable(true);
// jframe.add(panel);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/************************************************************************************************/
// Below codes are for the menu bar
jframe.setJMenuBar(menu);
// imported the Menu into the JFrame
JMenu file = new JMenu("File");
// File on the menu
menu.add(file);
//quit.addActionListener(new EventHandler());
// Stored quit into the file JMenu
file.add(quit);
JMenu fractal = new JMenu("Fractal");
// Fractal on the menu
menu.add(fractal);
//items.addActionListener(new EventHandler1());
JMenuItem items1 = new JMenuItem("Julia Set");
JMenuItem items2 = new JMenuItem("Burning Ship Set");
JMenuItem items3 = new JMenuItem("Multibrot Set");
// Stored Items for each fractal into the file JMenu
fractal.add(items);
fractal.add(items1);
fractal.add(items2);
fractal.add(items3);
JMenu color = new JMenu("Color");
// Color on the menu
menu.add(color);
JMenuItem colors = new JMenuItem("Blue");
JMenuItem colors1 = new JMenuItem("Yellow");
JMenuItem colors2 = new JMenuItem("Green");
JMenuItem colors3 = new JMenuItem("Black");
// Stored Items for each of your color schemes into the file JMenu
color.add(colors);
color.add(colors1);
color.add(colors2);
color.add(colors3);
jframe.pack();
/************************************************************************************************/
}
}
package code;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class EventHandler implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
Object quit = new UI().quit;
Object item = new UI().items;
Object src = e.getSource();
if ((src == quit)) {
System.exit(0); // exit the program
} else if (src == item) {
System.out.println("Mandelbrot Set"); // output Mandelbrot Set
}
}
}
Explanation / Answer
import java.awt.Color;
public class Mandelbrot {
// return number of iterations to check if c = a + ib is in Mandelbrot set
public static int mand(Complex z0, int max) {
Complex z = z0;
for (int t = 0; t < max; t++) {
if (z.abs() > 2.0) return t;
z = z.times(z).plus(z0);
}
return max;
}
public static void main(String[] args) {
double xc = Double.parseDouble(args[0]);
double yc = Double.parseDouble(args[1]);
double size = Double.parseDouble(args[2]);
int n = 512; // create n-by-n image
int max = 255; // maximum number of iterations
Picture picture = new Picture(n, n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
double x0 = xc - size/2 + size*i/n;
double y0 = yc - size/2 + size*j/n;
Complex z0 = new Complex(x0, y0);
int gray = max - mand(z0, max);
Color color = new Color(gray, gray, gray);
picture.set(i, n-1-j, color);
}
}
picture.show();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.