import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import josx.rcxcomm.*;

/**
 * A very simple remote control client for use with the RCX Remote Control Server.
 * Allows the robot to be driven forward, backward, turn left and turn right.
 * As well as clicking the button, the keys WASD can be used to control the robot.
 * Remember to change the port that the IR tower is actually connected to below.
 *
 * @author David (www.uk-dave.com)
 * @version 1.0, 22/03/2004
 */
public class SimplePCClient extends JFrame implements ActionListener, KeyListener, WindowListener
{
	////////////////////////////////////////////////////
	// CONSTANTS AND CLASS VARIABLES
	////////////////////////////////////////////////////

	public static final string RCX_PORT = "COM2";

	public static final byte STOP=0x00, SET_POWER=0x05, PLAY_SYSTEM_SOUND=0x10;
	public static final byte FORWARD=0x01, FORWARD_THEN_STOP=0x11;
	public static final byte BACKWARD=0x02, BACKWARD_THEN_STOP=0x12;
	public static final byte LEFT=0x03, LEFT_THEN_STOP=0x13;
	public static final byte RIGHT=0x04, RIGHT_THEN_STOP=0x14;

	private static RCXBean rcx;
	private byte currentCommand=STOP;

	private JButton forwardBtn, backwardBtn, stopBtn, leftBtn, rightBtn;






	////////////////////////////////////////////////////
	// CONSTRUCTOR
	////////////////////////////////////////////////////

	/**
	 *
	 */
	public SimplePCClient()
	{
		super("RCX Remote Control");
		try { javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName()); }
		catch (Exception e) {}
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		addWindowListener(this);
		addKeyListener(this);
		Container c = getContentPane();

		forwardBtn = new JButton(new ImageIcon("forward.gif"));
		forwardBtn.addActionListener(this);
		forwardBtn.addKeyListener(this);
		c.add(forwardBtn, BorderLayout.NORTH);
		backwardBtn = new JButton(new ImageIcon("backward.gif"));
		backwardBtn.addActionListener(this);
		backwardBtn.addKeyListener(this);
		c.add(backwardBtn, BorderLayout.SOUTH);
		stopBtn = new JButton(new ImageIcon("stop.gif"));
		stopBtn.addActionListener(this);
		stopBtn.addKeyListener(this);
		c.add(stopBtn, BorderLayout.CENTER);
		leftBtn = new JButton(new ImageIcon("left.gif"));
		leftBtn.addActionListener(this);
		leftBtn.addKeyListener(this);
		c.add(leftBtn, BorderLayout.WEST);
		rightBtn = new JButton(new ImageIcon("right.gif"));
		rightBtn.addActionListener(this);
		rightBtn.addKeyListener(this);
		c.add(rightBtn, BorderLayout.EAST);

		rcx = new RCXBean();
		try { rcx.setComPort(RCX_PORT); }
		catch (IOException ioe) { JOptionPane.showMessageDialog(this, ioe.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); }

		pack();
		Dimension screen = getToolkit().getScreenSize();
		setLocation(screen.width/2 - getWidth()/2,  screen.height/2 - getHeight()/2);
		show();
	}






	////////////////////////////////////////////////////
	// METHODS
	////////////////////////////////////////////////////

	/**
	 *
	 * @param command
	 */
	public void sendCommand(byte command)
	{
		try
		{
			if (command!=currentCommand)
			{
				rcx.send(command);
				currentCommand = command;
			}
		}
		catch (IOException ioe)
		{
			JOptionPane.showMessageDialog(this, ioe.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
		}
	}




	////////////////////////////////////////////////////
	// ACTION LISTENERS
	////////////////////////////////////////////////////

	/**
	 *
	 * @param e
	 */
	public void actionPerformed(ActionEvent e)
	{
		if (e.getSource().equals(forwardBtn))
		{
			sendCommand(FORWARD);
		}
		else if (e.getSource().equals(backwardBtn))
		{
			sendCommand(BACKWARD);
		}
		else if (e.getSource().equals(stopBtn))
		{
			sendCommand(STOP);
		}
		else if (e.getSource().equals(leftBtn))
		{
			sendCommand(LEFT);
		}
		else if (e.getSource().equals(rightBtn))
		{
			sendCommand(RIGHT);
		}
	}


	/**
	 *
	 * @param e
	 */
	public void keyPressed(KeyEvent e)
	{
		if (e.getKeyChar()=='w' || e.getKeyChar()=='W')
		{
			sendCommand(FORWARD);
		}
		else if (e.getKeyChar()=='a' || e.getKeyChar()=='A')
		{
			sendCommand(LEFT);
		}
		else if (e.getKeyChar()=='s' || e.getKeyChar()=='S')
		{
			sendCommand(BACKWARD);
		}
		else if (e.getKeyChar()=='d' || e.getKeyChar()=='D')
		{
			sendCommand(RIGHT);
		}
	}

	/**
	 *
	 * @param e
	 */
	public void keyReleased(KeyEvent e)
	{
		// Stop the robot
		sendCommand(STOP);
	}

	/**
	 *
	 * @param e
	 */
	public void keyTyped(KeyEvent e) {}



	public void windowClosing(WindowEvent windowevent) { rcx.close(); System.exit(0); }
	public void windowActivated(WindowEvent e) {}
	public void windowClosed(WindowEvent e) {}
	public void windowDeactivated(WindowEvent e) {}
	public void windowDeiconified(WindowEvent e) {}
	public void windowIconified(WindowEvent e) {}
	public void windowOpened(WindowEvent e) {}






	////////////////////////////////////////////////////
	// MAIN
	////////////////////////////////////////////////////

	/**
	 *
	 * @param args
	 */
	public static void main(String[] args)
	{
		new SimplePCClient();
	}
}