Friday, July 4, 2008
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
//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) s="randomWord();" color="red">//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);
}
}
Subscribe to:
Post Comments (Atom)
SUBSCRIBE VIA eMAIL
Recent Posts
Archives
- December 2008 (5)
- November 2008 (15)
- October 2008 (17)
- September 2008 (9)
- August 2008 (12)
- July 2008 (19)
- June 2008 (22)
- May 2008 (17)
- April 2008 (2)
Categories
- java.sql (19)
- Examples (18)
- INTRODUCTION (9)
- JAVA APPLET (9)
- java.awt (8)
- java.net (8)
- java.beans (7)
- JAVA String Utility (6)
- Arrays (4)
- java.math (3)
- java.util.regex (3)
- Sort (2)
- Swing (2)
- java.security (2)
- java.util.zip (2)
- Catching Exceptions (1)
- Classes and Objects (1)
- Core Java Programs-part 1 (1)
- Core Java Programs-part 2 (1)
- Core Java Programs-part3 (1)
- Criticism of Java programming language (1)
- Falling Letters (1)
- File I/O and Streams (1)
- Fun With Letters and Words (1)
- Get current working directory (1)
- How do I convert String to Date object? (1)
- How do I convert string into InputStream? (1)
- How do i calculate directory size? (1)
- How to make executable jar files in JDK1.3.1? (1)
- Interfaces (1)
- JAVA Date Utility (1)
- Java Arithmetic Operators (1)
- Java Assignment Operators (1)
- Java Boolean Operators (1)
- Java Command Line Arguments (1)
- Java Comments (1)
- Java Conditional Operators (1)
- Java Data and Variables (1)
- Java Hello World Program (1)
- Java If-Else Statement (1)
- Java Increment and Decrement Operators (1)
- Java Loops (while (1)
- Java Relational Operators (1)
- Java Variables and Arithmetic Expressions (1)
- Java Virtual Machine (1)
- Know the current position of cursor (1)
- Letters (1)
- Methods (Includes Recursive Methods) (1)
- Rotating Lines (1)
- Send an email with attachment (1)
- Use for..each in Java (1)
- What is Autoboxing? (1)
- Write text file (1)
- connection to database (1)
- do-while and for loops) (1)


0 comments:
Post a Comment