java - JPanel paintComponent in a Thread -


i found nice code on web drawing binary tree. created binary search tree algorithm it, if pass root node function, draws out tree. want make draw step step, running in thread, , making sleep after inserting element. problem is, guess, jpanel won't returned until drawing complete, if runs on different thread, won't added split pane ( have many sorting algorithms implemented, , being painted, , can accessed same tabbed pane. every tabbed pane contains split pane , every split pane contains controlling buttons, , jpanel, draw algorithms, others not extend jpanel! ). please me find way run in totally different thread main program, , able see how draws tree?

please not judge me on code, put fast, trying make work, not way code.

public class binarytree extends jpanel implements runnable {  private int radius = 20; private int vgap = 45, hgap = 150; private int x = 350, y = 25; private graphnode root; private binarysearchtreesort binarytreesort; private int latency = 270; private boolean ismanual = false;  //stores sorting algorithm's instance public binarytree(binarysearchtreesort binarytreesort) {     this.binarytreesort = binarytreesort; }  @override public void run() { //maybe should put here paintcomponent somehow???     root = binarytreesort.binarytreesort(); }  @override public void paintcomponent(final graphics g) {     displaytree(g, root, x, y, hgap); }  private void displaytree(graphics g, graphnode root, int x, int y, int hgap) {     // display root       while (!isstarted) {         try {             thread.sleep(100);         } catch (interruptedexception ex) {             logger.getlogger(binarytreeframe.class.getname()).log(level.severe, null, ex);         }     }     g.drawoval(x - radius, y - radius, 2 * radius, 2 * radius);      if (root.getvalue() != 0) {         g.drawstring(root.getvalue() + "", x - 13, y + 4);      }      sleep();      if (root.getleftchild() != null) {         // draw line left node           g.setcolor(color.black);         leftchild(g, x - hgap, y + vgap, x, y);         //draw left subtree            displaytree(g, root.getleftchild(), x - hgap, y + vgap, hgap / 2);     }      if (root.getrightchild() != null) {         // draw line right node           g.setcolor(color.black);         rightchild(g, x + hgap, y + vgap, x, y);         // draw right subtree recursively           displaytree(g, root.getrightchild(), x + hgap, y + vgap, hgap / 2);     }  }  /* draw left child */ private void leftchild(graphics g, int x1, int y1, int x2, int y2) {     double d = math.sqrt(vgap * vgap + (x2 - x1) * (x2 - x1));     int x11 = (int) (x1 + radius * (x2 - x1) / d);     int y11 = (int) (y1 - radius * vgap / d);     int x21 = (int) (x2 - radius * (x2 - x1) / d);     int y21 = (int) (y2 + radius * vgap / d);     g.drawline(x11, y11, x21, y21);      //g.drawstring("0", ((x21 + x11) - 20) / 2, (y21 + y11) / 2); }  /*draw right child */ private void rightchild(graphics g, int x1, int y1, int x2, int y2) {     double d = math.sqrt(vgap * vgap + (x2 - x1) * (x2 - x1));     int x11 = (int) (x1 - radius * (x1 - x2) / d);     int y11 = (int) (y1 - radius * vgap / d);     int x21 = (int) (x2 + radius * (x1 - x2) / d);     int y21 = (int) (y2 + radius * vgap / d);     g.drawline(x11, y11, x21, y21);      //g.drawstring("1", (x21 + x11) / 2, (y21 + y11) / 2); }  public void sleep() {     if (!ismanual) {         try {             thread.sleep(latency);         } catch (interruptedexception ex) {             logger.getlogger(binarytree.class.getname()).log(level.severe, null, ex);         }     } } 


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -