Pick Me

<< Random Number Generator | OtherProjectsTrailIndex | ClassQuizScorer >>

See the archived version in action here!

See the current JavaWide version in action here!

RandomNameTile.java
import  java.awt.*;
/**
 * A six sided Die.
 * 
 * @author Chris Thiel, OFMCap
 * @version 29 March 2011
 */
public class RandomNameTile
{
    // instance variables - 
    private int size;
    private int x; //top left corner
    private int y;
    private int choice;
    private Graphics g;
    private Color color;
    private String[] names={ "Alexander", "Alexi", "Cameron", "Ethan", "Ian", 
    		"Joseph", "Matthew", "Russell"};
    /**
     * Constructor for objects of class Die
     */
    public RandomNameTile(Graphics g,  int x, int y, int size)
    {
        this.g=g;
        this.size=size;
        this.x=x;
        this.y=y;
        this.choice=(int)(Math.random()*names.length);
        this.color=Color.WHITE;
    }

    public void setColor(Color c)
    {
        this.color=c;
    }

    public void draw()
    {
        g.setColor(color);
        g.fillRect(x-size/2,y,3*size,size);
        g.setColor(Color.BLACK);
        g.drawRect(x-size/2,y,3*size,size);

        if (color.equals(Color.WHITE))
        		g.setColor(Color.BLUE);
        g.setFont(new Font("Helvetica", Font.BOLD, size/2));
        g.drawString(names[choice], size/2, size);
    }
}

PickMe.java
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/**
 * Class Roll - click to roll a single 6-sided die
 * 
 * @author Chris Thiel, OFMCap
 * @version 29 March 2011
 */
public class PickMe extends Applet implements MouseListener
{
    // instance variables - replace the example below with your own
    private RandomNameTile nameTile;
    private int count;

    public void init()
    {
        addMouseListener(this);
        count=0;
    }




    /**
     * Paint method for applet.
     * 
     * @param  g   the Graphics object for this applet
     */
    public void paint(Graphics g)
    {
        // simple text displayed on applet
        g.setColor(new Color(150, 75, 25));
        g.fillRect(0, 0, 500, 250);
        g.setColor(Color.RED);
        g.drawString("Click to", 20, 20);
        g.setColor(Color.YELLOW);
        g.drawString("Randomly Pick Someone", 20, 40);
        if (count >0)
        {
            count--;
            nameTile = new RandomNameTile (g, 100,50, 150 );
            nameTile.setColor(Color.RED);
            nameTile.draw();
            repaint();
        }else{
            nameTile = new RandomNameTile (g, 100,50,150);
            nameTile.draw();
        }
    }
    // 5 MouseListener methods that NEED to be implemented since our Applet implements the MouseListener interface
    public void mouseReleased(MouseEvent e)
    {
        count = (int)(Math.random()*300.0)+175;
        repaint();
    }

    public void mouseEntered(MouseEvent e){}
    public void mouseExited(MouseEvent e){}
    public void mousePressed(MouseEvent e){}
    public void mouseClicked(MouseEvent e){}
}