Tuesday 4 December 2007

Comand Line Process Bar for Java

As we all know python can print without change line. Now the following codes shows how to do it in Java.

###############################################
"""sc.py
This is a simple class to demonstrate the use of jythonc.
"""

import java.lang.String

class sc(java.lang.Object):
def __init__(self):
"""public sc() # constructor is not forced to add'@sig'
"""

def tprint(self,c='hello'):
"""@sig public void tprint(String c)
"""
print c,

if __name__ == '__main__':
sc1 = sc()
sc1.tprint('zhengxin')


################################################
// Simpleclasstest.java

import java.lang.*;

public class Simpleclasstest {
public static void main(String[] args) {
sc sc1 = new sc();
for (int i = 1;i<100; i++)
{ try { Thread.sleep(200); } catch (InterruptedException e) {};
sc1.tprint("--");
}
System.out.println('\n');
}
}
#########################################
What to do now:
$jythonc sc.py # to generate java class.
$javac -cp /usr/share/jython/jython.jar:./jpywork Simpleclasstest.java # using Simpleclasstest.java to call the jython generated class.
$java -cp /usr/share/jython/jython.jar:.:./jpywork Simpleclasstest # run it.
=========================================================
Here is the way to achieve it in pure Java. and this java Class can be called by matlab to show process bar.
#########################################
//tryoutput.java
public class tryoutput {
String s;
public void setStr(String s)
{ this.s = s; }
public void tPrint()
{ try { Thread.sleep(200); } catch (InterruptedException e) {};
System.out.print(this.s);
}

public static void main(String[] args) {
// TODO Auto-generated method stub
tryoutput op = new tryoutput();
op.setStr("=");
for ( int i=0; i<100; i++) {
op.tPrint();
} } }
#####################################

Comments:
Here for both java and python, the loop can be put in the tprint methods. Same effect.

No comments:

My photo
London, United Kingdom
twitter.com/zhengxin

Facebook & Twitter