Dropping Letters

<< Ant Hill | OtherProjectsTrailIndex | ChessRating >>

See a working version of the game here

Letter.java

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;


public class Letter {
	private char letter;
	private int x,y,drop, height, width, max;
	private Color color;
	private Font font;
	public Letter(int height, int width){
		//A=65 a=97
		letter = (char)rand(65,97+25);
		this.height = height;
		this.width = width;
		this.max = 50;
		this.x = rand(10, width - max);
	    this.y = rand(10, height - 100);//margin at top for messages
	    drop = rand(1,3);
	    color = new Color(rand(0,255),rand(0,255),rand(0,255) );
	    font = new Font("Helvtica", Font.BOLD, rand(max/2, max));
	}
	public int rand(int min, int max){
		return min+(int)((max-min+1)*Math.random());
	}
	public boolean draw(Graphics g){
		g.setColor(color);
		g.setFont(font);
		g.drawString(letter+"", x, y);
		y +=  drop;//drop
		return y < height;
	}
	public char getChar() {

		return letter;
	}
}

LetterTest.java

import javax.swing.*;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import javax.swing.Timer;


public class LetterTest extends JPanel implements ActionListener
{
    private ArrayList<Letter> list;
    private Timer fallTimer, spawnTimer;
    private static final int WIDTH = 800, HEIGHT = 600;
    private Font f=new Font("Helvetica", Font.BOLD|Font.ITALIC,20);

    public LetterTest(){
    	     list =  new ArrayList<Letter>();
    	     fallTimer = new Timer(30, this);
    	     spawnTimer = new Timer(45,this);
    	     for (int i = 0; i<30; i++){
    	    	      list.add(new Letter(HEIGHT, WIDTH));
    	     }
    	     fallTimer.start();
    	     spawnTimer.start();

    }
	public void paintComponent(Graphics g){
		g.setColor(Color.WHITE);
		g.fillRect(0, 0, WIDTH, HEIGHT);
		int i=0;
		while (i<list.size()){
			Letter x=list.get(i);
			if (x.draw(g)){
				i++;//go to next letter
			}else{
				list.remove(i);
				//miss++;
			}
		}
		g.setFont(f);
		g.setColor(Color.BLACK);
		g.drawString(list.size()+" letters are falling ", 20 , 20);	
	}
	public static void main(String[] args) {
        LetterTest app= new LetterTest();
        JFrame window = new JFrame("My Generic Application");
        window.setSize(WIDTH, HEIGHT);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.getContentPane().add(app);
        window.setVisible(true);

    }
	public void update(Graphics g)
    {
        paint(g); //get rid of flicker with this method
    }

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource()==fallTimer){

			repaint();
		} else {//must have been the spawnTimer generating the event
			list.add(new Letter(HEIGHT, WIDTH));
		}

	}

}