GUI Question/Problem

This is where you talk about the NXJ software itself, installation issues, and programming talk.

Moderators: 99jonathan, roger, imaqine

GUI Question/Problem

Postby BHCluster » Thu Mar 13, 2008 2:44 pm

I am a new at this and have a little problem I just cannot make this program work. The problem is I cannot connect my GUI to Bechambot on page 239-240. So if any body has any suggestions please help.

Gui.java

Code: Select all
public class Gui extends javax.swing.JPanel {
   private static javax.swing.JTable jTable;
   private javax.swing.JScrollPane jScrollPane;
   private javax.swing.JPanel jPanel0;
   private javax.swing.JPanel jPanel1;
   private javax.swing.JPanel jPanel2;
   private javax.swing.JPanel jPanel3;
   private javax.swing.JPanel jPanel4;
   private javax.swing.JPanel jPanel5;
   private javax.swing.JPanel jPanel6;
   private javax.swing.JPanel jPanel7;
   private javax.swing.JTextField jTextField1;
   private javax.swing.JButton jButton1;
   private javax.swing.JButton jButton2;
   private javax.swing.JButton jButton3;
   private javax.swing.JButton jButton4;


   public Gui() {

      //Beckhambot bot new Beckhambot();

      jPanel0 = new javax.swing.JPanel(new java.awt.BorderLayout());
      jPanel1 = new javax.swing.JPanel(new java.awt.BorderLayout());

      jTextField1 = new javax.swing.JTextField(50);

      jButton1 = new javax.swing.JButton("Up");
      jButton2 = new javax.swing.JButton("Down");
      jButton3 = new javax.swing.JButton("Left");
      jButton4 = new javax.swing.JButton("Right");

      jPanel2 = new javax.swing.JPanel(new java.awt.BorderLayout());

      jPanel3 = new javax.swing.JPanel(new java.awt.BorderLayout());
      jPanel4 = new javax.swing.JPanel(new java.awt.BorderLayout());
      jPanel5 = new javax.swing.JPanel(new java.awt.BorderLayout());
      jPanel6 = new javax.swing.JPanel(new java.awt.BorderLayout());

      jPanel7 = new javax.swing.JPanel(new java.awt.BorderLayout());

      jPanel3.add(jButton1);
      jPanel4.add(jButton2);
      jPanel5.add(jButton3);
      jPanel6.add(jButton4);

      jPanel7.add(jTextField1);

      jButton1.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent e) {
               //bot.keyPressed(key.getKeyCode());
         }
      });

      jButton2.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent e) {
            //bot.keyPressed(key.getKeyCode());

         }
      });

      jButton3.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent e) {
            //bot.keyPressed(key.getKeyCode());
         }
      });

      jButton4.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent e) {
            //bot.keyPressed(key.getKeyCode());
         }
      });

      jPanel2.add(jPanel3, java.awt.BorderLayout.NORTH);
      jPanel2.add(jPanel4, java.awt.BorderLayout.SOUTH);
      jPanel2.add(jPanel5, java.awt.BorderLayout.WEST);
      jPanel2.add(jPanel6, java.awt.BorderLayout.EAST);
      jPanel7.add(jTextField1, java.awt.BorderLayout.CENTER);

      jPanel0.add(jPanel7, java.awt.BorderLayout.SOUTH);
      jPanel1.add(jPanel2, java.awt.BorderLayout.NORTH);

      jPanel0.setPreferredSize(new java.awt.Dimension(200, 200));
      jPanel1.setPreferredSize(new java.awt.Dimension(200, 200));
      jPanel3.setPreferredSize(new java.awt.Dimension(20, 20));
      jPanel4.setPreferredSize(new java.awt.Dimension(20, 20));
      jPanel5.setPreferredSize(new java.awt.Dimension(75, 20));
      jPanel6.setPreferredSize(new java.awt.Dimension(75, 20));
      jPanel7.setPreferredSize(new java.awt.Dimension(200, 200));
      add(jPanel0);
      add(jPanel1);
   }

   //public static void addEntry(MainGui mainGui) {

   //}


   //public static void editEntry(MainGui mainGui) {

   //}

   //public void removeEntry() {

   //}

}


I have edited this code a bit
Beckhambot.java

Code: Select all
import icommand.nxt.*;
import icommand.navigation.*;
import icommand.nxt.comm.*;

import java.awt.event.*;

public class Beckhambot implements KeyListener {

   Pilot kicker = new Pilot(5.6F, 16.5F, Motor.B, Motor.C);

   final int FORWARD = 87, // W = forward
   BACKWARD = 83, // S = backward
   LEFT = 65, // A = left turn
   RIGHT = 68, // D = right
   QUIT = 81; // Q

   public Beckhambot() {


      NXTCommand.open();
      NXTCommand.setVerify(true);
      System.out.println("BT Opened");
      Motor.A.setSpeed(900);
      kicker.setSpeed(500);
   }

   public void keyPressed(KeyEvent key) {
      switch(key.getKeyCode()) {
      case FORWARD:
         kicker.forward();
         break;
      case BACKWARD:
         kicker.backward();
         break;
      case LEFT:
         kicker.rotate(100000, true);
         break;
      case RIGHT:
         kicker.rotate(-100000, true);
         break;
      case QUIT:
         NXTCommand.close();
         System.exit(0);
         break;
      }
   }

   public void keyReleased(KeyEvent arg0) {
      kicker.stop();
   }

   public void keyTyped(KeyEvent arg0) {}

   //public static void main(String [] args) {
      //Beckhambot b = new Beckhambot();
      //Vision.getFrame().addKeyListener(b);
   //}
}


main.java
Code: Select all
import java.util.Scanner;

public class main{

   public main(){

      Gui gui = new Gui();

            javax.swing.JFrame.setDefaultLookAndFeelDecorated(true);

            javax.swing.JFrame jFrame = new javax.swing.JFrame("Main Gui");

            jFrame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);

            jFrame.setResizable(true);
            jFrame.setPreferredSize(new java.awt.Dimension(800, 300));
            jFrame.getContentPane().add(new Gui());

            jFrame.pack();
            jFrame.setVisible(true);

      //Beckhambot b = new Beckhambot();

     }

    public static void main (String[] args){
      new main();
    }
}


Please hep.
User avatar
BHCluster
New User
 
Posts: 23
Joined: Thu Mar 13, 2008 2:36 pm

Postby gloomyandy » Thu Mar 13, 2008 11:19 pm

Hi,
It would probably help if you told us what you are expecting the code to do and then what actually happens...

Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 4072
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Postby BHCluster » Fri Mar 14, 2008 9:00 pm

Sorry I am trying to move/control NXT with those four buttons up, down, left, right trough GUI. Thanks.
User avatar
BHCluster
New User
 
Posts: 23
Joined: Thu Mar 13, 2008 2:36 pm

Postby gloomyandy » Fri Mar 14, 2008 9:32 pm

Ok so what happens....
1. When you start the program...
2. When you press one of the buttons...

Do you see any of the system.out.print output? Can you add some more to see if pressing the buttons is actually triggering the code you want it to?

Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 4072
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Postby BHCluster » Fri Mar 14, 2008 10:54 pm

That is the thing I don't know how to connect or what to put in button listener from beckhambot class. That is my key problem any pointers are greatly appreciated.
User avatar
BHCluster
New User
 
Posts: 23
Joined: Thu Mar 13, 2008 2:36 pm

Postby BHCluster » Sat Mar 15, 2008 9:04 pm

What I mean is what should I put in the jButton1 action listener from the bechambot class in order to control it trough
User avatar
BHCluster
New User
 
Posts: 23
Joined: Thu Mar 13, 2008 2:36 pm

Postby lawrie » Sat Mar 15, 2008 9:18 pm

The way you have things and the moment, uncommenting the creation of bot in Gui.java, and doing bot.keyPressed('W') etc. should work, but you are better off reprogramming the Beckambot class not to use keys.
lawrie
leJOS Team Member
 
Posts: 922
Joined: Mon Feb 05, 2007 1:27 pm


Return to NXJ Software

Who is online

Users browsing this forum: No registered users and 4 guests

more stuff