Main
/*
 * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *   - Neither the name of Oracle or the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
 
/*
 * KeyEventDemo
 */
 
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
 
import javax.swing.*;
 
/**
 * Main class of remote control for the predator. Draws the window and catches
 * the keyEvents to trigger actions.
 * 
 * @author falk
 * 
 */
public class Main extends JFrame implements KeyListener {
 
    private static final long serialVersionUID = 1L;
    private JToggleButton attack, forward, backward, left, right, stop;
    private ButtonGroup group;
 
    private static RemoteControl rc;
 
    public Main(String name) {
        super(name);
    }
 
    public static void main(String[] args) {
        // Connect to the robot
        rc = new RemoteControl();
        rc.connect("Predator", "00165310886D");
 
        /* Use an appropriate Look and Feel */
        try {
            // UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            UIManager
                    .setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
        } catch (UnsupportedLookAndFeelException ex) {
            ex.printStackTrace();
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        } catch (InstantiationException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
 
        // Schedule a job for event dispatch thread:
        // creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
 
    /**
     * Create the GUI and show it. For thread safety, this method should be
     * invoked from the event-dispatching thread.
     */
    private static void createAndShowGUI() {
        // Create and set up the window.
        Main frame = new Main("Control the Beast!");
        // Make sure the window cannot be accedently closed
        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
 
        // Set up the content pane.
        frame.addComponentsToPane();
        frame.pack();
 
        // Get the size of the screen
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
 
        // Determine the new location of the window
        int w = frame.getSize().width;
        int h = frame.getSize().height;
        int x = (dim.width - w) / 2;
        int y = (dim.height - h) / 2;
 
        // Move and display the window
        frame.setLocation(x, y);
        frame.setVisible(true);
 
        // Add the KeyListener        
        frame.requestFocusInWindow();
        frame.addKeyListener(frame);
    }
 
    private void addComponentsToPane() {
 
        forward = new JToggleButton("Forward");
        backward = new JToggleButton("Backward");
        right = new JToggleButton("Right");
        left = new JToggleButton("Left");
        stop = new JToggleButton("Stop");
        attack = new JToggleButton("ATTACK");
 
        forward.addKeyListener(this);
        backward.addKeyListener(this);
        left.addKeyListener(this);
        right.addKeyListener(this);
        attack.addKeyListener(this);
 
        /*
         * Create a button group. In this group, activating one button will
         * deactivate the others.
         */
        group = new ButtonGroup();
        group.add(forward);
        group.add(backward);
        group.add(left);
        group.add(right);
        group.add(stop);
 
        // Stop should be active on startup
        stop.doClick();
 
        // Layout
        Container c = getContentPane();
        c.setLayout(new GridLayout(3, 4, 1, 1));
 
        c.add(new JLabel("<html><b>Arrow Keys</b> - Movement<br>" +
                "<b>Shift</b> - Stop" +
                "</html>"));
        c.add(new JLabel()); // dummy to leave the cell blank
        c.add(new JLabel()); // dummy to leave the cell blank
        c.add(new JLabel()); // dummy to leave the cell blank
        c.add(new JLabel("<html><b>Space</b> - Toggle Attack<br>" +
                "<b>ESC</b> - Quit" +
                "</html>"));
        c.add(forward);
        c.add(new JLabel()); // dummy to leave the cell blank
        c.add(attack);
        c.add(left);
        c.add(backward);
        c.add(right);
        c.add(stop);
    }
 
    /** Handle the key released event from the text field. */
    public void keyReleased(KeyEvent e) {
    }
 
    /** Handle the key typed event from the text field. */
    public void keyTyped(KeyEvent e) {
    }
 
    /** Handle the key pressed event from the text field. */
    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();
        switch (keyCode) {
        case KeyEvent.VK_UP:
            forward.doClick();
            rc.forward();
            break;
        case KeyEvent.VK_DOWN:
            backward.doClick();
            rc.backward();
            break;
        case KeyEvent.VK_LEFT:
            left.doClick();
            rc.left();
            break;
        case KeyEvent.VK_RIGHT:
            right.doClick();
            rc.right();
            break;
        case KeyEvent.VK_SHIFT:
            stop.doClick();
            rc.stop();
            break;
        case KeyEvent.VK_SPACE:
            attack.doClick();
            rc.attack();
            break;
        case KeyEvent.VK_S:
            rc.playSound();
            break;
        case KeyEvent.VK_L:
            rc.laugh();
            break;
        case KeyEvent.VK_P:
            rc.psycho();
            break;
        case KeyEvent.VK_ESCAPE:
            rc.quit();
            this.dispose();
            System.exit(0);
        case KeyEvent.VK_Q:
            rc.quit();
            this.dispose();
            System.exit(0);
        }
    }
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License