Thursday, July 3, 2008
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
//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 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 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 fc[i].fall();
for(i=0;i fc[i].paint(gBuffer);
}
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