Wednesday, 15 August 2012

java - Traverse tree based on timer -


I use the traversal of a tree (BFS and DFS) in a JPNL timer and Paint Component ... similarly ...

Enter image details here </ P> <p> Now the BFS algorithm is instantly spun into Cyan by the eyes of all the nodes and paints ... but I would like to see how the tree is transposed ... node by the node .. So when I next <code> w Hile loop </ code> is running, trying to add a timer ... it is not working at all ... </ p> <P> <strong> Timer: </ strong> </ P> <pre> <code> Public Zero Timer () {int initialDelay = 1000; Timer.scheduleAtFixedRate (Public Timer Task) {If (cancel timer) {timer.cancel ();} If (counter == 3) {// reset counter = 0;} If (counter & lt; 3) {++ counter; System.out.printLN (counter);}}}, initial della, 1000); </ P> <p> <code> </ pre> <p> <strong> Paint communants: they are traced in the form of nodes </ strong> </ p> <pre> <code> Public Zero Paint Commons (Graphics G) {g.setColor (Art Color); G.fillRect (0, 0, width, height); G.setColor (rootNode.getColor ()); G.fillRect (rootNode.getX (), rootNode.getY (), rootNode.getWidth (), rootNode.getHeight ()); G.setColor (Color.WHITE); G.drawString (rootNode.getValue (), RouteNode.EXEx (+9), RoutodeGetting () + 16); Paint communion (G, root node); } Public Zero Paint Component (Graphics G, Nodes Pern-node) {// Preparation of new nodeprint list for loading with new children ArrayList & lt; Nodes & gt; Node print = new array of & lt; Nodes & gt; (); // Base case: End of the node list if (node ​​list.exex (parentlon) == node list size () - 1) {System.out.println (

BFS ():

  Public Zero BFS () {QQ = New Linkedist (); Q.add (rootNode); RootNode.visited (true); RootNode.setColor (Color.cyan); PrintNode (rootNode); // only when counter = 10 checks; While (! Q.isEmpty ()) {nodes n = (nodes) q.remove (); Nodes child = null; // while putting all unvisited children in the queue ((child = getUnvisitedChildNode (n)) = tap) {if (counter == 3) {child.visited (true); PrintNode (child); Q.add (child); Child.setColor (Color.cyan); }}} If (q.isEmpty ()) {Cancel timer = true; //RepaintManagerCurrentManager(this).markCompletelyClean(this); }}   

Any thoughts? Thanks!

For example, you can type queue < Nodes & gt; which will accept nodes for painting. That is, in your bfs () method, where you set the color child.setColor (Color.cyan); Add this node to a cue . Therefore:

  if (counter == 3) {child.visited (true); PrintNode (child); Q.add (child); PaintQueue.add (child); }   

and in the timer, at fixed delays, poll this line and node color:

  timer.scheduleAtFixedRate (new timer Task () {Public Zero Run (if (PaintQQ.ISEplay ()) {nodes node = PentUQUP (.); Node.Setterang (color.);}}}, Initial del, 1000);    

No comments:

Post a Comment