📄 mjset.java
字号:
}
public void mousePressed(MouseEvent e) {
mx0=e.getX(); my0=e.getY(); // set first point
}
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
// implement the two methods in interface MouseMotionListener
public void mouseMoved(MouseEvent e) {
int xm=e.getX(),ym=e.getY();
if(xm<w && ym<h)
p.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
else p.setCursor(Cursor.getDefaultCursor());
if(mj) {
a=a1+xm*xyr; b=b2-ym*xyr;
xavl.setText(nfm.format(a)); ybvl.setText(nfm.format(b));
}
else {
x=x1+xm*xyr; y=y2-ym*xyr;
xavl.setText(nfm.format(x)); ybvl.setText(nfm.format(y));
}
}
public void mouseDragged(MouseEvent e) { // draw temporal shape
if(drag) drawRectangle(); else drag=true;
mx=e.getX(); my=e.getY();
drawRectangle();
}
// implement the three methods in interface KeyListener
public void keyPressed(KeyEvent e) {
int key=e.getKeyCode();
if(key==KeyEvent.VK_ESCAPE) esc=true;
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
// implement the single method in interface Runnable
public void run(){
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
if(mj) drawMJSet(a1,a2,b1,b2); else drawMJSet(x1,x2,y1,y2);
setCursor(Cursor.getDefaultCursor());
}
// method of create thread and start drawing
void threadDraw() {
Thread runDraw=new Thread(this);
runDraw.start();
}
// method of drawing xor rectangle
void drawRectangle() {
mx1=mx0; my1=my0; mx2=mx; my2=my;
g=p.getGraphics();
g.setColor(grayCol);
g.setXORMode(bgCol);
if(mx1>mx2) {t=mx1; mx1=mx2; mx2=t;}
if(my1>my2) {t=my1; my1=my2; my2=t;}
g.drawRect(mx1,my1,mx2-mx1,my2-my1);
}
// method of drawing Mandelbrot set or Julia set
void drawMJSet(double x1,double x2,double y1,double y2) {
int i,j,k;
double x,y,a,b,xx,yy,aa,bb,xr=(x2-x1)/w,yr=(y2-y1)/h;
d=p.getSize();
w=d.width; h=d.height;
xr=(x2-x1)/w; yr=(y2-y1)/h;
g=p.getGraphics();
g.setColor(bgCol);
g.fillRect(0,0,w,h);
img=new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
ig=img.getGraphics();
ig.setColor(bgCol);
ig.fillRect(0,0,w,h);
if(xr>yr) {xyr=xr; h=(int)((y2-y1)/xyr+0.5);}
else {xyr=yr; w=(int)((x2-x1)/xyr+0.5);}
for(i=0;i<w;i++) {
for(j=0;j<h;j++) {
k=0;
if(mj){x=x0; y=y0; a=a1+i*xyr; b=b1+j*xyr;}
else {a=a0; b=b0; x=x1+i*xyr; y=y1+j*xyr;}
do {
xx=x*x-y*y+a; yy=2*x*y+b;
x=xx; y=yy;
k++;
if(k==n) break;
} while(x*x+y*y<4);
if(k==n) img.setRGB(i,h-j-1,bw?blckCol:whtCol);
else img.setRGB(i,h-j-1,randCol?rndCol[k]:stdCol[k%ncol]);
}
g.drawImage(img,i,0,1,h,i,0,1,h,null);
if(esc) {esc=false; break;}
}
}
// method of create color objects
void setRndCol() {
int i,red,green,blue;
rndCol=new int[n];
Random rndm=new Random(0);
for(i=0;i<n;i++) {
red=rndm.nextInt(256);
green=rndm.nextInt(256);
blue=rndm.nextInt(256);
if((red==255 && green==255 && blue==255) || (red==0 && green==0 && blue==0)) i--;
else rndCol[i]=(red<<16)+(green<<8)+blue;
}
rndColSet=true;
}
// method of set label's text
void setLabelText() {
if(mj) {
mjl.setText(" Mandelbrot Set ");
xa0l.setText(" x0:"); xa0vl.setText(nf.format(x0));
yb0l.setText(" y0:"); yb0vl.setText(nf.format(y0));
dxal.setText("a:["+nf.format(a1)+","+nf.format(a2)+"]");
dybl.setText("b:["+nf.format(b1)+","+nf.format(b2)+"]");
xal.setText(" a:"); ybl.setText(" b:");
}
else {
mjl.setText(" Julia Set ");
xa0l.setText(" a0:"); xa0vl.setText(nf.format(a0));
yb0l.setText(" b0:"); yb0vl.setText(nf.format(b0));
dxal.setText("x:["+nf.format(x1)+","+nf.format(x2)+"]");
dybl.setText("y:["+nf.format(y1)+","+nf.format(y2)+"]");
xal.setText(" x:"); ybl.setText(" y:");
}
}
// method of do input dialog
boolean doDlg() {
ParamInfo prm=new ParamInfo(this,mj);
if(dlg==null) dlg=new InputDlg(this,mj);
if(dlg.showDlg(prm)) {
if(mj) {
x0=Double.parseDouble(prm.xa0s); y0=Double.parseDouble(prm.yb0s);
a1=Double.parseDouble(prm.ax1s); a2=Double.parseDouble(prm.ax2s);
b1=Double.parseDouble(prm.by1s); b2=Double.parseDouble(prm.by2s);
if(a2<=a1) a2=a1+0.1; if(b2<=b1) b2=b1+0.1;
}
else {
a0=Double.parseDouble(prm.xa0s); b0=Double.parseDouble(prm.yb0s);
x1=Double.parseDouble(prm.ax1s); x2=Double.parseDouble(prm.ax2s);
y1=Double.parseDouble(prm.by1s); y2=Double.parseDouble(prm.by2s);
if(x2<=x1) x2=x1+0.1; if(y2<=y1) y2=y1+0.1;
}
return true;
}
return false;
}
}
// ParamInfo class -- used for change parameters between input dialog and frame
class ParamInfo {
public String xa0s,yb0s,ax1s,ax2s,by1s,by2s;
NumberFormat nf=NumberFormat.getNumberInstance();
public ParamInfo(MJSetFrame frm,boolean mj) {
nf.setMaximumFractionDigits(8);
if(mj) {
xa0s=nf.format(frm.x0); yb0s=nf.format(frm.y0);
ax1s=nf.format(frm.a1); ax2s=nf.format(frm.a2);
by1s=nf.format(frm.b1); by2s=nf.format(frm.b2);
}
else {
xa0s=nf.format(frm.a0); yb0s=nf.format(frm.b0);
ax1s=nf.format(frm.x1); ax2s=nf.format(frm.x2);
by1s=nf.format(frm.y1); by2s=nf.format(frm.y2);
}
}
}
// InputDlg class -- dialog for input parameters
class InputDlg extends JDialog implements ActionListener {
JTextField xa0tf,yb0tf,ax1tf,ax2tf,by1tf,by2tf;
JButton okBttn,cancelBttn;
boolean ok;
public InputDlg(MJSetFrame frm,boolean mj) {
super(frm,"Input Parameters",true);
Container cnt=getContentPane();
JPanel p1=new JPanel();
p1.setLayout(new BoxLayout(p1,BoxLayout.X_AXIS));
Box b1,b2,bs,b3,b4;
p1.add(Box.createHorizontalStrut(5));
b1=new Box(BoxLayout.Y_AXIS);
b1.add(Box.createVerticalStrut(8));
b1.add(new JLabel(mj?"x0:":"a0:"));
b1.add(Box.createVerticalStrut(4));
b1.add(new JLabel(mj?"a1:":"x1:"));
b1.add(Box.createVerticalStrut(4));
b1.add(new JLabel(mj?"b1:":"y1:"));
p1.add(b1);
b2=new Box(BoxLayout.Y_AXIS);
b2.add(Box.createVerticalStrut(5));
b2.add(xa0tf=new JTextField(""));
b2.add(ax1tf=new JTextField(""));
b2.add(by1tf=new JTextField(""));
p1.add(b2);
p1.add(Box.createHorizontalStrut(10));
b3=new Box(BoxLayout.Y_AXIS);
b3.add(Box.createVerticalStrut(8));
b3.add(new JLabel(mj?"y0:":"b0:"));
b3.add(Box.createVerticalStrut(4));
b3.add(new JLabel(mj?"a2:":"x2:"));
b3.add(Box.createVerticalStrut(4));
b3.add(new JLabel(mj?"b2:":"y2:"));
p1.add(b3);
b4=new Box(BoxLayout.Y_AXIS);
b4.add(Box.createVerticalStrut(5));
b4.add(yb0tf=new JTextField(""));
b4.add(ax2tf=new JTextField(""));
b4.add(by2tf=new JTextField(""));
p1.add(b4);
p1.add(Box.createHorizontalStrut(5));
cnt.add("Center",p1);
JPanel p2=new JPanel();
okBttn=new JButton("OK"); p2.add(okBttn);
okBttn.addActionListener(this);
p2.add(new JSeparator(JSeparator.VERTICAL));
cancelBttn=new JButton("Cancel"); p2.add(cancelBttn);
cancelBttn.addActionListener(this);
cnt.add("South",p2);
setSize(240,140);
setLocation(200,100);
setResizable(false);
}
public void actionPerformed(ActionEvent e) {
//Object src=e.getSource();
if(e.getSource()==okBttn) ok=true; else ok=false;
setVisible(false);
}
public boolean showDlg(ParamInfo prm) {
xa0tf.setText(prm.xa0s); yb0tf.setText(prm.yb0s);
ax1tf.setText(prm.ax1s); ax2tf.setText(prm.ax2s);
by1tf.setText(prm.by1s); by2tf.setText(prm.by2s);
show();
if(ok) {
prm.xa0s=xa0tf.getText(); prm.yb0s=yb0tf.getText();
prm.ax1s=ax1tf.getText(); prm.ax2s=ax2tf.getText();
prm.by1s=by1tf.getText(); prm.by2s=by2tf.getText();
}
return ok;
}
}
// MJSet class -- main for draw Mandelbrot set and Julia set
public class MJSet {
public static void main(String[] args){
MJSetFrame frame=new MJSetFrame();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
frame.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -