Dec 1, 2016

A "Hello World" example with Visual Studio Code and Java

Yes, I just got my new 13" Macbook Pro (Oct 18, with Fn keys, of course). It has been more than 10 years since I replaced my iBook G4 with a Dell :( --Don't make me wrong, I love windows:)

It is a great time nowadays with cross-platform programming become easier and easier. I don't use VS code, or Visual Studio Code very often. I love its simplicity. For this reason, I install it on the Mac and begin to play with it. This time, in Java. Why Java?Well, it just happens like that.

So let's begin with a simple "Hello World" example.

Outline:
- Download VS Code;
- Install JDK (Java Development Kit);
- Test the example.
- Install a Java extension (optional);

(1) Download VS Code from here or google it. 

(2) Install JDK (Java Development Kit) from here(Oracle site).

(3)Test our example.

3-1 From the menu, select "Open..", select a folder where you want to put your file in;

3-2 "New file" to add a file, for example, "example.java" and copy the following code in it:

class hello{
    public static void main( String args[] ) {
    System.out.println("Hello World");
    }
}

 3-3 compile and debug with it.

-From the menu select "View->Integrated Terminal"
-compile the source file, here is "example.java", using "javac"
javac example.java
-run the built class file, here is "hello", using "java"
java  hello
-check the output

3-4 (optional) install a Java extension and how to find java-home path

I am not going to introduce the extension here. But if you need further functionality like auto-completion, you will need such extensions, just search for it. Just in case, you don't know how to set the settings for java configurations, especially for "java-home".

Select the "Code->Preference->User Settings", VS code will show a window with default settings, and new window for your to overwrite the default settings.
add a line call "java.home" (as you are typing, this should appear for you to choose), after that, input the path for your JDK, by default for my mac, it is:

    "java.home": "/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home"

In fact, the problem is, how to find this path for your system on a Mac? Here is a good reference on Stackoverflow. In short, in the Terminal, use 

ls -l 'which java'
and copy the one similar to the above.

Userful links:

VS Code with Java (Japanese): blogs.msdn.microsoft.com/ayatokura/2016/07/05/vscode_java/
Path for java-home: stackoverflow.com/questions/18144660/what-is-path-of-jdk-on-mac


Dec 29, 2014

How to Call Gnuplot from C# (WPF)

Two years ago, I've written a post on how to call gnuplot from C++. It turns out the most viewed post of my posts. If you're missing it, click here.

Now as I am developing an UI for my program, I begin to use C# now. And here the old problem comes back again: how to call gnuplot from C#.

Instead of using Foms of C#, I get used to using WPF. However, as we are talking about the C#, so it should work in both structures.

Here we go -- How to call gnuplot from C#.

I'll skip the basics of C# and assume that you all know how to start a project (here I use desktop WPF) or create a button click event.

1 At you design view, create a button click, all the coding we're going to do is just within this event. So it has the most flexibility for using elsewhere.







2  Preparation. Only two preparation we need: 


a, add " using System.IO;" and "using System.Diagnostics" at the top of the file (as you can see in the previous picture). In WPF, these are not added by default. So don't forget to add them before you procceed.


b. download and copy the gnuplot folder to the location where your setup would be. I recommend download the "gp466-win32.zip" and unzip it then you get a folder named "gnuplot". I put it under the debug folder as shown in this picture. You can put it any where as you like, later in the code we have to specify the location of it.


3 set up a new process and specify the path of gnuplot, using StreamWriter to communicate with gnuplot. I will give three examples here to show the usage.


3.1 Simple case: function plot:


 private void Button_Click(object sender, RoutedEventArgs e)
       {
           Process plotProcess = new Process();
           plotProcess.StartInfo.FileName = @"D:\Project\VisualGnuplot\VisualGnuplot\bin\Debug\gnuplot\bin\gnuplot.exe";
           plotProcess.StartInfo.UseShellExecute = false;
           plotProcess.StartInfo.RedirectStandardInput = true;        
           plotProcess.Start();
           StreamWriter sw = plotProcess.StandardInput;
           String strInputText = "plot sin(x)\n";
           sw.WriteLine(strInputText);
           sw.Flush();
           

      MessageBox.Show("Close the gnuplot Window? " );
      sw.Close();           
      plotProcess.Close();
}
Then debug it. if you click on the button, it should appear like this:


notice that if I added a MessageBox to keep the gnuplot window. If you omit it, there will be only a flash and you can't see anything. Another thing is that here the output terminal for gnuplot is the interactive mode, so it's very convenient for a single plot use. For example, in 3D plot you could change the view with mouse.

3.2 normal case: plot external data file.


here suppose that we have a "3d.txt" file whose content is like (don't forget to include # as comment and blank line for 3D plot in gnuplot! I put this data file under the Debug folder as well, you can see this from previous screenshot):
# X Y Z
0 1 2
0 2 4
0 3 8
0 4 16
0 5 32
1 1 5
1 2 3
1 3 5
1 4 3
1 5 5
2 1 32
2 2 16
2 3 8
2 4 4
2 5 2
3 1 3
3 2 9
3 3 27
3 4 81
I purposedly write only four elements in "3" while for "0" to "2" each of them has five element to indicate that you don't have to have the same shape in 3D line plot.

what we need to do is modify the code sent to gnuplot. Simply as it is:

private void Button_Click(object sender, RoutedEventArgs e)
       {
           Process plotProcess = new Process();
           plotProcess.StartInfo.FileName = @"D:\Project\VisualGnuplot\VisualGnuplot\bin\Debug\gnuplot\bin\gnuplot.exe";
           plotProcess.StartInfo.UseShellExecute = false;
           plotProcess.StartInfo.RedirectStandardInput = true;        
           plotProcess.Start();
           StreamWriter sw = plotProcess.StandardInput;
           //String strInputText = "plot sin(x)\n";
           String strInputText = "splot \"3d.txt\" using 1:2:3 with lines\n";
           sw.WriteLine(strInputText);
           sw.Flush();        
           MessageBox.Show("Close the gnuplot Window? " );
           sw.Close();
           plotProcess.Close();
                  } 

Now the result is:


I think now you have get a sense on how to use it now. Like in the C++ case, we sent all the commands to the gnuplot and let it work. But pratically, we need lots of settings to plot a graph we want, such as colors, labels and legends. Most of the case, we'd like the ability to derectly output the image itself. I think now you should know how to do it. So let's give a simple last example to show how to do it: plot an external data file and output the image.

3.3 plot an external data file and with external plot setting file

suppose we write another file "sp.plt" like this:

set terminal pngset output "plot.png"
splot "3d.txt" using 1:2:3 with lines
and now we read in these commands with StreamReader (not StringReader as I was cheated by the auto correction!)
       private void Button_Click(object sender, RoutedEventArgs e)
       {
           Process plotProcess = new Process();
           plotProcess.StartInfo.FileName = @"D:\Project\VisualGnuplot\VisualGnuplot\bin\Debug\gnuplot\bin\gnuplot.exe";
           plotProcess.StartInfo.UseShellExecute = false;
           plotProcess.StartInfo.RedirectStandardInput = true;        
           plotProcess.Start();
           StreamWriter sw = plotProcess.StandardInput;
           //String strInputText = "plot sin(x)\n";
           //String strInputText = "splot \"3d.txt\" using 1:2:3 with lines\n";
           String strInputText;
           StreamReader sr = new StreamReader("sp.plt");
           strInputText = sr.ReadToEnd();
           sr.Close();
           sw.WriteLine(strInputText);
           sw.Flush();        
          // MessageBox.Show("Close the gnuplot Window? " );
           sw.Close();
           plotProcess.Close();
           }

note that because we output the image, so we don't need the gnuplot window again. This will be handful when we have lots of files to plot. And there should be a png file generated now.
(a window flashed and disappeared)



Summary:


Now we could use the gnuplot from C# in a flexible way. Further thing you might be interested is as following: a. on the designer view design the setting pannel; b, write the setting parameters from the user and save it to a plot setting file; c, plot the data (ouput from code or external ones) to image; d, display the image as you want to the window.

PS. I'm quite busy with my work so I didn't check the comments very often. But you can still leave your comments there that might be helpful for other readers or I will reply to it when I see it.







Nov 17, 2014

C# tip 1: Open a new window with a button click

Finally, I decide to forget about the cross-platform thing and just want to finish my project as soon as possible. Because I mainly use Windows, I turn to C# for the GUI development.  I write my code in C++ with Visual Studio, so it may be easier for me to write C# under the same environment.

Now I'm thinking of building the GUI with C#, the main calculation will still be running in C++. The idea is C# just for the configuration and output of parameter list, which would be imported later in C++ code. All the front end and data processing are built with C#, only the developer touches on the C++  calculation part.

The first tip I eager to learn is how to generate a Window by click a button.
It it rather intuitive in C#.


  • Right click on the project name and select Add -> Window, give your new window a name, say, comEditorWindow.



  •  Drag a button object to the design viewer, name it.



  • In the Properties, click on "Event handler"(thundering mark).



  • At option "Click", input a name for you click, say, "buttonClick" then the Visual Studio would jump right into the method by which you write about what will happen when click the button.


One thing to note is that if you choose "Show( )" instead of "ShowDialog( )", then the user could click on the main window freely, so usually it's recommended to use "ShowDialog( )", or the user might have clicked the button twice and open two same windows. But sometimes both windows might need to be active at the same time, so choose the one that fits your need.


Mar 13, 2014

about XPM

This is the first time I come across a format called "xpm". Two things on it:

1 what is .xpm format?
X PixMap (XPM) is an image file format used by the X Window System, created in 1989 by Daniel Dardailler and Colas Nahaboo working at Bull Research Center at Sophia Antipolis, France, and later enhanced by Arnaud Le Hors. (from wiki)

2 how to convert general pictures to this format?
using xnconverter.

Feb 20, 2014

Using WxWidgets 3.0 in Microsoft Visual Studio 2012 or 2013

For the past year I've been caught in experiment, now would swing back to the simulation side. For some reasons, I want to develop a program with a easy-to-use UI. For present, I would use it for my study; for near future, it might be helpful for new comers in this field. After a brief searching on the Qt or WxWidgets, I simply pick up WxWidgets.

The problem is how to use it with Microsoft Visual Studio (2012 or 2013 at present). I went through a tough way in configuring FFTW before(basically it is using DLL in visual studio), so this time  I try to find a way as easy as possible that people like me who is not coder but have to code could follow.

And here it is.


  1. Install the  Microsoft Visual Studio 2013 (any edition works for you, or 2012), just click and go.

    2. Download and install the WxWidgets 3.0.0. (present it's Version 3.0.0 and windows installer edition is recommended). As recommended in the documentation, install it without space in the path name, so I simply use the default: "C:\wxWidgets-3.0.0".

    3.  Open the Advanced System Settings(see screenshot) and click on the "Environment Variables...".
















    4. Add a New item: Variable name: WXWIN, Variable value: C:\wxWidgets-3.0.0 (path of your wxwidgets);


     5.  Go to your wxWidgets folder (here, for example, C:\wxWidgets-3.0.0), open the "\build\msw\wx_vc10.sln" file with visual studio (just double click on it).  If prompted with some update of the files, just hit "OK". 

    6.  Build this solution (hit F7 or from menu) for "Debug" and "Release" separately(choose the build type as shown in the picture, each build process needs may be 1~2 min). (Note: If you need DLL, just build the DLL Debug and DLL Release). It should build without error and show something like"========== Build: 23 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========".


     7.   Now go to your wxWidgets folder and open "\samples\minimal\minimal.dsw" with visual studio (double click only). Again,  if prompted with some update of the files, just hit "OK". Then build it (hit F7, or F5 for debug mode) for debug or release to test it works OK or not. It should build without error pretty quickly and now you have a new folder called "vc_mswud" under "\samples\minimal\vc_mswud", where you would find a setup called "minimal.exe".
     8.  Double click it to see this setup.

     9. When you close the Visual Studio, it will ask you to save the change or not, just click Yes and save it to the same folder. As shown here.


     10.  Done. Now you can just develop your own code by modify this "minimal" example. But usually, you might want your project in other places, for example, I'd like my project in my Documents folder and there's too many files in present folder. We could copy only those we need to start a new project. After Step 7-9, you may notice that there are several new files generated in the "minimal" folder, among them: minimal.vcxproj, minimal.sln. What we need to copy are these two files and minimal.cpp(or you can write you own .cpp file). 

     11. Open the .vcxproj with notepad and replace ".\..\.." with  "C:\wxWidgets-3.0.0" (where you install your wxWidgets). In my case, I got 28 places replaced. 


     12.  then open the .vcxproj file with Visual Studio (double click only), and delete the sample.rc file (because we don't have it anymore)
     13.  Hit F5 for debug mode or F7 for build mode and you should see no problem with it. Now you can start you own project now. EXTRA: if you want to change the project name(for example, MyApp), please be sure that you do it this way:
a. change file name: minimal.vcxproj -> MyApp.vcxproj, minimal.sln ->MyApp.sln, minimal.cpp->MyApp.cpp
b. open .vcxproj file with notepad and replace all "minimal" with "MyApp"
c. open .sln file with notepad and replace all "minimal" with "MyApp"

Brief Summary:
With all these mentioned above, the idea is rather simple: After install the WxWidgets, we run the wx_vc10.sln to have it build the necessary library for us, then we can build the sample which is in the "workspace" of wxWidget. If we want to our own project on a different place(workspace), we have to configure the settings to connect to the library, which is saved in the vcxproj file. So we just simply change the path in it to avoid configuring from the scratch. 

The above method is derived from various resources, among which the most useful comes from wxWidgets's wiki page,which is little bit out-of-date though.