package com.hatherly.babykeyboardfun;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.GraphicsEnvironment;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.util.Stack;
import javax.swing.ImageIcon;
/**
* This is the main class for the "Baby Keyboard Fun" game. It is a swing
* application that runs full-screen and listens for keyboard key presses. When
* it detects one, an image relevant to the key is displayed, and a sound
* clip is played. The basic code for a full-screen swing app was taken
* from this example:
* http://schmidt.devlib.org/java/gui-countdown.html
*
* @author Adam Hatherly
*/
public class BabyGame implements Runnable {
private JFrame frame;
private JLabel label;
private static Character letter = null;
SoundFactory soundFactory = new SoundFactory();
ImageFactory imageFactory = new ImageFactory();
/**
* Constructor - Creates a new instance of babygame
*/
public BabyGame(JFrame frame, JLabel label) {
this.frame = frame;
this.label = label;
}
/**
* @param args the command line arguments - not used
*/
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.addKeyListener(new KeyListener() {
public void keyPressed(KeyEvent event) {}
public void keyReleased(KeyEvent event) {
if (event.getKeyChar() == KeyEvent.VK_ESCAPE) {
System.exit(0);
} else {
letter = (new Character(event.getKeyChar()));
}
}
public void keyTyped(KeyEvent event) {}
}
);
frame.setUndecorated(true);
JLabel label = new JLabel("");
label.setBackground(Color.BLACK);
label.setForeground(Color.WHITE);
label.setOpaque(true);
label.setHorizontalAlignment(SwingConstants.CENTER);
frame.getContentPane().add(label);
GraphicsEnvironment.getLocalGraphicsEnvironment().
getDefaultScreenDevice().setFullScreenWindow(frame);
new BabyGame(frame, label).run();
}
public void run() {
do
{
if (letter != null) {
ImageIcon icon = imageFactory.getImage(letter.charValue());
if (icon != null) {
label.setIcon(icon);
soundFactory.makeSound();
}
letter = null;
}
try {
Thread.sleep(300);
} catch (InterruptedException ie) {
ie.printStackTrace(System.err);
}
}
while (true);
}
}
syntax highlighted by Code2HTML, v. 0.9.1