【问题标题】:Calling 2 classes from Java GUI within Matlab在 Matlab 中从 Java GUI 调用 2 个类
【发布时间】:2011-08-19 14:50:02
【问题描述】:

由于一旦使用 Java 就无法卸载本机库(在 Matlab 中;参见 SO question),我试图从 Matlab 中调用 2 个 GUI 类。我正在从相机中抓取图像,然后将其保存在磁盘上。我想使用一个 Java 类与相机通信,而另一个类 (GUI) 在 Matlab 中仍然打开。这可能吗?代码如下:

1.

 public class GUI
    {

    public static void main(String[] args)
    {
    // Just open up the window and start things running
    MainWindow mWindow = new MainWindow();
    }

    public static void main2()
    {
        MainWindow.grabImage(0);
    }
}

2。

public class MainWindow

{

static volatile int commandVal;
        Thread updateThread;
        static CameraImage cImage;
static int fs_c =1;
    MainWindow(){

    JFrame main_f = new JFrame("M");
        main_f.getContentPane().setLayout(new BorderLayout());
        main_f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        main_f.getContentPane().add(tabPane1, "Center");

        main_f.pack();
        main_f.setVisible(true);
                kkk = 1;

        mySerial = new CameraSerial(cWindow.getPort());
}
}

3.

public static void grabImage(int commandVal){

                   while (MainWindow.kkk == 1) {
                       if (MainWindow.fs_c == 1) {
                        MainWindow.commandVal = 5;
            }
            if (MainWindow.commandVal == 5 || MainWindow.commandVal == 6){

                cImage.sendFrame(0);
                                JFileChooser save_d = new JFileChooser();
                                File saveFile = save_d.getSelectedFile();
                cImage.writeImage(saveFile + ".jpg");

                                MainWindow.fs_c = 0;
                                MainWindow.commandVal = 0;
                                mySerial.write("\r");
                                System.out.println("Camera Ready...");

break;

                        }

                else if (commandVal == -1) {
                MainWindow.commandVal = 0;
                        mySerial.write("\r");
                                status_t.setText("Camera Ready...");
            }
                       else {
                try {
                    Thread.sleep(100);

                } catch (Exception e) {
                }
            }
        }

}

在 Matlab 中,我首先调用 Gui.main([]),然后调用 Gui.main2()。它是第一次工作。但是当我再次调用Gui.main2() 时,Matlab 什么也不做。我认为这是一个糟糕的代码。感谢回复!

【问题讨论】:

  • 您是否将其放入调试器并检查它停止的位置?
  • 您能否重新格式化代码以提高可读性并添加缺失的部分?比如MainWindow.fs_c在哪里定义?

标签: java user-interface matlab camera


【解决方案1】:

只是一个假设,没有更多信息:

这个检查:

if (MainWindow.fs_c == 1) {
  MainWindow.commandVal = 5;
}

稍后(请注意,第一次检查成功后commandVal 将是 5):

MainWindow.fs_c = 0;
MainWindow.commandVal = 0;

问题来了:

在第一次运行中,commandVal 设置为 5,假设 MainWindow.fs_c 最初为 1。 这样MainWindow.fs_c = 0;就被执行了。

在第二次运行中,MainWindow.fs_c == 1 为 false(MainWindow.fs_c 现在为 0),MainWindow.commandVal 也被设置为 0。因此该方法除了休眠 100 毫秒之外什么都不做。

【讨论】:

  • 非常感谢!我做了显而易见的事情,删除了你建议的两条令人困惑的行,它奏效了!!!
  • @Makaroni 好吧,我不知道您要实现的逻辑。一个快速的解决方案是省略MainWindow.fs_c = 0; 行,但我想这可能会破坏其他逻辑。所以不幸的是,除了调试和检查您尝试实现的逻辑之外,我无法给您任何其他好的建议。
猜你喜欢
  • 1970-01-01
  • 2015-06-29
  • 1970-01-01
  • 2014-05-21
  • 1970-01-01
  • 1970-01-01
  • 2017-05-15
  • 2011-10-22
  • 1970-01-01
相关资源
最近更新 更多