Jump to content
Sign in to follow this  
Elnof

Help

Recommended Posts

I have a problem. I have a program I'm working on, and I'm not sure how I want to utilize the menu, and how to display constantly changing text... If someone could help me, I'd apreciate it!Here's my code:

import java.awt.*;import java.awt.event.*;import javax.swing.JMenu;import javax.swing.JMenuItem;import javax.swing.JRadioButtonMenuItem;import javax.swing.ButtonGroup;import javax.swing.JMenuBar;import javax.swing.KeyStroke;import javax.swing.JPanel;import javax.swing.JTextArea;import javax.swing.JScrollPane;import javax.swing.JFrame;public class NXT_control_center_1_0_0 {static JTextArea output;JScrollPane scrollPane;public JMenuBar createMenuBar() {JMenuBar menuBar;JMenu menu, submenu;JMenuItem menuItem;JRadioButtonMenuItem rbMenuItem;//Create the menu bar.menuBar = new JMenuBar();//Build the first menu.menu = new JMenu("File");menu.setMnemonic(KeyEvent.VK_A);menu.getAccessibleContext().setAccessibleDescription("The File Menu");menuBar.add(menu);//Connect or disconnect from icommandButtonGroup group = new ButtonGroup();rbMenuItem = new JRadioButtonMenuItem("Connect to icommand");rbMenuItem.setSelected(true);rbMenuItem.setMnemonic(KeyEvent.VK_C);group.add(rbMenuItem);menu.add(rbMenuItem);rbMenuItem = new JRadioButtonMenuItem("Disconnect from icommand");rbMenuItem.setMnemonic(KeyEvent.VK_D);group.add(rbMenuItem);menu.add(rbMenuItem);menu.addSeparator();menuItem = new JMenuItem("Quit",KeyEvent.VK_Q);//menuItem.setMnemonic(KeyEvent.VK_T); //used constructor insteadmenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.ALT_MASK));menuItem.getAccessibleContext().setAccessibleDescription("Quit the program");menu.add(menuItem);//Build second menu in the menu bar.menu = new JMenu("Motors");menu.setMnemonic(KeyEvent.VK_N);menu.getAccessibleContext().setAccessibleDescription("Change what buttons do what to the motors.");//Create Submenu for Wsubmenu = new JMenu("W");submenu.setMnemonic(KeyEvent.VK_W);//Submenu W for motor AButtonGroup wMotorA = new ButtonGroup();rbMenuItem = new JRadioButtonMenuItem("Motor A forwards");rbMenuItem.setSelected(true);rbMenuItem.setMnemonic(KeyEvent.VK_A);wMotorA.add(rbMenuItem);submenu.add(rbMenuItem);rbMenuItem = new JRadioButtonMenuItem("Motor A backwards");rbMenuItem.setMnemonic(KeyEvent.VK_K);wMotorA.add(rbMenuItem);submenu.add(rbMenuItem);rbMenuItem = new JRadioButtonMenuItem("No Motor A");rbMenuItem.setMnemonic(KeyEvent.VK_M);wMotorA.add(rbMenuItem);submenu.add(rbMenuItem);submenu.addSeparator();//Submenu W for motor BButtonGroup wMotorB = new ButtonGroup();rbMenuItem = new JRadioButtonMenuItem("Motor B forwards");rbMenuItem.setSelected(true);rbMenuItem.setMnemonic(KeyEvent.VK_;wMotorB.add(rbMenuItem);submenu.add(rbMenuItem);rbMenuItem = new JRadioButtonMenuItem("Motor B backwards");rbMenuItem.setMnemonic(KeyEvent.VK_K);wMotorB.add(rbMenuItem);submenu.add(rbMenuItem);rbMenuItem = new JRadioButtonMenuItem("No Motor B");rbMenuItem.setMnemonic(KeyEvent.VK_O);wMotorB.add(rbMenuItem);submenu.add(rbMenuItem);submenu.addSeparator();//Submenu W for motor CButtonGroup wMotorC = new ButtonGroup();rbMenuItem = new JRadioButtonMenuItem("Motor C forwards");rbMenuItem.setSelected(true);rbMenuItem.setMnemonic(KeyEvent.VK_C);wMotorC.add(rbMenuItem);submenu.add(rbMenuItem);rbMenuItem = new JRadioButtonMenuItem("Motor C backwards");rbMenuItem.setMnemonic(KeyEvent.VK_D);wMotorC.add(rbMenuItem);submenu.add(rbMenuItem);rbMenuItem = new JRadioButtonMenuItem("No Motor C");rbMenuItem.setMnemonic(KeyEvent.VK_R);wMotorC.add(rbMenuItem);submenu.add(rbMenuItem);menu.add(submenu);//Create Submenu for Asubmenu = new JMenu("A");submenu.setMnemonic(KeyEvent.VK_A);//Submenu A for motor AButtonGroup aMotorA = new ButtonGroup();rbMenuItem = new JRadioButtonMenuItem("Motor A forwards");rbMenuItem.setSelected(true);rbMenuItem.setMnemonic(KeyEvent.VK_A);aMotorA.add(rbMenuItem);submenu.add(rbMenuItem);rbMenuItem = new JRadioButtonMenuItem("Motor A backwards");rbMenuItem.setMnemonic(KeyEvent.VK_K);aMotorA.add(rbMenuItem);submenu.add(rbMenuItem);rbMenuItem = new JRadioButtonMenuItem("No Motor A");rbMenuItem.setMnemonic(KeyEvent.VK_M);aMotorA.add(rbMenuItem);submenu.add(rbMenuItem);submenu.addSeparator();//Submenu A for motor BButtonGroup aMotorB = new ButtonGroup();rbMenuItem = new JRadioButtonMenuItem("Motor B forwards");rbMenuItem.setSelected(true);rbMenuItem.setMnemonic(KeyEvent.VK_;aMotorB.add(rbMenuItem);submenu.add(rbMenuItem);rbMenuItem = new JRadioButtonMenuItem("Motor B backwards");rbMenuItem.setMnemonic(KeyEvent.VK_K);aMotorB.add(rbMenuItem);submenu.add(rbMenuItem);rbMenuItem = new JRadioButtonMenuItem("No Motor B");rbMenuItem.setMnemonic(KeyEvent.VK_O);aMotorB.add(rbMenuItem);submenu.add(rbMenuItem);submenu.addSeparator();//Submenu A for motor CButtonGroup aMotorC = new ButtonGroup();rbMenuItem = new JRadioButtonMenuItem("Motor C forwards");rbMenuItem.setSelected(true);rbMenuItem.setMnemonic(KeyEvent.VK_C);aMotorC.add(rbMenuItem);submenu.add(rbMenuItem);rbMenuItem = new JRadioButtonMenuItem("Motor C backwards");rbMenuItem.setMnemonic(KeyEvent.VK_D);aMotorC.add(rbMenuItem);submenu.add(rbMenuItem);rbMenuItem = new JRadioButtonMenuItem("No Motor C");rbMenuItem.setMnemonic(KeyEvent.VK_R);aMotorC.add(rbMenuItem);submenu.add(rbMenuItem);menu.add(submenu);//Create Submenu for Ssubmenu = new JMenu("S");submenu.setMnemonic(KeyEvent.VK_S);//Submenu S for motor AButtonGroup sMotorA = new ButtonGroup();rbMenuItem = new JRadioButtonMenuItem("Motor A forwards");rbMenuItem.setSelected(true);rbMenuItem.setMnemonic(KeyEvent.VK_A);sMotorA.add(rbMenuItem);submenu.add(rbMenuItem);rbMenuItem = new JRadioButtonMenuItem("Motor A backwards");rbMenuItem.setMnemonic(KeyEvent.VK_K);sMotorA.add(rbMenuItem);submenu.add(rbMenuItem);rbMenuItem = new JRadioButtonMenuItem("No Motor A");rbMenuItem.setMnemonic(KeyEvent.VK_M);sMotorA.add(rbMenuItem);submenu.add(rbMenuItem);submenu.addSeparator();//Submenu S for motor BButtonGroup sMotorB = new ButtonGroup();rbMenuItem = new JRadioButtonMenuItem("Motor B forwards");rbMenuItem.setSelected(true);rbMenuItem.setMnemonic(KeyEvent.VK_;sMotorB.add(rbMenuItem);submenu.add(rbMenuItem);rbMenuItem = new JRadioButtonMenuItem("Motor B backwards");rbMenuItem.setMnemonic(KeyEvent.VK_K);sMotorB.add(rbMenuItem);submenu.add(rbMenuItem);rbMenuItem = new JRadioButtonMenuItem("No Motor B");rbMenuItem.setMnemonic(KeyEvent.VK_O);sMotorB.add(rbMenuItem);submenu.add(rbMenuItem);submenu.addSeparator();//Submenu S for motor CButtonGroup sMotorC = new ButtonGroup();rbMenuItem = new JRadioButtonMenuItem("Motor C forwards");rbMenuItem.setSelected(true);rbMenuItem.setMnemonic(KeyEvent.VK_C);sMotorC.add(rbMenuItem);submenu.add(rbMenuItem);rbMenuItem = new JRadioButtonMenuItem("Motor C backwards");rbMenuItem.setMnemonic(KeyEvent.VK_D);sMotorC.add(rbMenuItem);submenu.add(rbMenuItem);rbMenuItem = new JRadioButtonMenuItem("No Motor C");rbMenuItem.setMnemonic(KeyEvent.VK_R);sMotorC.add(rbMenuItem);submenu.add(rbMenuItem);menu.add(submenu);//Create Submenu for Dsubmenu = new JMenu("D");submenu.setMnemonic(KeyEvent.VK_D);//Submenu D for motor AButtonGroup dMotorA = new ButtonGroup();rbMenuItem = new JRadioButtonMenuItem("Motor A forwards");rbMenuItem.setSelected(true);rbMenuItem.setMnemonic(KeyEvent.VK_A);dMotorA.add(rbMenuItem);submenu.add(rbMenuItem);rbMenuItem = new JRadioButtonMenuItem("Motor A backwards");rbMenuItem.setMnemonic(KeyEvent.VK_K);dMotorA.add(rbMenuItem);submenu.add(rbMenuItem);rbMenuItem = new JRadioButtonMenuItem("No Motor A");rbMenuItem.setMnemonic(KeyEvent.VK_M);dMotorA.add(rbMenuItem);submenu.add(rbMenuItem);submenu.addSeparator();//Submenu D for motor BButtonGroup dMotorB = new ButtonGroup();rbMenuItem = new JRadioButtonMenuItem("Motor B forwards");rbMenuItem.setSelected(true);rbMenuItem.setMnemonic(KeyEvent.VK_;dMotorB.add(rbMenuItem);submenu.add(rbMenuItem);rbMenuItem = new JRadioButtonMenuItem("Motor B backwards");rbMenuItem.setMnemonic(KeyEvent.VK_K);dMotorB.add(rbMenuItem);submenu.add(rbMenuItem);rbMenuItem = new JRadioButtonMenuItem("No Motor B");rbMenuItem.setMnemonic(KeyEvent.VK_O);dMotorB.add(rbMenuItem);submenu.add(rbMenuItem);submenu.addSeparator();//Submenu D for motor CButtonGroup dMotorC = new ButtonGroup();rbMenuItem = new JRadioButtonMenuItem("Motor C forwards");rbMenuItem.setSelected(true);rbMenuItem.setMnemonic(KeyEvent.VK_C);dMotorC.add(rbMenuItem);submenu.add(rbMenuItem);rbMenuItem = new JRadioButtonMenuItem("Motor C backwards");rbMenuItem.setMnemonic(KeyEvent.VK_D);dMotorC.add(rbMenuItem);submenu.add(rbMenuItem);rbMenuItem = new JRadioButtonMenuItem("No Motor C");rbMenuItem.setMnemonic(KeyEvent.VK_R);dMotorC.add(rbMenuItem);submenu.add(rbMenuItem);menu.add(submenu);//Create Submenu for Qsubmenu = new JMenu("Q");submenu.setMnemonic(KeyEvent.VK_Q);//Submenu Q for motor AButtonGroup qMotorA = new ButtonGroup();rbMenuItem = new JRadioButtonMenuItem("Motor A forwards");rbMenuItem.setSelected(true);rbMenuItem.setMnemonic(KeyEvent.VK_A);qMotorA.add(rbMenuItem);submenu.add(rbMenuItem);rbMenuItem = new JRadioButtonMenuItem("Motor A backwards");rbMenuItem.setMnemonic(KeyEvent.VK_K);qMotorA.add(rbMenuItem);submenu.add(rbMenuItem);rbMenuItem = new JRadioButtonMenuItem("No Motor A");rbMenuItem.setMnemonic(KeyEvent.VK_M);qMotorA.add(rbMenuItem);submenu.add(rbMenuItem);submenu.addSeparator();//Submenu Q for motor BButtonGroup qMotorB = new ButtonGroup();rbMenuItem = new JRadioButtonMenuItem("Motor B forwards");rbMenuItem.setSelected(true);rbMenuItem.setMnemonic(KeyEvent.VK_;qMotorB.add(rbMenuItem);submenu.add(rbMenuItem);rbMenuItem = new JRadioButtonMenuItem("Motor B backwards");rbMenuItem.setMnemonic(KeyEvent.VK_K);qMotorB.add(rbMenuItem);submenu.add(rbMenuItem);rbMenuItem = new JRadioButtonMenuItem("No Motor B");rbMenuItem.setMnemonic(KeyEvent.VK_O);qMotorB.add(rbMenuItem);submenu.add(rbMenuItem);submenu.addSeparator();//Submenu Q for motor CButtonGroup qMotorC = new ButtonGroup();rbMenuItem = new JRadioButtonMenuItem("Motor C forwards");rbMenuItem.setSelected(true);rbMenuItem.setMnemonic(KeyEvent.VK_C);qMotorC.add(rbMenuItem);submenu.add(rbMenuItem);rbMenuItem = new JRadioButtonMenuItem("Motor C backwards");rbMenuItem.setMnemonic(KeyEvent.VK_D);qMotorC.add(rbMenuItem);submenu.add(rbMenuItem);rbMenuItem = new JRadioButtonMenuItem("No Motor C");rbMenuItem.setMnemonic(KeyEvent.VK_R);qMotorC.add(rbMenuItem);submenu.add(rbMenuItem);menu.add(submenu);menuBar.add(menu);return menuBar;}public Container createContentPane() {//Create the content-pane-to-be.dJPanel contentPane = new JPanel(new BorderLayout());contentPane.setOpaque(true);//Create a scrolled text area.output = new JTextArea(5, 30);output.setEditable(false);scrollPane = new JScrollPane(output);//Add the text area to the content pane.contentPane.add(scrollPane, BorderLayout.CENTER);return contentPane;}/*** 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.JFrame frame = new JFrame("NXT Control Center 1.0.0");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//Create and set up the content pane.NXT_control_center_1_0_0 demo = new NXT_control_center_1_0_0();frame.setJMenuBar(demo.createMenuBar());frame.setContentPane(demo.createContentPane());//Display the window.frame.setSize(650, 460);frame.setVisible(true);}public static void main(String[] args) {//Schedule a job for the event-dispatching thread://creating and showing this application's GUI.javax.swing.SwingUtilities.invokeLater(new Runnable() {public void run() {createAndShowGUI();}});}}

Share this post


Link to post
Share on other sites

what do you mean by constantly changing text? are you writing a Content Management System (dynamic content)?

Share this post


Link to post
Share on other sites

It's a program to controll a robot from your computer. And it will have fields where it displays the readings from the robots sensors. So, I would like the test to be constantly getting the information from the robot, and changing to suit the data. It might be considered dynamic content...

Share this post


Link to post
Share on other sites

okay, I assumed you were refering to web pages. I am not very familiar with Java programs.I don't know if this will help you or not but hopefully it helps you find the right plac to look.Windows programs are constantly processing messages sent by the program (ie it is always listening for key presses and waiting to be told to re paint the screen.In C# when programming a game you override the OnPaint method and do all your drwaing then the last line you tell the screen immediately repaint it's self.Do you know that method (or whatever) listens for and handles the data sent by the robot sensors? You will probably have to override that method and force it to continually loop.

Share this post


Link to post
Share on other sites

Well, I could do that, but I think that I might be able to create a variable to gather the information... But the problem of repainting the screen still remains...

Share this post


Link to post
Share on other sites

I was using painting as an example. The OnPaint is just one of many messages that windows listens for. You need to find the correct message being sent fromt he robot and override the method that handles it.Does the robot come with some documentation or an API?

Share this post


Link to post
Share on other sites

Not that I know of... It might. But I'm not following, it's not the robot that paints the screen, it's the program I'm trying to make.

Share this post


Link to post
Share on other sites

forget the paint idea, it was just an example! Where is the robot from? Where did you buy it?Give me some info and I could probably dig asomething up for you. I can't see any company selling a programmable robot without any documentation!

Share this post


Link to post
Share on other sites

It's a Lego NXT, using the lejos firmware. Also, I have done some work, and I have new code, but it doesn't quite work.

[b][size=2][color="#7f0055"]
[left]import[/b][/color][/size][size=2] java.awt.event.*;

[/size]

import java.awt.*;

import javax.swing.ImageIcon;

public class NXT2 implements KeyListener{

/**

*

*/

private static final long serialVersionUID = 1L;

Color unEdit = new Color(211, 211, 211);

final int FORWARD = 87, // W = forward

BACKWARD = 83, // S = backward

LEFT = 65, // A = left turn

RIGHT = 68, // D = right

QUIT = 81; // Q = quit

TextField output;

public TextField createMessage() {

output = new TextField("Welcome to NXT Command Center 1.0.0!");

output.setEditable(false);

output.setBackground(unEdit);

output.setFocusable(false);

output.setForeground(Color.RED);

return output;

}

public NXT2() {

//Create frame

Frame command = new Frame("NXT Command Center 1.0.0");

Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

int width = ((int) dim.getWidth() / 2) - 300;

int height = ((int) dim.getHeight() / 2) - 225;

command.setBounds(width, height, 600, 450);

//Add Keylisteners and make it non-resizeable

command.addKeyListener(this);

command.setResizable(false);

//Set the icon

ImageIcon icon = new ImageIcon("icon_08.png");

command.setIconImage(icon.getImage());

createMessage();

command.add(output, "South");

//Make it visible, and possible to exit.

command.setVisible(true);

command.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent evt) {

// Exit the application

System.exit(0);

}

});

}

public void keyPressed(KeyEvent e) {

switch(e.getKeyCode()) {

case FORWARD:

output.setText("process: UNABLE to complete command - Forward");

break;

case BACKWARD:

output.setText("process: UNABLE to complete command - Backward");

break;

case LEFT:

output.setText("process: UNABLE to complete command - Left");

break;

case RIGHT:

output.setText("process: UNABLE to complete command - Right");

break;

}

}

public void keyReleased(KeyEvent e) {

switch(e.getKeyCode()) {

case FORWARD:

case BACKWARD:

output.setText("process: UNABLE to complete command");

break;

case LEFT:

case RIGHT:

output.setText("process: UNABLE to complete command");

break;

case QUIT:

output.setText("Good Bye!");

System.exit(0);

}

}

public static void main(String[] args) {

new NXT2();

}

public void keyTyped(KeyEvent arg0) {

// TODO Auto-generated method stub

}

}[/left]

Share this post


Link to post
Share on other sites

which sensor are you trying to capture? The API for lejos has several listners for different sensorsColorListnerLightListenerMotionListnerHave you read their tutorial?

Share this post


Link to post
Share on other sites

Yea, I've bought the book about it. But that's not my problem, I'm having trouble with the making of the actual program. I understand the leJOS stuff.PS: Wrong link, thats for the stuff from 2002

Share this post


Link to post
Share on other sites

does the book you bought and the tutorials explain how to make a program? I am having a hard time understanding what exactly you are having trouble with and what do and don't know.

Share this post


Link to post
Share on other sites

The book explains how to use the leJOS API, not how to write in Java. I'm having trouble figuring out how to add a menu to a regulare Java frame. I know how to add one to a Jframe, but not to a regulare frame.

Share this post


Link to post
Share on other sites
The book explains how to use the leJOS API, not how to write in Java. I'm having trouble figuring out how to add a menu to a regulare Java frame. I know how to add one to a Jframe, but not to a regulare frame.
Hello,I'm not too familiar with Swing, as I am more of a Java web app developer, but my Java book states the following about Menus under a section titled 'Using Menus with Frames':"In Swing GUIs, menus can be attached only to objects of the classes that provide method setJMenuBar. Two such classes are JFrame and JApplet. The classes used to declare menus are JMenuBar, JMenu, JMenuItem, JCheckBoxMenuItem and class JRadioButtonMenuItem."Hope this helps.

Share this post


Link to post
Share on other sites

Ok, thanks, I think I figured it out, but now I have another problem:

java.lang.NoClassDefFoundError: NXT2

Exception in thread "main" I resently changed the project name from NXT2 to Nxt_Command_Center_1_0_0. PS: The book was for the leJOS NXJ api. Not all of java.

[left][left]package nxt_command_center;

import java.awt.event.*;

import java.awt.*;

import javax.swing.BorderFactory;

import javax.swing.ButtonGroup;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JCheckBox;

import javax.swing.JComboBox;

import javax.swing.JFrame;

import javax.swing.JRadioButton;

import javax.swing.JTabbedPane;

import javax.swing.border.Border;

public class NXT_Command_Center implements KeyListener{

/**

*

*/

private static final long serialVersionUID = (long) 1.0;

Color unEdit = new Color(211, 211, 211);

Frame command;

final int FORWARD = 38, // Up = forward

BACKWARD = 40, // Down = backward

LEFT = 37, // Left = left turn

RIGHT = 39, // Right = right

QUIT = 27, //Escape

SPACE = 32, // Space

SHIFT = 16, // Shift = what ever

SETTINGS = 112, //F1 = Settings

ABOUT = 114, // F3 = about

REPAINT = 113; //F2 = repaint

Frame setIn;

JFrame JAbout;

boolean windowOpen = false;

boolean cmOrIn = true;

boolean dgOrRt = true;

boolean resetYOR = false;

int up1Int = 2;

int up2Int = 4;

int down1Int = 3;

int down2Int = 5;

int left1Int = 3;

int left2Int = 4;

int right1Int = 2;

int right2Int = 5;

int space1Int = 0;

int space2Int = 6;

int shift1Int = 1;

int shift2Int = 6;

int curWindow = 0;

TextField output;

TextField sen1;

TextField sen2;

TextField sen3;

TextField sen4;

TextField mot1;

TextField mot2;

TextField mot3;

Panel sensors;

Panel motors;

Panel sensors1;

Panel sensors2;

Panel sensors3;

Panel sensors4;

Panel motors1;

Panel motors2;

Panel motors3;

FlowLayout panelsCenter = new FlowLayout(FlowLayout.CENTER);

FlowLayout panelsLeft = new FlowLayout(FlowLayout.LEFT);

Label s1;

Label s2;

Label s3;

Label s4;

public class settings implements ActionListener {

/**

*

*/

private static final long serialVersionUID = 1L;

Label motorALabel;

Label motorBLabel;

Label motorCLabel;

Label upArrow;

Label downArrow;

Label leftArrow;

Label rightArrow;

Label space;

Label shift;

Label sonarLabel;

Label motorLabel;

Border loweredbevel = BorderFactory.createLoweredBevelBorder();

JTabbedPane navigation;

Panel motorSettingsPanel;

Panel upArrowPanel;

Panel downArrowPanel;

Panel leftArrowPanel;

Panel rightArrowPanel;

Panel spacePanel;

Panel shiftPanel;

Panel keySettingsPanel;

Panel infoSettingsPanel;

Panel motorSettingsA;

Panel motorSettingsB;

Panel motorSettingsC;

Panel curSettings;

Panel sonarPanel;

Panel motorsPanel;

Panel motor2;

Panel checkBox;

TextField motorAText;

TextField motorBText;

TextField motorCText;

JComboBox up1;

JComboBox up2;

JComboBox down1;

JComboBox down2;

JComboBox left1;

JComboBox left2;

JComboBox right1;

JComboBox right2;

JComboBox space1;

JComboBox space2;

JComboBox shift1;

JComboBox shift2;

JRadioButton sonarCM;

JRadioButton sonarIN;

JRadioButton motorRT;

JRadioButton motorDG;

ButtonGroup sonar;

ButtonGroup motor;

JCheckBox reset;

JButton keySubmit;

JButton submitMotors;

Color grey = new Color(220, 220, 220);

public Panel infoSettings() {

infoSettingsPanel = new Panel(new GridLayout(3,1));

sonar = new ButtonGroup();

sonarPanel = new Panel(panelsLeft);

sonarLabel = new Label("Sonar:");

sonarPanel.add(sonarLabel);

sonarCM = new JRadioButton("Centimeters");

sonarCM.addActionListener(this);

sonarIN = new JRadioButton("Inches");

sonarIN.addActionListener(this);

if (cmOrIn == true) sonarCM.setSelected(true);

else sonarIN.setSelected(true);

sonarCM.setBackground(grey);

sonarIN.setBackground(grey);

sonar.add(sonarCM);

sonar.add(sonarIN);

sonarPanel.add(sonarCM);

sonarPanel.add(sonarIN);

motor = new ButtonGroup();

motorsPanel = new Panel(new GridLayout(2,1));

checkBox = new Panel(new FlowLayout(FlowLayout.RIGHT));

motors2 = new Panel(panelsLeft);

motorLabel = new Label("Motors:");

motors2.add(motorLabel);

motorRT = new JRadioButton("Rotations");

motorDG = new JRadioButton("Degrees");

motorRT.setBackground(grey);

motorRT.addActionListener(this);

motorDG.setBackground(grey);

motorDG.addActionListener(this);

if (dgOrRt == true) motorDG.setSelected(true);

else motorRT.setSelected(true);

motor.add(motorRT);

motor.add(motorDG);

motors2.add(motorRT);

motors2.add(motorDG);

motorsPanel.add(motors2);

reset = new JCheckBox("Reset when motor stops");

reset.setBackground(grey);

reset.addActionListener(this);

if (resetYOR == true) reset.setSelected(true);

checkBox.add(reset);

motorsPanel.add(checkBox);

infoSettingsPanel.add(sonarPanel);

infoSettingsPanel.add(motorsPanel);

return infoSettingsPanel;

}

public Panel keySettings() {

keySettingsPanel = new Panel(new GridLayout(7, 1));

upArrowPanel = new Panel(panelsLeft);

downArrowPanel = new Panel(panelsLeft);

leftArrowPanel = new Panel(panelsLeft);

rightArrowPanel = new Panel(panelsLeft);

spacePanel = new Panel(panelsLeft);

shiftPanel = new Panel(panelsLeft);

String[]List = {"A Forward", "A Backward", "B Forward", "B Backward", "C Forwards", "C Backwards", "None"};

//Combo Boxes for the up arrow

up1 = new JComboBox(List);

up2 = new JComboBox(List);

up1.setSelectedIndex(up1Int);

up2.setSelectedIndex(up2Int);

upArrow = new Label("Up Arrow:");

upArrowPanel.add(upArrow);

upArrowPanel.add(up1);

upArrowPanel.add(up2);

//Combo Boxes for the down arrow

down1 = new JComboBox(List);

down2 = new JComboBox(List);

down1.setSelectedIndex(down1Int);

down2.setSelectedIndex(down2Int);

downArrow = new Label("Down Arrow:");

downArrowPanel.add(downArrow);

downArrowPanel.add(down1);

downArrowPanel.add(down2);

//Combo Boxes for the left arrow

left1 = new JComboBox(List);

left2 = new JComboBox(List);

left1.setSelectedIndex(left1Int);

left2.setSelectedIndex(left2Int);

leftArrow = new Label("Left Arrow:");

leftArrowPanel.add(leftArrow);

leftArrowPanel.add(left1);

leftArrowPanel.add(left2);

//Combo Boxes for the right arrow

right1 = new JComboBox(List);

right2 = new JComboBox(List);

right1.setSelectedIndex(right1Int);

right2.setSelectedIndex(right2Int);

rightArrow = new Label("Right Arrow:");

rightArrowPanel.add(rightArrow);

rightArrowPanel.add(right1);

rightArrowPanel.add(right2);

//Combo Boxes for space

space1 = new JComboBox(List);

space2 = new JComboBox(List);

space1.setSelectedIndex(space1Int);

space2.setSelectedIndex(space2Int);

space = new Label("Space Bar:");

spacePanel.add(space);

spacePanel.add(space1);

spacePanel.add(space2);

//Combo Boxes for shift

shift1 = new JComboBox(List);

shift2 = new JComboBox(List);

shift1.setSelectedIndex(shift1Int);

shift2.setSelectedIndex(shift2Int);

shift = new Label("Shift Key:");

shiftPanel.add(shift);

shiftPanel.add(shift1);

shiftPanel.add(shift2);

keySubmit = new JButton("Submit");

keySubmit.addActionListener(this);

keySettingsPanel.add(upArrowPanel);

keySettingsPanel.add(downArrowPanel);

keySettingsPanel.add(leftArrowPanel);

keySettingsPanel.add(rightArrowPanel);

keySettingsPanel.add(spacePanel);

keySettingsPanel.add(shiftPanel);

keySettingsPanel.add(keySubmit);

return keySettingsPanel;

}

public Panel motorSettings() {

motorSettingsPanel = new Panel(new GridLayout(4,1));

motorAText = new TextField(20);

motorALabel = new Label("Motor A Power:");

motorSettingsA = new Panel(panelsCenter);

motorSettingsA.add(motorALabel);

motorSettingsA.add(motorAText);

motorSettingsPanel.add(motorSettingsA, "North");

motorBText = new TextField(20);

motorBLabel = new Label("Motor B Power:");

motorSettingsB = new Panel(panelsCenter);

motorSettingsB.add(motorBLabel);

motorSettingsB.add(motorBText);

motorSettingsPanel.add(motorSettingsB, "Center");

motorCText = new TextField(20);

motorCLabel = new Label("Motor C Power:");

motorSettingsC = new Panel(panelsCenter);

motorSettingsC.add(motorCLabel);

motorSettingsC.add(motorCText);

motorSettingsPanel.add(motorSettingsC, "South");

submitMotors = new JButton("Submit");

submitMotors.addActionListener(this);

motorSettingsPanel.add(submitMotors, "South");

return motorSettingsPanel;

}

//Create Settings window

settings() {

GridLayout setOut = new GridLayout(2,1);

setOut.setHgap(0);

windowOpen = true;

curWindow = 2;

output.setText("process: settings frame OPENED");

setIn = new Frame("Settings");

setIn.setBackground(Color.WHITE);

motorSettings();

keySettings();

infoSettings();

int location = JTabbedPane.TOP; // or BOTTOM, LEFT, RIGHT

navigation = new JTabbedPane(location);

navigation.setBackground(grey);

navigation.addTab("Motors", motorSettingsPanel);

navigation.addTab("Keys", keySettingsPanel);

navigation.addTab("Information", infoSettingsPanel);

setIn.add(navigation);

Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

int width = ((int) dim.getWidth() / 2) - 170;

int height = (int) ((dim.getHeight() / 2) - 170);

setIn.setBounds(width, height, 340, 340);

setIn.setResizable(false);

setIn.show();

setIn.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent evt) {

// Exit the application

setIn.dispose();

windowOpen = false;

output.setText("process: settings frame CLOSED");

curWindow = 0;

}

});

}

public void actionPerformed(ActionEvent e) {

if (e.getSource() == sonarCM && cmOrIn == false) cmOrIn = true;

else if (e.getSource() == sonarIN && cmOrIn == true) cmOrIn = false;

else if (e.getSource() == reset && resetYOR == false) resetYOR = true;

else if (e.getSource() == motorDG && dgOrRt == false) dgOrRt = true;

else if (e.getSource() == reset && resetYOR == true) resetYOR = false;

else if (e.getSource() == motorRT && dgOrRt == true) dgOrRt = false;

else if (e.getSource() == keySubmit ) {

up1Int = up1.getSelectedIndex();

up2Int = up2.getSelectedIndex();

down1Int = down1.getSelectedIndex();

down2Int = down2.getSelectedIndex();

left1Int = left1.getSelectedIndex();

left2Int = left2.getSelectedIndex();

right1Int = right1.getSelectedIndex();

right2Int = right2.getSelectedIndex();

space1Int = space1.getSelectedIndex();

space2Int = space2.getSelectedIndex();

shift1Int = shift1.getSelectedIndex();

shift2Int = shift2.getSelectedIndex();

}

}

}

public class about extends JFrame{

/**

*

*/

private static final long serialVersionUID = 1L;

JButton Ok;

about() {

JAbout = this;

JAbout.setTitle("About");

JAbout.getContentPane().setBackground(Color.WHITE);

JAbout.resize(340, 340);

JAbout.getContentPane().setLayout(panelsCenter);

Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

int width = ((int) dim.getWidth() / 2) - 170;

int height = (int) ((dim.getHeight() / 2) - 170);

JAbout.move(width, height);

Ok = new JButton("OK");

Ok.addMouseListener(new MouseAdapter() {

public void mouseClicked(MouseEvent e){

if (e.getSource() == Ok) {

JAbout.dispose();

windowOpen = false;

curWindow = 0;

}

}

});

JAbout.getContentPane().add(Ok, "South");

JAbout.setVisible(true);

JAbout.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent evt) {

// Exit the application

JAbout.dispose();

windowOpen = false;

curWindow = 0;

output.setText("process: about frame CLOSED");

}

});

}

}

public TextField createMessage() {

output = new TextField("Welcome to NXT Command Center 1.0.0!");

output.setEditable(false);

output.setBackground(unEdit);

output.setFocusable(false);

output.setForeground(Color.RED);

return output;

}

public TextField createS1(String text) {

sen1 = new TextField(text, 30);

sen1.setFocusable(false);

return sen1;

}

public TextField createS2(String text) {

sen2 = new TextField(text, 30);

sen2.setFocusable(false);

return sen2;

}

public TextField createS3(String text) {

sen3 = new TextField(text, 30);

sen3.setFocusable(false);

return sen3;

}

public TextField createS4(String text) {

sen4 = new TextField(text, 30);

sen4.setFocusable(false);

return sen4;

}

public Panel sensorsInput() {

sensors = new Panel(new GridLayout(4,1));

sensors1 = new Panel(new FlowLayout(FlowLayout.LEFT));

s1 = new Label("Sensor 1:");

sensors2 = new Panel(new FlowLayout(FlowLayout.LEFT));

s2 = new Label("Sensor 2:");

sensors3 = new Panel(new FlowLayout(FlowLayout.LEFT));

s3 = new Label("Sensor 3:");

sensors4 = new Panel(new FlowLayout(FlowLayout.LEFT));

s4 = new Label("Sensor 4:");

sensors1.add(s1);

sensors1.add(createS1("Sensor 1"), "North");

sensors2.add(s2);

sensors2.add(createS2("Sensor 2"), "North");

sensors3.add(s3);

sensors3.add(createS3("Sensor 3"), "North");

sensors4.add(s4);

sensors4.add(createS4("Sensor 4"), "North");

sensors.add(sensors1, "North");

sensors.add(sensors2, "Center");

sensors.add(sensors3, "South");

sensors.add(sensors4, "South");

return sensors;

}

public TextField createM1(String text) {

mot1 = new TextField(text, 30);

mot1.setFocusable(false);

return mot1;

}

public TextField createM2(String text) {

mot2 = new TextField(text, 30);

mot2.setFocusable(false);

return mot2;

}

public TextField createM3(String text) {

mot3 = new TextField(text, 30);

mot3.setFocusable(false);

return mot3;

}

public Panel motorsInput() {

motors = new Panel(new GridLayout(4,1));

motors1 = new Panel(panelsLeft);

Label m1 = new Label("Motor A:");

motors2 = new Panel(panelsLeft);

Label m2 = new Label("Motor B:");

motors3 = new Panel(panelsLeft);

Label m3 = new Label("Motor C:");

motors1.add(m1);

motors1.add(createM1("Motor A"), "Center");

motors2.add(m2);

motors2.add(createM2("Motor B"), "Center");

motors3.add(m3);

motors3.add(createM3("Motor C"), "Center");

//info.add(m41, "North");

//info.add(m42, "Center");

motors.add(motors1, "North");

motors.add(motors2, "Center");

motors.add(motors3, "South");

//motors.add(info, "South");

return sensors;

}

public NXT_Command_Center() {

//Create frame

command = new Frame("NXT Command Center 1.0.0");

Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

int width = ((int) dim.getWidth() / 2) - 325;

int height = ((int) dim.getHeight() / 2) - 225;

command.setBounds(width, height, 650, 450);

//Add Keylisteners and make it non-resizeable

command.addKeyListener(this);

command.setResizable(false);

//Set the icon

ImageIcon icon = new ImageIcon("icon_08.png");

command.setIconImage(icon.getImage());

//Create sensor inputs

sensorsInput();

motorsInput();

createMessage();

command.add(sensors, "West");

command.add(motors, "East");

command.add(output, "South");

//Make it visible, and possible to exit.

command.setVisible(true);

command.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent evt) {

// Exit the application

System.exit(0);

}

});

}

public static void main(String args[]) {

System.out.println("Starting NXT Command Center 1.0.0");

new NXT_Command_Center();

}

public void keyPressed(KeyEvent e) {

switch(e.getKeyCode()) {

case FORWARD:

output.setText("process: UNABLE to complete command - Forward");

break;

case BACKWARD:

output.setText("process: UNABLE to complete command - Backward");

break;

case LEFT:

output.setText("process: UNABLE to complete command - Left");

break;

case RIGHT:

output.setText("process: UNABLE to complete command - Right");

break;

case SPACE:

output.setText("process: UNABLE to complete command - Space");

break;

case SHIFT:

output.setText("process: UNABLE to complete command - Shift");

break;

case REPAINT:

command.repaint();

break;

case SETTINGS:

output.setText("process: settings frame OPENING");

if (windowOpen == false)

new settings();

else {

output.setText("process: UNABLE to complete request - a window is already open");

if (curWindow == 2)

setIn.requestFocus();

}

break;

case ABOUT:

if (windowOpen == false) {

new about();

windowOpen = true;

curWindow = 1;

}

else {

output.setText("process: UNABLE to complete request - a window is already open");

if (curWindow == 1)

JAbout.requestFocus();

}

break;

case QUIT:

output.setText("Good Bye!");

break;

default:

String kCode = Integer.toString(e.getKeyCode());

output.setText(kCode);

break;

}

}

public void keyReleased(KeyEvent e) {

switch(e.getKeyCode()) {

case FORWARD:

case BACKWARD:

case LEFT:

case RIGHT:

case SPACE:

case SETTINGS:

case SHIFT:

case ABOUT:

break;

case REPAINT:

output.setText("process: REPAINTED");

break;

case QUIT:

System.exit(0);

break;

default:

output.setText("process: UNKNOWN command - " + e.getKeyChar());

break;

}

}

public void keyTyped(KeyEvent arg0) {

}

}

[/left][/left]

Share this post


Link to post
Share on other sites

could you fix your post? The color coding messed up the code and it is not all showing. It looks like either you still have a reference in your code tot he old class name or either the editor you are using is still trying to link it. What are you using for an IDE? Eclipse?

Share this post


Link to post
Share on other sites
Eclipse
Have you tried cleaning the project after you changed the name? Project -> Clean...My experience (I also use Eclipse) with changing project names is that it is almost certain to cause problems. I might not be doing it the right way, but I'd much rather create a new project, copy the files over, and then delete the old project.

Share this post


Link to post
Share on other sites

Good point, now the ImageIcon that I was using is gone too.

Share this post


Link to post
Share on other sites

No, cleaning the project didn't do anything that I noticed.

Share this post


Link to post
Share on other sites

Well, I've got it working now!PS: is their a way to make my own properties? So I could do something like: cheese.startCutting(1.00); Or something?

Share this post


Link to post
Share on other sites

yes you can create your own classes and define the methods and properties you want. Also you can extend an existing class to include new protperties you want.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...