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 Seven Contents
Fun With Letters and Words
Rotating Lines and Polygons
Sorting and Shuffling

Letters - Words - Lines - Shuffle

Fun With Letters and Words - Falling Letters

To conclude this chapter, let's create some funny random words! Some of them will sound familiar, some really are. The algorithm is quite similar to the last applets, but this time we create two String constants, containing vowels and the consonants each. In our method randomWord(), which returns the random string when we call it, we create 3 random characters, each of the vowels and the consonants string. Then we combine these characters in a specific order, to get words without two consonants or vowels in succession. I use one of four algorithms, that are selected at random. To make it more fun to watch, I create only 1 word a second and print them one below the other and shifted to the right, until the window is full. Then the background is repainted and everything starts again.

//Sourcecode

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

public class Project39 extends Applet implements Runnable
{
    Thread runner;
    Image Buffer;
    Graphics gBuffer;
    String vowels="aeiouy";                       //6 characters
    String consonants="bcdfghklmnprstvwz";        //17 characters
    int x=10, y=30;
    Font font=new Font("Helvetica", Font.PLAIN,  30);
    Color bgColor=new Color(0,120,0);

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

    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)
        {
            try {runner.sleep(1000);}
            catch (Exception e) { }

            repaint();
        }
    }

    String randomWord()
    {
        int c1=(int)(Math.random()*17);
        int c2=(int)(Math.random()*17);
        int c3=(int)(Math.random()*17);

        String sc1=consonants.substring(c1, c1+1);
        String sc2=consonants.substring(c2, c2+1);
        String sc3=consonants.substring(c2, c2+1);

        int v1=(int)(Math.random()*6);
        int v2=(int)(Math.random()*6);
        int v3=(int)(Math.random()*6);

        String sv1=vowels.substring(v1, v1+1);
        String sv2=vowels.substring(v2, v2+1);
        String sv3=vowels.substring(v3, v3+1);

        //algo 0=cvcvc
        //algo 1=cvcvcv
        //algo 2=vcvc
        //algo 3=vcvcv

        String word;
        int algo=(int)(Math.random()*4);

        switch(algo)
        {
            case 0: word=sc1+sv1+sc2+sv2+sc3;
                break;

            case 1: word=sc1+sv1+sc2+sv2+sc3+sv3;
                break;

            case 2: word=sv1+sc1+sv2+sc2+sv3;
                break;

            default: word=sv1+sc1+sv2+sc2+sv3+sc3;
        }

        return word;
    }

    void drawStuff()
    {
        //repaint background if window is full
        if(y==30)
        {
            gBuffer.setColor(bgColor);
            gBuffer.fillRect(0,0,size().width,size().height);
        }

        gBuffer.setFont(font);

        //pause after the last word
        if(y<300)
        {
            String s=randomWord();
            //the drop shadow
            gBuffer.setColor(Color.black);
            gBuffer.drawString(s,x+2,y+2);

            gBuffer.setColor(Color.green);
            gBuffer.drawString(s,x,y);
        }

        y+=36;
        x+=25;

        if(y>350)
        {
            y=30;
            x=10;
        }
    }

    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