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

📄 viewermandelbrot.java

📁 用java Japplet画julia和Mandrabriot曲线
💻 JAVA
字号:
/**
 * @file   ViewerMandelbrot.java
 * @author Schulei CHU <zleinter@yahoo.com.cn>
 * @date   Mon Nov 10 10:55:06 2008
 * 
 * @brief  plot Mandeblrot set
 * attention: I am a java beginner, I have not much
 * knowledge about java applet, so the GUI is ugly
 * very much. 
 * 
 */
import java.io.*;
import java.lang.String;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


/**
 * Describe class <code>Mandelbrot</code> here.
 *  for a give complex sequence C(n)(C(0)=0) and Cc
 *  implement iteration C = C*C+Cc
 *  plot color correspnding to #iteration util modlus(c) > 2
 * @author <a href="mailto:zleinter@yahoo.com.cn">Schulei CHU</a>
 * @version 1.0
 */
class Mandelbrot extends JApplet implements ActionListener {
    JTextField myx;
    JTextField myy;
    JTextField mdeltayx;
    JTextField mydeltay;
    JTextField mymaxit;
    JButton updateButton;
    public int convasx;
    public int convasy;
    public Mandelbrot(int cx, int cy  ){
        convasx = cx;
        convasy = cy;
    }

    /** 
     * prepare frame and other components for a java applet
     * 
     */
    public void init() {
        Container cp = getContentPane();
        cp.setLayout( new FlowLayout() );
        JLabel juliap = new JLabel("Mandelbrot Set Plot ",JLabel.LEFT);
        cp.add(juliap);
        
        JLabel myxp = new JLabel("real (x) coord (-2 to +1): ",JLabel.LEFT);
        myx= new JTextField(10);
        myx.setText("-2");
        cp.add(myxp);
        cp.add(myx);

        JLabel myyp = new JLabel("imaginary (y) coord (-1.5 to +1.5): ",JLabel.LEFT);
        myy= new JTextField(10);
        myy.setText("-1.5");
        cp.add(myyp);
        cp.add(myy);

        JLabel mdeltayxp = new JLabel("screen width (0 to 3): ",JLabel.LEFT);
        mdeltayx= new JTextField(3);
        mdeltayx.setText("3");
        cp.add(mdeltayxp);
        cp.add(mdeltayx);

        JLabel mydeltayp = new JLabel("screen height (0 to 3): ",JLabel.LEFT);
        mydeltay= new JTextField(3);
        mydeltay.setText("3");
        cp.add(mydeltayp);
        cp.add(mydeltay);

        JLabel mymaxitp = new JLabel("max iterations (16 to 16384 by powers of 2): ",JLabel.LEFT);
        mymaxit= new JTextField(8);
        mymaxit.setText("256");
        cp.add(mymaxitp);
        cp.add(mymaxit);
        
        updateButton = new JButton("Draw");
        cp.add(updateButton);
        updateButton.addActionListener(this);
    }

    public void actionPerformed(ActionEvent ae) {
        repaint();
    }
    
    public void paint( Graphics g) {
        mandelbrot(g, myx, myy, mdeltayx, mdeltayx, mymaxit ); 
    }

    
    /**
     * Describe <code>mandelbrot</code> method here.
     * method for ploting julia set
     * @param g a <code>Graphics</code> value
     * @param myx a <code>JTextField</code> value real part of complex Cc
     * @param myy a <code>JTextField</code> value image part of complex Cc
     * @param mdeltayx a <code>JTextField</code> value screen size x
     * @param mydeltay a <code>JTextField</code> value screen size y
     * @param mymaxit a <code>JTextField</code> value max iteration number
     */
    public void mandelbrot ( Graphics g, JTextField myx, JTextField myy, JTextField mdeltayx, JTextField mydeltay, JTextField mymaxit){
        float f ;
        double x , y , x0 , y0 , deltax , deltay , stepx , stepy ;
        int i , j , l , n , maxit,modcolor=16;
        // color set
        Color color[] = { Color.black,
                      Color.gray,
                      Color.lightGray,
                      Color.red,
                      Color.green,
                      Color.blue,
                      Color.cyan,
                      Color.magenta,
                      Color.yellow,
                      Color.pink,
                      Color.green,
                      Color.blue,
                      Color.cyan,
                      Color.magenta,
                      Color.yellow,
                      Color.white};
        // get parameters
        try {
            x0 = Double.parseDouble(myx.getText().trim());
        }
        catch (NumberFormatException e) {
            x0=-2;
        }
        
        try {
            y0 = Double.parseDouble(myy.getText().trim());
        }
        catch (NumberFormatException e) {
            y0=-1.5;
        }

        // deltax and delta y is designed to control the screen size
        try {
            deltax = Double.parseDouble(mdeltayx.getText().trim());
        }
        catch (NumberFormatException e) {
            deltax=3;
        }

        try {
            deltay = Double.parseDouble(mydeltay.getText().trim());
        }
        catch (NumberFormatException e) {
            deltay=3;
        }
        
        try {
            maxit = (int)Double.parseDouble(mymaxit.getText().trim());
        }
        catch (NumberFormatException e) {
            maxit=256;
        }
        if(maxit < 16)
            maxit = 16 ;
        else if(maxit > 16384)
            maxit = 16384 ;
        stepx = deltax / convasx ;
        stepy = deltay / convasy ;        
        g.setColor(color[1]);
        x=x0 ;
        for ( i=0; i <= convasx; i++ ) {
            y=y0 ;
            for ( j= convasy; j >= 0; j-- ) {
                ComplexNumber cn = new ComplexNumber( x,y );
                n = mandelbrotit(cn,maxit) ;
                g.setColor(color[n%modcolor]);
                g.drawLine(i,j,i,j);
                y = y + stepy ;
            }
            x = x + stepx ;
        }
    }
    
    /**
     * Describe <code>mandelbrotit</code> method here.
     * method for iteration
     * @param c a <code>ComplexNumber</code> value Complex number for iteration
     * @param maxit an <code>int</code> value maxmium iteration number
     * @return an <code>int</code> value
     */
    public int mandelbrotit ( ComplexNumber c , int maxit )
    {
        double abs;
        int itnum;
        ComplexNumber cn = new ComplexNumber( 0,0 );
        for  ( itnum = 0; itnum < maxit ; itnum++ ) {
            cn = cn.multiple( cn ).add( c );
            abs = cn.cabs( cn );
            if ( abs >= 2. )
                break ;
        }
        return (itnum);
    }

}


public class ViewerMandelbrot{
    public static void main(String args[]) {
        JApplet Mandelbrot = new Mandelbrot(800,600);
        JFrame frame = new JFrame( "Mandelbrot" );
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.getContentPane().add( Mandelbrot );
        frame.setSize(800,600);
        Mandelbrot.init(  );
        Mandelbrot.start(  );
        frame.setVisible( true );
    }               
}

⌨️ 快捷键说明

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