swing - Java Reflection Problem -


hi doing final year project; need develop algorithm visualization tool. need cater user-defined algo; animate algorithm user types in text-editor provided in tool.

i using java compiler api compile code user has typed , saved. tool offers set of classes user can use in his/her algo.

for example:

myarray(this class provided tool)

import java.awt.*; import java.util.logging.level; import java.util.logging.logger; import javax.accessibility.accessiblecontext; import javax.swing.*; public class myarray extends jcomponent {  int size = 0; int count = 0; int[]hold; thread th;    public myarray(int[]arr)//pass user array parameter {     //th = new thread();      size=arr.length;     hold = arr;//make copy of array use later in swap operation  }  public int length() {         return hold.length; }   public void setaccessiblecontext(accessiblecontext accessiblecontext) {     this.accessiblecontext = accessiblecontext; }   public void paintcomponent(graphics g) {     super.paintcomponent(g);             graphics2d g2d = (graphics2d) g;             this.setpreferredsize(new dimension(360,100));     for(int i=1; i<=size; i++)     {         g2d.drawrect((i*30), 30, 30, 50);     }      for(int i=1; i<=size; i++)     {         g2d.drawstring(integer.tostring(hold[i-1]), (i*30)+15, 30+25);     } }       public void set(int i, int j)//position of 2 elements swap in array {     try {         th.sleep(2000);//sleep before swapping because else user won't see original array since swap , sleep     } catch (interruptedexception e) {         // todo auto-generated catch block         e.printstacktrace();     }     int temp = hold[i];     hold[i] = hold[j];     hold[j] = temp;             hold[i]=j;     this.repaint();//can use eapint class extends jpanel }      public void swap(int i, int j)//position of 2 elements swap in array {     try {         th.sleep(2000);//sleep before swapping because else user won't see original array since swap , sleep     } catch (interruptedexception e) {         // todo auto-generated catch block         e.printstacktrace();     }     int temp = hold[i];     hold[i] = hold[j];     hold[j] = temp;             this.repaint();//can use eapint class extends jpanel }   public int get(int pos) {     return hold[pos];  } 

}

this portion of gui cause compilation:

    javacompiler jc = null;     standardjavafilemanager sjfm = null;     file javafile = null;     string[] options = null;     file outputdir = null;     url[] urls = null;     urlclassloader ucl = null;     class clazz = null;     method method = null;     object object = null;           try         {          jc = toolprovider.getsystemjavacompiler();          sjfm = jc.getstandardfilemanager(null, null, null);          file[] files = new file[1];          //files[0] = new file("c:/users/user/documents/netbeansprojects/my_final_year_project/myarray.java");          //files[1] = new file("c:/users/user/documents/netbeansprojects/my_final_year_project/tool.java");          files[0] = new file("c:/users/user/documents/netbeansprojects/my_final_year_project/userdefined.java");         // getjavafileobjects’ param vararg         iterable fileobjects = sjfm.getjavafileobjects(files);         jc.gettask(null, sjfm, null, null, null, fileobjects).call();         // add more compilation tasks         sjfm.close();         options = new string[]{"-d", "c:/users/user/documents/netbeansprojects/my_final_year_project"};         jc.gettask(null, sjfm, null, arrays.aslist(options), null, fileobjects).call();          outputdir = new file("c:/users/user/documents/netbeansprojects/my_final_year_project");         urls = new url[]{outputdir.tourl()};         ucl = new urlclassloader(urls);         clazz = ucl.loadclass("userdefined");           method = clazz.getmethod("user", null);         object = clazz.newinstance();         object ob = method.invoke(object, null);           } 

this example of user-defined algo(userdefined.java):

import java.awt.*; import javax.swing.*;   public class userdefined { public void user() {     int [] numarr  = {1,3,1,-1,5,-5,0,7,12,-36};     myarray myarray = new myarray(numarr);      jframe frame = new jframe("rectangles");      frame.setdefaultcloseoperation(jframe.dispose_on_close);     frame.setsize(360, 300);     frame.setlocationrelativeto(null);     frame.setvisible(true);         frame.add(myarray);      (int i=myarray.length(); i>1; i--)     {         (int j=0; j<i-1; j++)         {             if (myarray.get(j) > myarray.get(j+1))             {                 myarray.swap(j, j+1);             }         }     }  } 

}

the problem getting if try use reflection above; white window not show animation) displays result @ end.

however if use instead of reflection(and change method void user() static void main(string args) in userdefined.java):

        javacompiler compiler = toolprovider.getsystemjavacompiler();         if(compiler.run(null, null, null, "userdefined.java") != 0) {         system.err.println("could not compile.");         system.exit(0);         }         try {         runtime rt = runtime.getruntime();         process pr = rt.exec("java "+"userdefined");         bufferedreader input = new bufferedreader(new              inputstreamreader(pr.getinputstream()));         string line=null;         while((line=input.readline()) != null) {         system.out.println(line);         }         } catch(exception e) {         system.out.println(e.tostring());         e.printstacktrace(); 

it woks provided after first compilation place myarray class in same folder userdefined.java. in case can see animation take place correctly.

how use reflection invoke main method instead of using instance of class. please need this. thanks!

you violating / missusing first rule of swing: acces swing components in edt (event dispatch thread).

when start program using main method, violating rule. happens work, might have kinds of weird effects. not theoretic warning, happend me , not nice.

when run using reflection code, in edt, algorithm runs before gui gets updated again (which happens on edt). thats why see final result of algorithm.

the correct way be: run algorithm in seperate thread , make sure changes myarray component happen in edt, using swingutilities.invokeandwait or swingutilities.invokelater


Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -