⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mjset.java

📁 Java 语言实现的计算器
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* MJSet.java -- Draw Mandelbrot Set and Julia Set
*
* (C) 2001, Li Caiwei
* Department of Computer Sciences, Zhongshan University, P.R. China
*
* Drag mouse to draw and zoom in the rectangle.
* Click right mouse button to switch between Mandelbrot Set and Julia Set.
* Change window size to change the set scale.
*/

import java.text.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.colorchooser.*;

// MJSetFrame class -- constract frame and menu for draw Mandelbrot Set and Julia Set
class MJSetFrame extends JFrame implements ActionListener,MouseListener,MouseMotionListener,KeyListener,Runnable {
	JMenuBar mb;
	JMenu mFile,mMJSet,mOption,mColor,mLimitCol,mHelp;
	JMenuItem mRedraw,mExit,mMSet0,mMSet,mJSet0,mJSet,mSetn,mAbout;
	JRadioButtonMenuItem rbmStdCol,rbmRndCol,rbmBlackCol,rbmWhiteCol;
	JLabel mjl=new JLabel(" Mandelbrot Set "),
		xa0l=new JLabel(" x0:"),xa0vl=new JLabel("0.0"),
		yb0l=new JLabel(" y0:"),yb0vl=new JLabel("0.0"),
		xal=new JLabel("a:"),xavl=new JLabel("0.000000"),
		ybl=new JLabel("b:"),ybvl=new JLabel("0.000000"),
		dxal=new JLabel("a:[-2.2,0.6]"),dybl=new JLabel("b:[-1.75,1.75]"),
		nl=new JLabel(" n:"),nvl=new JLabel("400");
	Color[] cola={Color.red,Color.green,Color.blue,Color.yellow,Color.orange,Color.pink,Color.magenta,
			Color.cyan,Color.lightGray,Color.gray,Color.darkGray};
	Color bgCol=Color.white,grayCol=Color.gray;
	Dimension d0=new Dimension(600,400),d=new Dimension(600,400);
	BufferedImage img=new BufferedImage(600,400,BufferedImage.TYPE_INT_ARGB);
	int w,h,mx,my,mx0,my0,mx1,my1,mx2,my2,t,n=400,ncol=10;
	final double xy0=1.75;
	double x,y,x0=0,y0=0,x1=-xy0,y1=-xy0,x2=xy0,y2=xy0,xyr,
		a,b,a0=-0.48176,b0=0.53165,a1=-2.2,b1=-1.2,a2=0.6,b2=1.2;
	NumberFormat nf=NumberFormat.getNumberInstance(),nfm=NumberFormat.getNumberInstance();
	boolean drag=false,first=true,mj=true,randCol=false,esc=false,frst=true,bw=true,rndColSet=false;
	int blckCol=Color.black.getRGB(),whtCol=Color.white.getRGB();
	int[] stdCol=new int[ncol],rndCol;
	Graphics g,ig;
	InputDlg dlg=null;
	JPanel sb=new JPanel();

	// panel object for drawing
	JPanel p=new JPanel(){
		// draw image
		public void paint(Graphics g) {
			if(first) {d0=getSize(); threadDraw(); first=false;}
			else if(img!=null) {
				d=getSize();
				if(d.width==0 || d.height==0) return;
				if(d.width!=d0.width || d.height!=d0.height) {d0=p.getSize(); threadDraw();}
				else g.drawImage(img,0,0,null);
			}
		}
	};

	// constructor
	public MJSetFrame() {
		setTitle("Mandelbrot Set and Julia Set");
		setSize(500,400);
		setLocation(40,40);

		getContentPane().add(p,BorderLayout.CENTER);

		g=p.getGraphics();
		p.setBackground(Color.white);
		p.addMouseListener(this);
		p.addMouseMotionListener(this);
		addKeyListener(this);

		nf.setMaximumFractionDigits(6);
		nfm.setMaximumFractionDigits(6);
		nfm.setMinimumFractionDigits(6);

		// pull-down menu bar
		mb=new JMenuBar();
		setJMenuBar(mb);

		// file menu
		mFile=new JMenu("File");
		mFile.setMnemonic('F');
		mRedraw=new JMenuItem("Redraw",'R');
		//mRedraw.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R,InputEvent.CTRL_MASK));
		mRedraw.addActionListener(this);
		mFile.add(mRedraw);
		mFile.addSeparator();
		mExit=new JMenuItem("Exit",'X');
		mExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4,InputEvent.ALT_MASK));
		mExit.addActionListener(this);
		mFile.add(mExit);
		mb.add(mFile);

		// MJSet menu
		mMJSet=new JMenu("MJSet");
		mMJSet.setMnemonic('M');
		mMSet=new JMenuItem("Mandelbrot Set...",'M');
		mMSet.addActionListener(this);
		mMJSet.add(mMSet);
		mJSet=new JMenuItem("Julia Set...",'J');
		mJSet.addActionListener(this);
		mMJSet.add(mJSet);
		mMJSet.addSeparator();
		mMSet0=new JMenuItem("Mandelbrot Set 0",'0');
		mMSet0.addActionListener(this);
		mMJSet.add(mMSet0);
		mJSet0=new JMenuItem("Julia Set 0",'l');
		mJSet0.addActionListener(this);
		mMJSet.add(mJSet0);
		mb.add(mMJSet);

		// Option menu
		mOption=new JMenu("Option");
		mOption.setMnemonic('O');
		mColor=new JMenu("Color");
		mColor.setMnemonic('C');
		ButtonGroup cg=new ButtonGroup();
		rbmStdCol=new JRadioButtonMenuItem("Standard Colors",true);
		rbmStdCol.setMnemonic('S');
		rbmStdCol.addActionListener(this);
		rbmStdCol.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));
		mColor.add(rbmStdCol);
		cg.add(rbmStdCol);
		rbmRndCol=new JRadioButtonMenuItem("Random Colors");
		rbmRndCol.setMnemonic('R');
		rbmRndCol.addActionListener(this);
		rbmRndCol.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R,InputEvent.CTRL_MASK));
		mColor.add(rbmRndCol);
		cg.add(rbmRndCol);
		mOption.add(mColor);
		mLimitCol=new JMenu("Limit Color");
		mLimitCol.setMnemonic('L');
		ButtonGroup lcg=new ButtonGroup();
		rbmBlackCol=new JRadioButtonMenuItem("Black",true);
		rbmBlackCol.setMnemonic('B');
		rbmBlackCol.addActionListener(this);
		rbmBlackCol.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_B,InputEvent.CTRL_MASK));
		mLimitCol.add(rbmBlackCol);
		lcg.add(rbmBlackCol);
		rbmWhiteCol=new JRadioButtonMenuItem("White",false);
		rbmWhiteCol.setMnemonic('W');
		rbmWhiteCol.addActionListener(this);
		rbmWhiteCol.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W,InputEvent.CTRL_MASK));
		mLimitCol.add(rbmWhiteCol);
		lcg.add(rbmWhiteCol);
		mOption.add(mLimitCol);

		mSetn=new JMenuItem("Max Repeat Times...",'M');
		mSetn.addActionListener(this);
		mSetn.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK));
		mOption.add(mSetn);
		mb.add(mOption);

		// help menu
		mHelp=new JMenu("Help");
		mHelp.setMnemonic('H');
		mAbout=new JMenuItem("About...",'A');
		mAbout.addActionListener(this);
		mHelp.add(mAbout);
		mb.add(mHelp);

		// add labels to menu bar
		JSeparator sp1=new JSeparator(JSeparator.VERTICAL);
		//sp1.setPreferredSize(new Dimension(5,0));
		mb.add(sp1);
		mjl.setBorder(BorderFactory.createLoweredBevelBorder());
		mb.add(mjl);
		xa0vl.setBorder(BorderFactory.createLoweredBevelBorder());
		yb0vl.setBorder(BorderFactory.createLoweredBevelBorder());
		mb.add(xa0l); mb.add(xa0vl);
		mb.add(yb0l); mb.add(yb0vl);
		nvl.setBorder(BorderFactory.createLoweredBevelBorder());
		mb.add(nl); mb.add(nvl);

		// add labels to status bar
		dxal.setBorder(BorderFactory.createLoweredBevelBorder());
		dybl.setBorder(BorderFactory.createLoweredBevelBorder());
		sb.add(dxal); sb.add(dybl);
		JSeparator sp2=new JSeparator(JSeparator.VERTICAL);
		sp2.setPreferredSize(new Dimension(5,0));
		sb.add(sp2);
		//xal.setBorder(BorderFactory.createLoweredBevelBorder());
		//ybl.setBorder(BorderFactory.createLoweredBevelBorder());
		xavl.setBorder(BorderFactory.createLoweredBevelBorder());
		ybvl.setBorder(BorderFactory.createLoweredBevelBorder());
		sb.add(xal); sb.add(xavl);
		sb.add(ybl); sb.add(ybvl);
		sb.setLayout(new FlowLayout(FlowLayout.LEFT));
		//sb.setBorder(BorderFactory.createLoweredBevelBorder());
		getContentPane().add(sb,BorderLayout.SOUTH);
		for(int i=0;i<ncol;i++) stdCol[i]=cola[i].getRGB();
	}

	// implement the single method in interface ActionListener
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() instanceof JMenuItem) {
			String s=e.getActionCommand();
			if(s.equals("Redraw")) threadDraw(); // redraw set
			else if(s.equals("Exit")) System.exit(0); // exit
			// draw set
			else if(s.equals("Mandelbrot Set...")) {
				mj=true;
				if(doDlg()) {setLabelText(); threadDraw();}
			}
			else if(s.equals("Julia Set...")) {
				mj=false;
				if(doDlg()) {setLabelText(); threadDraw();}
			}
			else if(s.equals("Mandelbrot Set 0")) {
				mj=true;
				x0=0; y0=0;
				a1=-2.2; a2=0.6; b1=-1.25; b2=1.25;
				setLabelText(); threadDraw();
			}
			else if(s.equals("Julia Set 0")) {
				mj=false;
				x1=-xy0; x2=xy0; y1=-xy0; y2=xy0;
				setLabelText(); threadDraw();
			}
			// colore option
			else if(s.equals("Standard Colors")) {if(randCol) {randCol=false; threadDraw();}}
			else if(s.equals("Random Colors")) {
				if(!randCol) {
					randCol=true; threadDraw();
					if(!rndColSet)setRndCol();
				}
			}
			else if(s.equals("Black")) {if(!bw) {bw=true; threadDraw();}}
			else if(s.equals("White")) {if(bw) {bw=false; threadDraw();}}
			// set max repeat times
			else if(s.equals("Max Repeat Times...")) {
				s=(String)JOptionPane.showInputDialog(this,"max repeat times:",
					"Set Max Repeat Times",JOptionPane.PLAIN_MESSAGE,null,null,""+n);
				if(s!=null) {
					n=Integer.parseInt(s); if(n<10) n=10;
					nvl.setText(""+n);
					if(randCol) setRndCol(); else rndColSet=false;
					threadDraw();
				}
			}
			// open About dialog
			else if(s.equals("About...")) JOptionPane.showMessageDialog(this,
				" Drawing Mandelbrot Set and Julia Set\n (C) 2001 Li Caiwei",
				"Copyright Information",JOptionPane.PLAIN_MESSAGE);

		}
	}

	// implement the five methods in interface MouseListener
	public void mouseReleased(MouseEvent e) {
		if(drag) {
			g.setPaintMode();
			drag=false;
			if(mj) {
				a2=a1+mx2*xyr; a1+=mx1*xyr;
				b1=b2-my2*xyr; b2-=my1*xyr;
				dxal.setText("a:["+nf.format(a1)+","+nf.format(a2)+"]");
				dybl.setText("b:["+nf.format(b1)+","+nf.format(b2)+"]");
				if(a2>a1 && b2>b1) threadDraw();
			}
			else {
				x2=x1+mx2*xyr; x1+=mx1*xyr;
				y1=y2-my2*xyr; y2-=my1*xyr;
				dxal.setText("x:["+nf.format(x1)+","+nf.format(x2)+"]");
				dybl.setText("y:["+nf.format(y1)+","+nf.format(y2)+"]");
				if(x2>x1 && y2>y1) threadDraw();
			}
		}
		else if(e.isPopupTrigger()) {
			if(mj) {
				a0=a1+e.getX()*xyr; b0=b2-e.getY()*xyr;
				x1=-xy0; x2=xy0; y1=-xy0; y2=xy0;
				mj=false; setLabelText(); threadDraw();
			}
			else {
				x0=x1+e.getX()*xyr; y0=y2-e.getY()*xyr;
				a1=-2.2; a2=0.6; b1=-1.25; b2=1.25;
				mj=true; setLabelText(); threadDraw();
			}
		}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -