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

📄 metaball.java

📁 JAVA 反走样画直线 可在JAVA1.1下使用
💻 JAVA
字号:
///////////////////////////////////////////////////////////
// DeJaved by mDeJava v1.0. Copyright 1999 MoleSoftware. //
//       To download last version of this software:      //
//            http://molesoftware.hypermatr.net          //
//               e-mail:molesoftware@mail.ru             //
///////////////////////////////////////////////////////////

import java.applet.Applet;
import java.awt.*;
import java.awt.image.IndexColorModel;
import java.awt.image.MemoryImageSource;
import java.util.Random;

public class MetaBall extends Applet
    //implements Runnable
{

    Thread timer = null;
    long oldtime = 0;
    long newtime = 0;
    float timepass = 0;
    float time = 0;
    Matrix pMatrix = null;
    Dimension image_size = null;
    ColorGradient color_gradient = null;
    IndexColorModel color_model = null;
    int aa_buffer_size = 0;
    byte aa_buffer_bytes[] = null;
    MemoryImageSource aa_buffer_memory_source = null;
    Image aa_buffer_image = null;
    float Grid[] = null;
    float thresh = 0;
    Vect Balls[] = null;
    Vect BallFreq[] = null;
    Vect BallOfs[] = null;
    int MIDX = 0;
    int MIDY = 0;
    int MAXX = 0;
    int MAXY = 0;
    int meshData[][] = {
        new int[0], {
            0, 3
        }, {
            1, 0
        }, {
            1, 3
        }, {
            3, 2
        }, {
            0, 2
        }, {
            3, 0, 1, 2
        }, {
            1, 2
        }, {
            2, 1
        }, {
            0, 1, 2, 3
        }, 
        {
            2, 0
        }, {
            2, 3
        }, {
            3, 1
        }, {
            0, 1
        }, {
            3, 0
        }, new int[0]
    };
    float squareData[] = null;
    int wipeMode = 0;

    public MetaBall()
    {
        oldtime = 0L;
        newtime = 0L;
        timepass = 0.0F;
        time = 0.0F;
        color_gradient = new ColorGradient(Color.white, Color.red);
        Grid = new float[4096];
        thresh = 0.25F;
        Balls = new Vect[5];
        BallFreq = new Vect[5];
        BallOfs = new Vect[5];
        MIDX = 128;
        MIDY = 128;
        MAXX = 256;
        MAXY = 256;
        squareData = new float[4];
        wipeMode = 0;
    }

    public void init()
    {
        resize(300,200);

        setImageSize(getSize().width, getSize().height);
        initPalettes();
        initBuffers();
        Random random = new Random();
        for(int i = 0; i < Balls.length; i++)
        {
            BallFreq[i] = new Vect(random.nextFloat() + 0.5F, random.nextFloat() + 0.5F, random.nextFloat() + 0.5F);
            BallOfs[i] = new Vect(random.nextFloat() * 3.14159F, random.nextFloat() * 3.14159F, random.nextFloat() * 3.14159F);
        }

    }

    protected void initPalettes()
    {
        double d = 0.0D;
        double d1 = 0.00390625D;
        byte abyte0[] = new byte[256];
        byte abyte1[] = new byte[256];
        byte abyte2[] = new byte[256];
        for(int i = 0; i < 256; i++)
        {
            abyte0[i] = (byte)color_gradient.getColor(d).getRed();
            abyte1[i] = (byte)color_gradient.getColor(d).getGreen();
            abyte2[i] = (byte)color_gradient.getColor(d).getBlue();
            d += d1;
        }

        color_model = new IndexColorModel(8, 256, abyte0, abyte1, abyte2);
    }

    protected void initBuffers()
    {
        if(image_size == null)
            setImageSize(100, 100);
        aa_buffer_size = image_size.width * image_size.height;
        aa_buffer_bytes = new byte[aa_buffer_size];
        aa_buffer_memory_source = new MemoryImageSource(image_size.width ,image_size.height, color_model, aa_buffer_bytes, 0,image_size.width);
        aa_buffer_memory_source.setAnimated(true);
        aa_buffer_memory_source.setFullBufferUpdates(true);
        aa_buffer_image = Toolkit.getDefaultToolkit().createImage(aa_buffer_memory_source);
    }

    float ballVal(float f, float f1, float f2)
    {
        float f3 = f * f + f1 * f1 + f2 * f2;
        if(f3 > 1.0F)
            return 1.0F / f3;
        else
            return 1.0F;
    }

    void gridFill()
    {
        int i = 0;
        for(int j = 0; j < 16; j++)
        {
            for(int k = 0; k < 16; k++)
            {
                for(int l = 0; l < 16; l++)
                {
                    Grid[i] = 0.0F;
                    for(int i1 = 0; i1 < Balls.length; i1++)
                        Grid[i] += ballVal(((float)l + Balls[i1].d[0]) - 8F, ((float)k + Balls[i1].d[1]) - 8F, ((float)j + Balls[i1].d[2]) - 8F);

                    i++;
                }

            }

        }

    }

    float diff(float f, float f1)
    {
        if(f1 != f)
        {
            float f2 = (thresh - f) / (f1 - f);
            return f2;
        }
        else
        {
            return 0.0F;
        }
    }

    Vect getVectXY(int i, float f, float f1, float f2)
    {
        switch(i)
        {
        case 0: // '\0'
            return new Vect(f + diff(squareData[0], squareData[1]), f1, f2);

        case 1: // '\001'
            return new Vect(f + 1.0F, f1 + diff(squareData[1], squareData[3]), f2);

        case 2: // '\002'
            return new Vect(f + diff(squareData[2], squareData[3]), f1 + 1.0F, f2);

        case 3: // '\003'
            return new Vect(f, f1 + diff(squareData[0], squareData[2]), f2);

        }
        return new Vect(0.0F, 0.0F, 0.0F);
    }

    Vect getVectYZ(int i, float f, float f1, float f2)
    {
        switch(i)
        {
        case 0: // '\0'
            return new Vect(f, f1 + diff(squareData[0], squareData[1]), f2);

        case 1: // '\001'
            return new Vect(f, f1 + 1.0F, f2 + diff(squareData[1], squareData[3]));

        case 2: // '\002'
            return new Vect(f, f1 + diff(squareData[2], squareData[3]), f2 + 1.0F);

        case 3: // '\003'
            return new Vect(f, f1, f2 + diff(squareData[0], squareData[2]));

        }
        return new Vect(0.0F, 0.0F, 0.0F);
    }

    Vect getVectZX(int i, float f, float f1, float f2)
    {
        switch(i)
        {
        case 0: // '\0'
            return new Vect(f, f1, f2 + diff(squareData[0], squareData[1]));

        case 1: // '\001'
            return new Vect(f + diff(squareData[1], squareData[3]), f1, f2 + 1.0F);

        case 2: // '\002'
            return new Vect(f + 1.0F, f1, f2 + diff(squareData[2], squareData[3]));

        case 3: // '\003'
            return new Vect(f + diff(squareData[0], squareData[2]), f1, f2);

        }
        return new Vect(0.0F, 0.0F, 0.0F);
    }

    void gridMesh()
    {
        int i = 0;
        for(int j = 0; j < 15; j++)
        {
            for(int k = 0; k < 16; k++)
            {
                for(int l = 0; l < 16; l++)
                {
                    if(l < 15 && k < 15)
                    {
                        int i1 = 0;
                        if((squareData[0] = Grid[i]) > thresh)
                            i1++;
                        if((squareData[1] = Grid[i + 1]) > thresh)
                            i1 += 2;
                        if((squareData[2] = Grid[i + 16]) > thresh)
                            i1 += 4;
                        if((squareData[3] = Grid[i + 17]) > thresh)
                            i1 += 8;
                        for(int j1 = 0; j1 < meshData[i1].length; j1 += 2)
                            line(getVectXY(meshData[i1][j1], l, k, j), getVectXY(meshData[i1][j1 + 1], l, k, j));

                        i1 = 0;
                        if((squareData[0] = Grid[i]) > thresh)
                            i1++;
                        if((squareData[1] = Grid[i + 16]) > thresh)
                            i1 += 2;
                        if((squareData[2] = Grid[i + 256]) > thresh)
                            i1 += 4;
                        if((squareData[3] = Grid[i + 272]) > thresh)
                            i1 += 8;
                        for(int k1 = 0; k1 < meshData[i1].length; k1 += 2)
                            line(getVectYZ(meshData[i1][k1], l, k, j), getVectYZ(meshData[i1][k1 + 1], l, k, j));

                        i1 = 0;
                        if((squareData[0] = Grid[i]) > thresh)
                            i1++;
                        if((squareData[1] = Grid[i + 256]) > thresh)
                            i1 += 2;
                        if((squareData[2] = Grid[i + 1]) > thresh)
                            i1 += 4;
                        if((squareData[3] = Grid[i + 257]) > thresh)
                            i1 += 8;
                        for(int l1 = 0; l1 < meshData[i1].length; l1 += 2)
                            line(getVectZX(meshData[i1][l1], l, k, j), getVectZX(meshData[i1][l1 + 1], l, k, j));

                    }
                    i++;
                }

            }

        }

    }

    void line(Vect vect, Vect vect1)
    {
        vect = pMatrix.apply(vect);
        vect1 = pMatrix.apply(vect1);
        vect.Project();
        vect1.Project();
        drawLineAA(vect.d[0] + (float)MIDX, vect.d[1] + (float)MIDY, vect1.d[0] + (float)MIDX, vect1.d[1] + (float)MIDY, 23);
    }

    public void paint(Graphics g)
    {
/*        int i = image_size.width * image_size.height;
        int j = image_size.width;
        for(int k = j; k < i - j; k++)
            aa_buffer_bytes[k] = (byte)((aa_buffer_bytes[k] & 0xff) * 7 + ((aa_buffer_bytes[k - 1] & 0xff) + (aa_buffer_bytes[k + 1] & 0xff) + (aa_buffer_bytes[k - j] & 0xff) + (aa_buffer_bytes[k + j] & 0xff)) * 2 >> 4);

        oldtime = newtime;
        newtime = System.currentTimeMillis();
        timepass = newtime - oldtime;
        timepass /= 1000F;
        time += timepass;
        Matrix matrix = new Matrix();
        Matrix matrix1 = new Matrix();
        matrix1.setTranslate(-8F, -8F, -8F);
        matrix.setRotateX(time);
        matrix1 = matrix1.mul(matrix);
        matrix.setRotateY(time * 1.375F);
        matrix1 = matrix1.mul(matrix);
        matrix.setScale(3F, 3F, 3F);
        pMatrix = matrix1.mul(matrix);
        for(int l = 0; l < Balls.length; l++)
            Balls[l] = new Vect((float)(Math.sin(BallFreq[l].d[0] * time + BallOfs[l].d[0]) * 3.5D), (float)(Math.sin(BallFreq[l].d[1] * time + BallOfs[l].d[1]) * 3.5D), (float)(Math.sin(BallFreq[l].d[2] * time + BallOfs[l].d[2]) * 3.5D));

        gridFill();
        gridMesh();
        aa_buffer_memory_source.newPixels(aa_buffer_bytes, color_model, 0, image_size.width);
*/      
        Rectangle r1=this.getBounds();
        
        drawLineAA(0,r1.height/2,r1.width,r1.height/2+6,256);
        g.drawImage(aa_buffer_image, 0, 0, this);
    }

    public void start()
    {
        newtime = oldtime = System.currentTimeMillis();
        //timer = new Thread(this);
       // timer.start();
    }

    public void stop()
    {
        timer = null;
    }

    public void run1()
    {
        for(Thread thread = Thread.currentThread(); timer == thread;)
        {
            try
            {
                Thread.currentThread();
                Thread.sleep(20L);
            }
            catch(InterruptedException interruptedexception) { }
            repaint();
        }

    }

    public void update(Graphics g)
    {
        paint(g);
    }

    public void setImageSize(int i, int j)
    {
        image_size = new Dimension(i, j);
    }

    public Dimension getImageSize()
    {
        return image_size;
    }

    public final void drawLineAA(double d, double d1, double d2, double d3, int i)
    {
        int j = (int)(65536D * d);
        int k = (int)(65536D * d2);
        int l = (int)(65536D * d1);
        int i1 = (int)(65536D * d3);
        int j1 = j;
        int k1 = k;
        int l1 = l;
        int i2 = i1;
        int j2 = k1 - j1;
        int k2 = i2 - l1;
        if(Math.abs(d2 - d) > Math.abs(d3 - d1))
        {
            if(k1 < j1)
            {
                j1 = k;
                k1 = j;
                l1 = i1;
            }
            int l2 = l1;
            int j3 = j2 >> 16 != 0 ? k2 / (j2 >> 16) : k2;
            for(int l3 = j1 >> 16; l3 < k1 + 32767 >> 16; l3++)
            {
                int j4 = l2 >> 16;
                drawPixelAA(l3, j4, image_size.width, l2, i);
                l2 += j3;
            }

        }
        else
        {
            if(i2 < l1)
            {
                j1 = k;
                l1 = i1;
                i2 = l;
            }
            int i3 = j1;
            int k3 = k2 >> 16 != 0 ? j2 / (k2 >> 16) : j2;
            for(int i4 = l1 >> 16; i4 < i2 + 32767 >> 16; i4++)
            {
                int k4 = i3 >> 16;
                drawPixelAA(k4, i4, 1, i3, i);
                i3 += k3;
            }

        }
    }

    private final void drawPixelAA(int i, int j, int k, int l, int i1)
    {
        if(j >= 0 && j < image_size.height - 1 && i >= 0 && i < image_size.width - 1)
        {
            int j1 = j * image_size.width + i;
            if(j1 < 0 || j1 >= aa_buffer_bytes.length)
                return;
            long l1 = aa_buffer_bytes[j1];
            long l2 = aa_buffer_bytes[j1 + k];
            if(l1 < 0L)
                l1 = 256L + l1;
            if(l2 < 0L)
                l2 = 256L + l2;
            long l3 = l & 0xffff;
            long l4 = 0x10000L - l3;
            long l5 = (long)i1 * l4 >> 16;
            long l6 = (long)i1 * l3 >> 16;
            l1 += l5;
            l2 += l6;
            aa_buffer_bytes[j1] = l1 >= 255L ? -1 : (byte)(int)l1;
            if(k != 0)
            {
                j1 += k;
                if(j1 < 0 || j1 >= aa_buffer_bytes.length)
                    return;
                aa_buffer_bytes[j1] = l2 >= 255L ? -1 : (byte)(int)l2;
            }
        }
    }
}

⌨️ 快捷键说明

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