📄 julia.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Julia extends MainPanel implements MouseListener,MouseMotionListener,ActionListener{
double p, q;
int xs,ys,xe,ye,flag=1;
double xmin=-1.5, ymin=-1.5, xmax=1.5,ymax=1.5,zex=0,zey=0;
Graphics g1=getGraphics();
public int juliaZ(double x0,double y0) {
double xk,yk;
int i;
for (i=0;i<47;i++) {
xk=x0*x0-y0*y0+p;
yk=2*x0*y0+q;
if (xk*xk+yk*yk>4) return 200*i;
x0=xk;
y0=yk;
}
return i;
}
public void init(){
pl=new JLabel("P:");
ql=new JLabel("Q:");
pt=new JTextField("-0.46");
qt=new JTextField("0.57");
ok=new JButton("确定");
bk=new JButton("放大");
c.setLayout(new BorderLayout());
pe.setLayout(new FlowLayout());
pe.add(pl);
pe.add(pt);
pe.add(ql);
pe.add(qt);
pe.add(ok);
pe.add(bk);
c.add(pe,BorderLayout.EAST);
addMouseMotionListener(this);
addMouseListener(this);
ok.addActionListener(this);
bk.addActionListener(this);
}
public Julia(){
setLocation(150,100);
setDefaultCloseOperation(2);
}
public void paint (Graphics g) {
super.paint(g);
int x, y;
double tempxmin,tempymin;
tempxmin=xmin;
tempymin=ymin;
Color colJulia = new Color(100,0,100); // 设置颜色
Color col =Color.red ;
// p = -0.46175;
// q = 0.57850;
p=Double.parseDouble(pt.getText());
q=Double.parseDouble(qt.getText());
zex=(xmax-xmin)/800;
zey=(ymax-ymin)/560;
System.out.println("zex"+zex+" zey"+zey);
for (y=0;y<560;y++) {
tempxmin=xmin;
for (x=0;x<801;x++) {
if (juliaZ(tempxmin,tempymin)==47) {
g.setColor(colJulia);
g.drawLine(x,y,x,y);
}
else if(juliaZ(tempxmin,tempymin)>47){
g.setColor(new Color(juliaZ(tempxmin,tempymin)));
g.drawLine(x,y,x,y);
}
tempxmin=tempxmin+zex;
}
tempymin=tempymin+zey;
}
}
public void mousePressed(MouseEvent e) {
xs=e.getX();
ys=e.getY();
System.out.println(xs+" "+ys);
}
public void mouseReleased(MouseEvent e) {
double tempxmin=xmin,tempymin=ymin,tempxmax=xmax,tempymax=ymax;
xmin=tempxmin+xs*(tempxmax-tempxmin)/800.0;
xmax=tempxmin+xe*(tempxmax-tempxmin)/800.0;
ymin=tempymin+ys*(tempymax-tempymin)/560.0;
ymax=ymin+(xmax-xmin)*560/800;
System.out.println(xmin+" "+xmax+" "+ymin+" "+ymax);
g1.setColor(Color.yellow);
g1.drawRect(xs, ys, xe-xs, ye-ys);
bk.setText("放大");
flag=2;
//repaint
}
public void mouseDragged(MouseEvent e)
{
xe=e.getX();
ye=e.getY();
}
public void mouseClicked(MouseEvent e) {
// TODO 自动生成方法存根
}
public void mouseEntered(MouseEvent e) {
// TODO 自动生成方法存根
}
public void mouseExited(MouseEvent e) {
// TODO 自动生成方法存根
}
public void mouseMoved(MouseEvent e) {
// TODO 自动生成方法存根
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==ok) {
repaint();
}
else if(e.getSource()==bk){
if(flag==1)
{
xmin=ymin=-1.5;
xmax=ymax=1.5;
zex=3/800;
zey=3/560;
repaint();
}
else if(flag==2){
repaint();
flag=1;
bk.setText("取消");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -