advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement


Tutorials : Java by Example : Section 7 :
Section Six Contents
Fun With Letters and Words
Rotating Lines and Polygons
Sorting and Shuffling

Letters - Words - Lines - Shuffle

Fun With Letters and Words - Falling Letters

Now let's have some real fun with falling letters! We create a class FallingCharacter that represents the falling letters. We create an array of objects of that class (here: 100, represented by the variable MAX, can be changed easily). Each object takes care for random values for the initial position, size, color and speed! The speed is represented by the double precision floating point variable yinc, which is added to the y position in the member method fall(). Only floating point variables guarantee for smooth movement in many different speed values. They are casted to int values only in the paint method, in the end. The constructor of the FallingCharacter class receives the width and height of the applet as parameters, to make it possible to easily change the applet size without changing the code. After reaching the bottom of the window, the letters reappear at the top again.

//Sourcecode

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

class FallingCharacter
{
    int fontSize, h;
    double x, y, yinc;
    String s, alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    Color color;
    Graphics g;
    Font font;

    public FallingCharacter(int width, int height)
    {
        h=height;

        //random font size 10..50
        fontSize=(int)(Math.random()*40)+10;
        font=new Font("TimesRoman", Font.PLAIN,  fontSize);

        //random font color
        int red=(int)(Math.random()*255);
        int green=(int)(Math.random()*255);
        int blue=(int)(Math.random()*255);
        color=new Color(red, green, blue);

        //random initial coordinates within the applet bounderies
        x=(Math.random()*width);
        y=(Math.random()*height);

        //random falling speed
        yinc=(Math.random()*2.5)+1.0;

        //random substring from the alphabet string (one out of 26 characters)
        int character=(int)(Math.random()*26);
        s=alphabet.substring(character, character+1);
    }

    public void fall()
    {
        if(y<h+fontSize)
            y+=yinc;
        else
            y=-fontSize;
    }

    public void paint(Graphics gr)
    {
        g=gr;

        g.setColor(color);
        g.setFont(font);
        g.drawString(s,(int)x,(int)y);
    }
}

public class Project38 extends Applet implements Runnable
{
    Thread runner;
    Image Buffer;
    Graphics gBuffer;
    int i;
    static final int MAX=100;
    FallingCharacter fc[];

    public void init()
    {
        //create graphics buffer, the size of the applet
        Buffer=createImage(size().width,size().height);
        gBuffer=Buffer.getGraphics();

        fc = new FallingCharacter[MAX];

        for(i=0;i<MAX;i++)
            fc[i] = new FallingCharacter(size().width-30,size().height);
    }

    public void start()
    {
        if (runner == null)
        {
            runner = new Thread (this);
            runner.start();
        }
    }

    public void stop()
    {
        if (runner != null)
        {
            runner.stop();
            runner = null;
        }
    }

    public void run()
    {
        while(true)
        {
            Thread.currentThread().setPriority(Thread.MAX_PRIORITY);

            try {runner.sleep(15);}
            catch (Exception e) { }

            repaint();
        }
    }

    public void drawStuff()
    {
        gBuffer.setColor(Color.black);
        gBuffer.fillRect(0,0,size().width,size().height);

        for(i=0;i<MAX;i++)
            fc[i].fall();

        for(i=0;i<MAX;i++)
            fc[i].paint(gBuffer);
    }

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

    public void paint(Graphics g)
    {
        drawStuff();
        g.drawImage (Buffer,0,0, this);
    }
}

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.

 Microsoft Visual Studio 2010 Showcase
 Avaya Developer Showcase
 MSDN Spotlight
 PHP for Windows Showcase
XML error: undefined entity at line 39
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

Windows 7: From Beta to Final Code in One Year
Google Shows Off Chrome OS, Releases Source
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?
Fedora 12 Takes Aim at Linux Networking
Top Supercomputer Nearly Doubles in Speed
Fedora 12 Linux Tackles Virtualization
Apple Gives iPhone Developers App Status Tracker
Novell Sets OpenSUSE 11.2 Free

Creating Custom Export Filters for StarOffice with XSLT
WPF Wonders: Using DataTemplates
Crystal Reports Family Offers Options for Developers
Avaya Aura Session Manager video
Avaya Aura Overview video
Exploring HTML 5's Audio/Video Multimedia Support
Overriding Virtual Functions? Use C++0x Attributes to Avoid Bugs.
Understanding the Cloud Computing Security Vulnerabilities
Cisco and IBM Target a Greener World
Upgrade to Visual Studio 2010 with the Ultimate Offer

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs