2010/05/26

Terminate a running Matlab program from Java

Now the Train model in DynSolve is almost ready, except for the part of how to present the results. Mr Raid Karoumi is working on it. But I don't think it's possible to show DynSolve to Swedish Road Administration, since there's only one day but I haven't gotten the Matlab code for plotting results from Raid.

Communicating with a running Matlab program in Java makes me headache! A new break in this software design is that one could terminate a running Matlab program during calculation in Java. It's archived  by making a dialog window in Matlab. This method seems simple but it is the only way that I could come up with by far(since the design of PASS). 

For most of those who knows Matlab a bit, when it comes to terminating a running Matlab program, they may come up with "Ctrl+c". Unfortunately, this is totally meaningless in a Java program who calls the Matlab JAR generated by MATLAB Builder™ JA, because Java can never touch the keyboard for the uses. The bottleneck is that, Maltab code is unreachable for the users in such Java program. But there is way: a GUI window created in Matlab. It is like a bridge that connects the users directly to MCR, instead of giving the message to the Java boat man under the bridge who sends the message to MCR.

To control the Matlab program, you need to declare some global variables in Matlab code which are reachable by both the main Matlab code and the Matlab GUI(e.g, dialog window). When you want to terminate the Matlab program, change the variables via Matlab GUI, when the main Matlab program see the variables' change, it will 'return'. Do not use error command instead of 'return', cause 'error' doesn't work sometimes when it's working in Java.

One important thing: try to use 'pause' in the loop in Matlab code to check if the variables are changed. Because the Matlab GUI thread needs time to execute.

 


2010/04/02

Add splash screen in Java desktop application

I failed to add the splash screen to my Java desktop application project in Netbeans. But fortunately I have exe4j at hand!! There's a step called "Splash screen" in exe4j when converting a jar to .exe file, where one could specify the location of his splash screen picture.

Then run the converted .exe file and enjoy the splash screen of your own!

2010/03/30

Re: How to activate the busy animation in the status bar

While programing in Dynsolve I try to bother the busy animation and progress bar in the status bar in Netbeans IDE. I found a remarkable solution which I've not dared to finish reading on:

http://old.nabble.com/How-to-activate-the-busy-animation-in-the-status-bar--td19435912.html 

A real ambitious German geek answered the question "How to activate the busy animation in the status bar". It goes so deep that he could suggest SUN to include the answer into its on-line help document.

Yesterday I came out a simple version of the solution in which one does not need to interfere with the org.jdesktop.application.Task class. I do have some task such as 'plot nodes' that required to run in the background but they're not so time consuming. It doesn't worth extending the Task class and overriding those method for each short life task.

One could find an easy way to  activate the busy animation in the constructor of the xxxView. The default code under the comment

// connecting action tasks to status bar via TaskMonitor 

tells you how to active it:



if (!busyIconTimer.isRunning()) {
statusAnimationLabel.setIcon(busyIcons[0]);
busyIconIndex = 0;
busyIconTimer.start();
}
progressBar.setVisible(true);



You could add this code in you own thread.

2010/01/26

run Matlab in JAVA successful with Matlab 2009b

succeed in Matlab 2009b. Environment variables:
CLASSPATH:
.;E:\JAVA\jre6\lib\ext\QTJava.zip
JRE_HOME:
E:\JAVA\jre6\
MATLAB_JAVA
E:\JAVA\jre6
PATH
E:\program_files\miktex\miktex\bin;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;E:\JAVA\jdk\bin;E:\program_files\3.2.2_gcc-4.3.0\bin;C:\Program Files\QuickTime\QTSystem\;F:\shortcuts;C:\Program Files\Common Files\Thunder Network\KanKan\Codecs;E:\program_files\matlab\bin;E:\program_files\matlab\runtime\win32

online icon generater

Generate your icon on 


http://www.genfavicon.com/

2010/01/25

Add item to a panel dynamically

   This is a panel from my 'Dynsolve' project which enables the user to add item to a panel dynamically.

One could add a node group to the main panel(Text Pane) by clicking "add group" button.

The class definition of the node group panel(JPanel) and the action listeners of "Add Group" button, "Remove" button are shown as following:



Class NodesGroupPanel:

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
* Displays and generates nodes.
*
* @author chenyang
*/
public class NodesGroupPanel extends JPanel {
private int newWidth = 512;
private int newHeight = 27;
private JLabel groupNrBt;
private JTextField node1Txt, node2Txt, stepTxt, x1Txt, x2Txt, y1Txt, y2Txt;

public NodesGroupPanel(int groupNr) {
super();
if (groupNr<10){ groupnrbt =" new">=10&&groupNr<100){ groupnrbt =" new">=100&&groupNr<1000){ groupnrbt =" new" groupnrbt =" new" node1txt =" new" node2txt =" new" steptxt =" new" x1txt =" new" x2txt =" new" y1txt =" new" y2txt =" new">


jButton5ActionPerformed of "Add Group" button:


private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
jPanel6.setText(jPanel6.getText() + "\n\n");
NodesGroupPanel ngp = new NodesGroupPanel(nodesManager.getGroupCount() + 1);
jPanel6.add(ngp);
nodesManager.addToGroup(ngp);

// repaint
jPanel6.validate();
jPanel6.repaint();
// add item to jComboBox1 in remove panel
jComboBox1.addItem(nodesManager.getGroupCount());
}

jButton4ActionPerformed of "Add Group" button:

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
if (nodesManager.getGroupCount() > 0) {
removeNodeGroupDialog.setVisible(true);
}

}

%%%%%%%%%%%%%%

where removeNodeGroupDialog is shown as:

  And the "Remove" button on this Dialog acts as:






private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
NodesGroupPanel ngp = nodesManager.removeNodesGroup(jComboBox1.getSelectedIndex() + 1);
if (ngp != null) {
jPanel6.remove(ngp);
jPanel6.validate();
jPanel6.repaint();
jComboBox1.removeItemAt(jComboBox1.getItemCount() - 1);
}

}