Beginnings

//Mother Nature


This is one of my first publicly posted applications. I wrote this applet nearly 17 years ago at the age of 12. It was not meant to be anything fancy or truly show my abilities. Instead this was simply written for someone who asked me if I could create a java applet that would create the effect of rain and snow fall on a static background image. For whatever reason I chose to make my name appear as one of the applet parameters forever associating my name with this applet in Google and virtually every other search engine out there today. Good idea or bad idea, you be the judge, but in either event I felt like including this in my current site since when googling my name there is a good chance you will see random pages appear simply because my name is included in them. I also included the code which made this possible just to show how far I have come with my coding abilities.

If you would like to include this applet in your site I have given a download link which will give you everything you need to incorporate this applet and yes my name is still part of the parameter list just to keep the search engines on their toes.


Download

//Code


import java.applet.Applet;
import java.awt.*;

public class Nature extends Applet
    implements Runnable
{

    private void fog(Graphics g)
    {
        g.drawImage(OffScreenImage, 0, 0, size().width, size().height, this);
    }

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

    private void rain(Graphics g)
    {
        int Stat = 0;
        OffScreenGraphics.setColor(RainColor);
        OffScreenGraphics.drawImage(BackImg, 0, 0, size().width, size().height, this);
        Stat = (int)(Math.random() * 100D);

        if(RndDirection)
            if(Stat > 0 && Stat < 5)
            {
                LRdir = 1;
                WndSpeed = (int)(Math.random() * -5D);
            }
            else if(Stat > 55 && Stat < 60)
            {
                LRdir = 3;
                WndSpeed = (int)(Math.random() * 5D);
            }
            else if(Stat > 98)
            {
                LRdir = 2;
                WndSpeed = 0;
            }

        for(int I = 0; I < MaxCount; I++)
        {
            Ypos[I] += Speed[I];

            if(LRdir == 1)
                Xpos[I] += WndSpeed;
            else if(LRdir == 3)
                Xpos[I] += WndSpeed;

            if(Xpos[I] < 0)
                Xpos[I] = size().width - 1;

            if(Xpos[I] > size().width)
                Xpos[I] = 1;

            if(Ypos[I] > size().height)
            {
                Ypos[I] = 0;
                Xpos[I] = (int)(Math.random() * (double)size().width);
            }

            OffScreenGraphics.drawLine(Xpos[I], Ypos[I], Xpos[I] + WndSpeed / 2, Ypos[I] + 2);
            OffScreenGraphics.drawLine(Xpos[I], Ypos[I] + 1, Xpos[I] + WndSpeed / 2, Ypos[I] + 3);
        }

        g.drawImage(OffScreenImage, 0, 0, size().width, size().height, this);
    }

    public Nature()
    {
        BackImg = null;
        RainColor = new Color(145, 164, 164);
        slptime = 75;
        Season = 1;
        LRdir = 2;
        MaxCount = 100;
        Radius = 2;
        MaxSpeed = 8;
        WndSpeed = 0;
        Xpos = new int[1024];
        Ypos = new int[1024];
        Height = new int[1024];
        Speed = new int[1024];
        RndDirection = true;
        Accumulate = false;
        DevInfo = false;
    }

    public void getAppletParams()
    {
        Image DummyImage = createImage(size().width, size().height);
        Graphics DummyGraphics = DummyImage.getGraphics();
        Graphics ThisG = getGraphics();
        MediaTracker Tracker = new MediaTracker(this);
        FontMetrics MyMetrics = DummyGraphics.getFontMetrics();

         if(getParameter("DeveloperInfo") != null)
         {
           if(getParameter("DeveloperInfo").compareTo("Michael Chancey Jr.") == 0)
                DevInfo = true;
           else
                DevInfo = false;
         }
         else
         {
           DevInfo = false;
         }

        if(getParameter("speed") != null)
            slptime = Integer.parseInt(getParameter("speed"));

        if(getParameter("season") != null)
            if(getParameter("season").compareTo("winter") == 0)
                Season = 1;
            else
            if(getParameter("season").compareTo("summer") == 0)
            {
                Season = 2;
                if(getParameter("raincolor") != null)
                    RainColor = stringToColor(getParameter("raincolor"));
            }

        if(getParameter("accumulate") != null && getParameter("accumulate").compareTo("yes") == 0)
            Accumulate = true;

        if(getParameter("fallcount") != null)
            MaxCount = Integer.parseInt(getParameter("fallcount"));

        if(getParameter("randomdir") != null)
            if(getParameter("randomdir").compareTo("yes") == 0)
                RndDirection = true;
            else
                RndDirection = false;

        if(getParameter("graphic") != null)
        {
            BackImg = getImage(getCodeBase(), getParameter("graphic"));
            Tracker.addImage(BackImg, 0);
            try
            {
                showStatus("Loading Graphic: " + getParameter("graphic"));
                Tracker.waitForAll();
            }
            catch(InterruptedException e)
            {
                showStatus("Error Code: " + e.getMessage());
            }
        }
        else
        {
            DummyGraphics.drawString("Graphic Needed", 0, 20);
            BackImg = DummyImage;
        }

        if(!DevInfo)
        {
            int Top = size().height / 2 - 40;
            DummyGraphics.drawString("This Applet Was", size().width / 2 - MyMetrics.stringWidth("This Appplet Was") / 2, Top + 20);
            DummyGraphics.drawString("Created By:", size().width / 2 - MyMetrics.stringWidth("Created By:") / 2, Top + 40);
            DummyGraphics.drawString("Michael Chancey Jr.", size().width / 2 - MyMetrics.stringWidth("Michael Chancey Jr.") / 2, Top + 60);
            DummyGraphics.drawString("mchancey@bellatlantic.net", size().width / 2 - MyMetrics.stringWidth("mchancey@bellatlantic.net") / 2, Top + 80);
            BackImg = DummyImage;
        }
    }

    public String[][] getParameterInfo()
    {
        String info[][] = new String[0][];
        return info;
    }

    void initForm()
    {
        setBackground(Color.lightGray);
        setForeground(Color.black);
        setLayout(new BorderLayout());
    }

    private void snow(Graphics g)
    {
        int Stat = 0;
        OffScreenGraphics.setColor(Color.white);
        OffScreenGraphics.drawImage(BackImg, 0, 0, size().width, size().height, this);
        Stat = (int)(Math.random() * 100D);

        if(RndDirection)
            if(Stat > 0 && Stat < 5)
            {
                LRdir = 1;
                WndSpeed = (int)(Math.random() * 5D);
            } else
            if(Stat > 55 && Stat < 60)
            {
                LRdir = 3;
                WndSpeed = (int)(Math.random() * 5D);
            } else
            if(Stat > 98)
                LRdir = 2;

        for(int I = 0; I < MaxCount; I++)
        {
            Ypos[I] += Speed[I];

            if(LRdir == 1)
                Xpos[I] -= WndSpeed;
            else if(LRdir == 3)
                Xpos[I] += WndSpeed;

            if(Xpos[I] < 0)
                Xpos[I] = size().width - 1;

            if(Xpos[I] > size().width)
                Xpos[I] = 1;

            if(Ypos[I] >= Height[Xpos[I]])
            {
                Ypos[I] = 0;
                Xpos[I] = (int)(Math.random() * (double)size().width);

                if(Accumulate && Math.random() * 100D > 50D)
                {
                    Height[Xpos[I]] -= Radius - 1;
                    Height[Xpos[I] - 1] -= Radius - 2;
                    Height[Xpos[I] + 1] -= Radius - 2;
                }
            }

            OffScreenGraphics.fillOval(Xpos[I], Ypos[I], Radius, Radius);
        }

        for(int I = 0; I < size().width; I++)
            OffScreenGraphics.drawLine(I, Height[I], I, size().height);

        g.drawImage(OffScreenImage, 0, 0, size().width, size().height, this);
    }

    public void update(Graphics g)
    {
        switch(Season)
        {
        case 1:
            snow(g);
            break;

        case 2:
            rain(g);
            break;

        case 3:
            fog(g);
            break;
        }
    }

    public void start()
    {
    }

    public void run()
    {
        do
        {
            repaint();
            try
            {
                Thread.sleep(slptime);
            }
            catch(InterruptedException e) { }
        } while(true);
    }

    public void init()
    {
        initForm();
        getAppletParams();
        OffScreenImage = createImage(size().width, size().height);
        OffScreenGraphics = OffScreenImage.getGraphics();
        Animator = new Thread(this);
        Animator.start();

        for(int I = 0; I < MaxCount; I++)
        {
            Speed[I] = (int)(Math.random() * (double)MaxSpeed) + 3;
            Xpos[I] = (int)(Math.random() * (double)size().width);
            Ypos[I] = (int)(Math.random() * (double)size().height);
        }

        int height = size().height;

        for(int I = 0; I < 1024; I++)
            Height[I] = height;
    }

    private Color stringToColor(String paramValue)
    {
        int red = Integer.decode("0x" + paramValue.substring(0, 2)).intValue();
        int green = Integer.decode("0x" + paramValue.substring(2, 4)).intValue();
        int blue = Integer.decode("0x" + paramValue.substring(4, 6)).intValue();

        return new Color(red, green, blue);
    }

    Thread Animator;
    Image BackImg;
    Image OffScreenImage;
    Graphics OffScreenGraphics;
    Color RainColor;
    int slptime;
    int Season;
    int LRdir;
    int MaxCount;
    int Radius;
    int MaxSpeed;
    int WndSpeed;
    int Xpos[];
    int Ypos[];
    int Height[];
    int Speed[];
    boolean RndDirection;
    boolean Accumulate;
    boolean DevInfo;
}
  • RSS
  • Twitter
  • Facebook
  • LinkedIn
  • DeviantArt