« Return to Thread: Keyboard Configuration Tab Big Problem

Keyboard Configuration Tab Big Problem

by asherbaig :: Rate this Message:

Reply to Author | View in Thread

Hi Developers,

As in earlier days you people help me a lots for that i am really
thankful to you by heart.Now i have biggest problem facing hopefully
you will help me in this matter.

The problem is About configuration  of Keyboard for My Robot for
single player.

I will explain in step by step

At first in PreferencesDialog.java
i have created object

private PreferencesKeyConfigurationOptionsTab KeyConfigurationOptionsTab;



Under wizard i added tab



tabbedPane.insertTab("Key Configuration", null,
getKeyConfigurationnOptionsTab(), null, 5);
                        tabbedPane.setMnemonicAt(5, KeyEvent.VK_K);
                        tabbedPane.setDisplayedMnemonicIndexAt(5,0);



Now i created seperate calls for KeyConfigurationOptionsTab,java
Also i created seperated project for my Robot by the name Uniteam and
added pakcage single player and class Sprobot.java ( which is my robot).

Robots work fine Here is the code detail as follows:

package singleplayer;

import robocode.AdvancedRobot;

import java.awt.*;
import java.awt.event.KeyEvent;
import static java.awt.event.KeyEvent.*;

public class Sprobot extends AdvancedRobot {


        // Move direction: 1 = move forward, 0 = stand still, -1 = move backward
        int moveDirection;

        // Turn direction: 1 = turn right, 0 = no turning, -1 = turn left
        int turnDirection;

        // Amount of pixels/units to move
        double moveAmount;

        // The coordinate of the aim (x,y)
        int aimX, aimY;

        // Fire power, where 0 = don't fire
        int firePower;

        //Returns the direction that the robot's radar is facing, in degrees.
        //The value returned will be between 0 and 360 (is excluded).
        //Note that the heading in Robocode is like a compass, where 0 means
North,
        //90 means East, 180 means South, and 270 means West.
        double getRadarHeading;
       
        // Called when the robot must run
        public void run() {

                // Sets the colors of the robot
                // body = black, gun = white, radar = red
                setColors(Color.BLACK, Color.BLACK, Color.YELLOW);

                // Loop forever
                for (;;) {
                        // Sets the robot to move forward, backward or stop moving depending
                        // on the move direction and amount of pixels to move
                        setAhead(moveAmount * moveDirection);

                        // Decrement the amount of pixels to move until we reach 0 pixels
                        // This way the robot will automatically stop if the mouse wheel
                        // has stopped it's rotation
                        moveAmount = Math.max(0, moveAmount - 1);

                        // Sets the robot to turn right or turn left (at maximum speed) or
                        // stop turning depending on the turn direction
                        setTurnRight(45 * turnDirection); // degrees

                        // Turns the gun toward the current aim coordinate (x,y) controlled by
                        // the current mouse coordinate
        // double angle = normalAbsoluteAngle(Math.atan2(aimX - getX(), aimY
- getY()));

                       
                       
                // setTurnGunRightRadians(normalRelativeAngle(angle -
getGunHeadingRadians()));

                        // Fire the gun with the specified fire power, unless the fire
power = 0
                        if (firePower > 0) {
                                setFire(firePower);
                        }

                        // Execute all pending set-statements
                        execute();

                        // Next turn is processed in this loop..
                }
        }

        // Called when a key has been pressed
        public void onKeyPressed(KeyEvent e) {
                switch (e.getKeyCode()) {
                case VK_W:
                        // Arrow up key: move direction = forward (infinitely)
                        moveDirection = 1;
                        moveAmount = Double.POSITIVE_INFINITY;
                        break;

                case VK_S:
                        // Arrow down key: move direction = backward (infinitely)
                        moveDirection = -1;
                        moveAmount = Double.POSITIVE_INFINITY;
                        break;

                case VK_D:
                        // Arrow right key: turn direction = right
                        turnDirection = 1;
                        break;

               
                case VK_A:
                        // Arrow left key: turn direction = left
                        turnDirection = -1;
                        break;
                       
                        // Button 3 : return fire
                        case VK_G:
                                firePower = 3;
                                setBulletColor(Color.RED);
                                break;
                               
                                // Button 2 : return fire
                        case VK_H:
                                firePower = 2;
                                setBulletColor(Color.YELLOW);
                                break;
                               
                                // Button 1 : return fire
                        case VK_J:
                                firePower = 1;
                                setBulletColor(Color.WHITE);
                                break;
                               
                                // Move Gun : In Direction 180 to fire using buttons  
                        case VK_U:
                                char c = e.getKeyChar();
                                if (c == 'u') {
                                setTurnGunLeft(180);
                                } else if (c == 'i') {
                                setTurnGunRight(180);
                                }

                       
                        }
               
        }
               
        // Called when a key has been released (after being pressed)
        public void onKeyReleased(KeyEvent e) {
                switch (e.getKeyCode()) {
                case VK_W:
                case VK_S:
                        // Arrow up and down keys: move direction = stand still
                        moveDirection = 0;
                        break;

                case VK_D:
                case VK_A:
                        // Arrow right and left keys: turn direction = stop turning
                        turnDirection = 0;
                        break;
                       
                // Button 3 : Stop firing
                case VK_G:
                firePower = 0;
                break;
                       
                // Button 2 : Stop firing
                case VK_H:
                        firePower = 0;
                        break;
               
                // Button 1 : Stop firing
                case VK_J:
                        firePower = 0;
                        break;
                       
       
                }
        }



}


And Here is the code for PreferencesKeyConfigurationOptionsTab

package robocode.dialog;



import robocode.manager.RobocodeManager;
import robocode.manager.RobocodeProperties;
//import robocode.manager.RobocodeProperties;


import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;



public class PreferencesKeyConfigurationOptionsTab  extends WizardPanel {

        private RobocodeManager manager;

        private JPanel specificSettingsPanel;

        private JPanel fireSettingsPanel;
        private JPanel RadarSettingsPanel;
       
        private JComboBox optionsKeyboardComboBox;
        private JComboBox optionsKeyboard2ComboBox;
        private JComboBox optionsKeyboard3ComboBox;
        private JComboBox optionsKeyboard4ComboBox;
        // for firesettingsPanel
        private JComboBox optionsKeyboard5ComboBox;
        private JComboBox optionsKeyboard6ComboBox;
        private JComboBox optionsKeyboard7ComboBox;
        private JComboBox optionsKeyboard8ComboBox;
        private JComboBox optionsKeyboard9ComboBox;
        private JCheckBox optionsRenderingBufferImagesCheckBox;
       

        private JButton predefinedPlaformDefaultButton;
        private JButton predefinedSpeedButton;
        private JButton predefinedQualityButton;


        private EventHandler eventHandler;

        public PreferencesKeyConfigurationOptionsTab(RobocodeManager manager) {
                super();
                this.manager = manager;
                initialize();
        }

        private void initialize() {
                eventHandler = new EventHandler();

                setLayout(new GridLayout(1, 3));

                add(getDirectionSettingsPanel());
                add(getFireSettingsPanel());
                add(getRadarSettingsPanel());
       

                loadPreferences(manager.getProperties());
        }

        private JPanel getDirectionSettingsPanel() {
       
                if (specificSettingsPanel == null) {
                        specificSettingsPanel = new JPanel();
                        specificSettingsPanel.setBorder(
                               
BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),
"Direction settings"));
                        specificSettingsPanel.setLayout(new GridBagLayout());

                        GridBagConstraints c = new GridBagConstraints();

                        c.fill = GridBagConstraints.HORIZONTAL;
                        c.insets = new Insets(6,8,6,8);
                        c.anchor = GridBagConstraints.PAGE_START;

                        c.weightx = 2;

                        c.gridwidth = 4;

                        c.gridx = 0;
                        c.gridy = 0;
                        specificSettingsPanel.add(new JLabel("Set Direction of Robot:"), c);

                        c.gridwidth = 1;

                        c.gridy = 1;
                        specificSettingsPanel.add(new JLabel("RIGHT",
SwingConstants.RIGHT), c);
                        c.gridx = 1;
                        specificSettingsPanel.add(getOptionsKeyboardComboBox(), c);

                        c.gridx = 0;
                        c.gridy = 2;
                        specificSettingsPanel.add(new JLabel("LEFT", SwingConstants.RIGHT), c);
                        c.gridx = 1;
                        specificSettingsPanel.add(getOptionsKeyboard2ComboBox(), c);

                        c.gridx = 0;
                        c.gridy = 3;
                        specificSettingsPanel.add(new JLabel("UP", SwingConstants.RIGHT), c);
                        c.gridx = 1;
                        specificSettingsPanel.add(getOptionsKeyboard3ComboBox(), c);

                        c.gridx = 0;
                        c.gridy = 4;
                        specificSettingsPanel.add(new JLabel("DOWN", SwingConstants.RIGHT), c);
                        c.gridx = 1;
                        specificSettingsPanel.add(getOptionsKeyboard4ComboBox(), c);

                       
                }
                return specificSettingsPanel;
        }

        private JPanel getFireSettingsPanel() {
                if (fireSettingsPanel == null) {
                        fireSettingsPanel = new JPanel();
                        fireSettingsPanel.setBorder(
                               
BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),
"Power settings"));
                        fireSettingsPanel.setLayout(new GridBagLayout());

                        GridBagConstraints c = new GridBagConstraints();

                        c.fill = GridBagConstraints.HORIZONTAL;
                        c.insets = new Insets(6,8,6,8);
                        c.anchor = GridBagConstraints.PAGE_START;

                        c.weightx = 2;

                        c.gridwidth = 8;

                        c.gridx = 0;
                        c.gridy = 0;
                        fireSettingsPanel.add(new JLabel("Power Setting:"), c);

                        c.gridwidth = 1;

                        c.gridy = 1;
                        fireSettingsPanel.add(new JLabel("PowerKey 1",
SwingConstants.RIGHT), c);
                        c.gridx = 1;
                        fireSettingsPanel.add(getOptionsKeyboard5ComboBox(), c);

                        c.gridx = 0;
                        c.gridy = 2;
                        fireSettingsPanel.add(new JLabel("PowerKey 2",
SwingConstants.RIGHT), c);
                        c.gridx = 1;
                        fireSettingsPanel.add(getOptionsKeyboard6ComboBox(), c);

                        c.gridx = 0;
                        c.gridy = 3;
                        fireSettingsPanel.add(new JLabel("PowerKey 3",
SwingConstants.RIGHT), c);
                        c.gridx = 1;
                        fireSettingsPanel.add(getOptionsKeyboard7ComboBox(), c);
                       
                }
                return fireSettingsPanel;
        }
       
        private JPanel getRadarSettingsPanel() {
                if (RadarSettingsPanel == null) {
                        RadarSettingsPanel = new JPanel();
                        RadarSettingsPanel.setBorder(
                               
BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),
"Radar Settings"));
                        RadarSettingsPanel.setLayout(new GridBagLayout());

                        GridBagConstraints c = new GridBagConstraints();

                        c.fill = GridBagConstraints.HORIZONTAL;
                        c.insets = new Insets(6,8,6,8);
                        c.anchor = GridBagConstraints.PAGE_START;

                        c.weightx = 2;

                        c.gridwidth = 5;

                        c.gridx = 0;
                        c.gridy = 0;
                        RadarSettingsPanel.add(new JLabel("Set Radar Button Key:"), c);

                        c.gridwidth = 1;
                                               
                        c.gridx = 0;
                        c.gridy = 4;
                        RadarSettingsPanel.add(new JLabel("RadarKey 1",
SwingConstants.RIGHT), c);
                        c.gridx = 1;
                        RadarSettingsPanel.add(getOptionsKeyboard8ComboBox(), c);

                        c.gridx = 0;
                        c.gridy = 5;
                        RadarSettingsPanel.add(new JLabel("RadarKey 2",
SwingConstants.RIGHT), c);
                        c.gridx = 1;
                        RadarSettingsPanel.add(getOptionsKeyboard9ComboBox(), c);

                       
                }
                return RadarSettingsPanel;
        }
       


        private JComboBox getOptionsKeyboardComboBox() {
                if (optionsKeyboardComboBox == null) {
                        optionsKeyboardComboBox = new JComboBox(new String[] { " ","A",
"S", "D", "X"});
                        optionsKeyboardComboBox.addActionListener(eventHandler);
                       
                }
                return optionsKeyboardComboBox;
        }

        private JComboBox getOptionsKeyboard2ComboBox() {
                if (optionsKeyboard2ComboBox == null) {
                        optionsKeyboard2ComboBox = new JComboBox(new String[] { "S", "
","A", "D", "X"});
                        optionsKeyboard2ComboBox.addActionListener(eventHandler);
                }
                return optionsKeyboard2ComboBox;
        }

        private JComboBox getOptionsKeyboard3ComboBox() {
                if (optionsKeyboard3ComboBox == null) {
                        optionsKeyboard3ComboBox = new JComboBox(new String[] {  "D", "S",
"D", "X"});
                        optionsKeyboard3ComboBox.addActionListener(eventHandler);
                }
                return optionsKeyboard3ComboBox;
        }
       
        private JComboBox getOptionsKeyboard4ComboBox() {
                if (optionsKeyboard4ComboBox == null) {
                        optionsKeyboard4ComboBox = new JComboBox(new String[] {  "X", "S",
"D", "X"});
                        optionsKeyboard4ComboBox.addActionListener(eventHandler);
                }
                return optionsKeyboard4ComboBox;
        }
       
        private JComboBox getOptionsKeyboard5ComboBox() {
                if (optionsKeyboard5ComboBox == null) {
                        optionsKeyboard5ComboBox = new JComboBox(new String[] { " ","A",
"S", "D", "X"});
                        optionsKeyboard5ComboBox.addActionListener(eventHandler);
                }
                return optionsKeyboard5ComboBox;
        }

        private JComboBox getOptionsKeyboard6ComboBox() {
                if (optionsKeyboard6ComboBox == null) {
                        optionsKeyboard6ComboBox = new JComboBox(new String[] { " ","A",
"S", "D", "X"});
                        optionsKeyboard6ComboBox.addActionListener(eventHandler);
                }
                return optionsKeyboard6ComboBox;
        }
        private JComboBox getOptionsKeyboard7ComboBox() {
                if (optionsKeyboard7ComboBox == null) {
                        optionsKeyboard7ComboBox = new JComboBox(new String[] { " ","A",
"S", "D", "X"});
                        optionsKeyboard7ComboBox.addActionListener(eventHandler);
                }
                return optionsKeyboard7ComboBox;
        }


        private JComboBox getOptionsKeyboard8ComboBox() {
                if (optionsKeyboard8ComboBox == null) {
                        optionsKeyboard8ComboBox = new JComboBox(new String[] { " ","A",
"S", "D", "X"});
                        optionsKeyboard8ComboBox.addActionListener(eventHandler);
                }
                return optionsKeyboard8ComboBox;
        }

        private JComboBox getOptionsKeyboard9ComboBox() {
                if (optionsKeyboard9ComboBox == null) {
                        optionsKeyboard9ComboBox = new JComboBox(new String[] { " ","A",
"S", "D", "X"});
                        optionsKeyboard9ComboBox.addActionListener(eventHandler);
                }
                return optionsKeyboard9ComboBox;
        }





        private void loadPreferences(RobocodeProperties props) {
       
getOptionsKeyboardComboBox().setSelectedIndex(props.getOptionsRenderingAntialiasing());
       
getOptionsKeyboard2ComboBox().setSelectedIndex(props.getOptionsRenderingTextAntialiasing());
       
getOptionsKeyboard3ComboBox().setSelectedIndex(props.getOptionsRenderingMethod());
       
getOptionsKeyboard4ComboBox().setSelectedIndex(props.getOptionsRenderingMethod());
       
getOptionsKeyboard5ComboBox().setSelectedIndex(props.getOptionsRenderingMethod());
       
getOptionsKeyboard6ComboBox().setSelectedIndex(props.getOptionsRenderingMethod());
       
getOptionsKeyboard7ComboBox().setSelectedIndex(props.getOptionsRenderingMethod());
       
getOptionsKeyboard8ComboBox().setSelectedIndex(props.getOptionsRenderingMethod());
       
getOptionsKeyboard9ComboBox().setSelectedIndex(props.getOptionsRenderingMethod());
        }
       

        public void storePreferences() {
                RobocodeProperties props = manager.getProperties();

       
props.setOptionsRenderingAntialiasing(optionsKeyboardComboBox.getSelectedIndex());
       
props.setOptionsRenderingTextAntialiasing(optionsKeyboard2ComboBox.getSelectedIndex());
       
props.setOptionsRenderingMethod(optionsKeyboard3ComboBox.getSelectedIndex());
       
props.setOptionsRenderingMethod(optionsKeyboard4ComboBox.getSelectedIndex());
       
props.setOptionsRenderingMethod(optionsKeyboard5ComboBox.getSelectedIndex());
       
props.setOptionsRenderingMethod(optionsKeyboard6ComboBox.getSelectedIndex());
       
props.setOptionsRenderingMethod(optionsKeyboard7ComboBox.getSelectedIndex());
       
props.setOptionsRenderingMethod(optionsKeyboard8ComboBox.getSelectedIndex());
       
props.setOptionsRenderingMethod(optionsKeyboard9ComboBox.getSelectedIndex());
                manager.saveProperties();
        }

        @Override
        public boolean isReady() {
                return true;
        }

        private void setPredefinedSettings(int index) {
                optionsKeyboardComboBox.setSelectedIndex(index);
                optionsKeyboard2ComboBox.setSelectedIndex(index);
                optionsKeyboard3ComboBox.setSelectedIndex(index);
        }

        private class EventHandler implements ActionListener {

                public void actionPerformed(ActionEvent e) {
                         Object src = e.getSource();
                       
                        if (src == predefinedPlaformDefaultButton) {
                                setPredefinedSettings(0);
                               
                        } else if (src == predefinedQualityButton) {
                                setPredefinedSettings(1);
                        } else if (src == predefinedSpeedButton) {
                                setPredefinedSettings(2);
                        } else if (src == optionsRenderingBufferImagesCheckBox) {
                                // Reset images so they are reloaded and gets buffered or unbuffered
                                new Thread() {
                                        @Override
                                        public void run() {
                                                storePreferences();
                                                manager.getImageManager().initialize();
                                        }
                                }.start();
                                return;
                        }
               
manager.getWindowManager().getRobocodeFrame().getBattleView().setInitialized(false);
                }
        }
}


NOW I WANT USER TO GET KEY CONFIGURA FROM
PreferencesKeyConfigurationOptionsTab.JAVA

I HOPE U UNDERSTAND THIS PROBLEM.

I MEAN WHEN USER LEFT KEY FROM COMBOBOX.It will directly assigned to
Left in Sprobot.java ( which is my robot).

Also i want to send These three  java files.here is the link
1.PreferencesDialog.java
http://www.sendspace.com/file/6kgbv4
2.PreferencesKeyConfigurationOptionsTab.java
http://www.sendspace.com/file/rkzn2d
3.Uniteam3a SEPERATE PROJECT FOR Robot full package in rar format
http://www.sendspace.com/file/zkrv1m








 « Return to Thread: Keyboard Configuration Tab Big Problem