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

📄 viewerjulia.java

📁 用java Japplet画julia和Mandrabriot曲线
💻 JAVA
字号:
/**
 * @file   ViewerJulia.java
 * @author Schulei CHU <zleinter@yahoo.com.cn>
 * @date   Mon Nov 10 10:55:06 2008
 * 
 * @brief  plot jula 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>Julia</code> here.
 *
 * while given a complex constant C0 (x+yi), for another Complex
 * Number C, impletementing C = C*C + C0
 * plot color corresponding to #iteration util modlus(C) > 10
 * 
 * @author <a href="mailto:zleinter@yahoo.com.cn">Schulei CHU</a>
 * @version 1.0
 */
class Julia extends JApplet implements ActionListener {
    JTextField myx;
    JTextField myy;
    JButton updateButton;
    private int maxit,convasx,convasy;
    
    /**
     * Creates a new <code>Julia</code> instance.
     *
     * @param itnum an <code>int</code> value maxmium number of iteration
     * @param cx an <code>int</code> value frame's width
     * @param cy an <code>int</code> value frame's height
     */
    public Julia( int itnum, int cx, int cy  ){
        maxit  = (itnum>16)?16:itnum;
        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("Julia Set Plot ",JLabel.LEFT);
        cp.add(juliap);

        JLabel myxp = new JLabel("please set the real part: X",JLabel.LEFT);
        //here you can set initial real part of the Complex constant
        myx= new JTextField(10);
        myx.setText("-1.15");
        cp.add(myxp);
        cp.add(myx);

        JLabel myyp = new JLabel("please set the image part Y: ",JLabel.LEFT);
        //here you can set initial image part of the Complex constant
        myy= new JTextField(10);
        myy.setText("0.25");
        cp.add(myyp);
        cp.add(myy);


        updateButton = new JButton("draw");
        cp.add(updateButton);
        updateButton.addActionListener(this);
    }
    
    /**
     * Describe <code>actionPerformed</code> method here.
     * while receiving the click event of mouse, repaint the picture
     * @param ae an <code>ActionEvent</code> value
     */
    public void actionPerformed(ActionEvent ae) {
        repaint();
    }

    public void paint( Graphics g) {
        g.drawString("(-2,1.5i)",10,40);
        g.drawString("(-2,-1.5i)",10,convasy-10);
        g.drawString("(2,1.5i)",convasx-50,40);
        g.drawString("(2,-1.5i)",convasx-50,convasy-10);
        julia(g, myx, myy);
    }

    
    /**
     * Describe <code>julia</code> method here.
     * method for ploting  julia set
     * @param g a <code>Graphics</code> value Comprising julia set
     * @param myx a <code>JTextField</code> value real part of complex constant
     * @param myy a <code>JTextField</code> value real part of complex constant
     */
    public void julia ( Graphics g, JTextField myx, JTextField myy){
        float f ;
        double x0, y0;
        double x , y , deltax , deltay , xstep , ystep ;
        int i , j , k ,  n , itdiv;
        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};
        try {
            x0 = Double.parseDouble(myx.getText().trim());
        }
        catch (NumberFormatException e) {
            x0=0;
        }

        try {
            y0 = Double.parseDouble(myy.getText().trim());
        }
        catch (NumberFormatException e) {
            y0=0;
        }
        // control screen width and height
        deltax = 3. ;
        deltay = 2.5 ;
        // start value of x , you can use it control the picture
        x = -1.5 ;
        xstep = deltax / convasx ;
        ystep = deltay / convasy ;
        for ( i=0; i < convasx; i++ ) {
            // the same as x
            y = -1.25 ;
            for ( j = convasy; j >= 0; j-- ) {
                ComplexNumber cn = new ComplexNumber(x,y);
                ComplexNumber c0 = new ComplexNumber(x0,y0);
                n = juliait(cn,c0,maxit) ;
                g.setColor(color[maxit - n]);
                g.drawLine(i,j,i,j);
                y = y + ystep ;
            }
            x = x + xstep ;
        }
        g.setColor(Color.black);
        g.drawString("(-2,1.5i)",10,40);
        g.drawString("(-2,-1.5i)",10,convasy-10);
        g.drawString("(2,1.5i)",convasx-50,40);
        g.drawString("(2,-1.5i)",convasx-50,convasy-10);
    }
    
    /**
     * Describe <code>juliait</code> method here.
     * iteration manipulation
     * @param c a <code>ComplexNumber</code> value a choosen complex
     * @param c0 a <code>ComplexNumber</code> value complex constant
     * @param maxit an <code>int</code> value max iteration number
     * @return an <code>int</code> value
     */
    public int juliait ( ComplexNumber c, ComplexNumber c0 , int maxit )
    {
        double  abs;
        int m;
        for  ( m = 0; m < maxit ; m++ ) {
            c = c.multiple( c ).add( c0 );
            abs = c.cabs( c );
            if ( abs >= 8. )
                break ;
        }
        return (m);
    }
}


/**
 * Describe class <code>ViewerJulia</code> here.
 * launch a java applet from command line to plot julia set
 * @author <a href="mailto:zleinter@yahoo.com.cn">Schulei CHU</a>
 * @version 1.0
 */
public class ViewerJulia{
    public static void main(String args[]) {
        JApplet julia = new Julia(15,800,600);
        JFrame frame = new JFrame( "Julia Set" );
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.getContentPane().add( julia );
        frame.setSize( 800,600);
        julia.init(  );
        julia.start(  );
        frame.setVisible( true );
    }               
}

⌨️ 快捷键说明

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