1

My question is rather simple.

Let's suppose I'm executing the algorithm "A star" (search algorithm using a heuristic function to calculate next state to visit).

I want to show in a grid the updates (I will apply it to 8-puzzle problem). How should I do that? I want changes to be clearly visible.. but in my experience if I just do something like Grid[6].showValue(newValue) the GUI will just "stand-by".

I'm sure this could be done with multi-threading (maybe?) but is there any simpler way?

And one more very easy question if possible: I wonder if in Java (my IDE is Netbeans) there is any class containing methods for searching like BFS, DFS and A star? If so, could you provide a link to the code of the algorithms (I need to use them as a base for my code.. I can't include them directly.. you know.. university assignment). I suppose this code is easy to find since Java is a open-source language. Am I wrong?

Thank you very much

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
dragonmnl
  • 14,578
  • 33
  • 84
  • 129
  • Also, if you are required to implement A*, you should have at least a pseudo-code in your couse handout/coursebook. Try to implement the algorithm based on those before you want to look at already existing implementations. If you are stuck in your own implementation, provide details on how far you got and what the specific problem you are trying to solve (besides the display not refreshing) – Attila Mar 26 '12 at 18:41

2 Answers2

3

Don't do the processing in the GUI thread.

If we're talking about here, that's the Event Dispatch Thread; use worker threads as described in the Concurrency in Swing tutorial.

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • Thank you. Just one question: "don't do the processing in the GUI thread". What do you mean? Do you mean to use a "different thread" for operation (like search algorithm) and use the GUI thread (for example start class) just to show output and get input? – dragonmnl Mar 26 '12 at 19:30
  • Exactly. Look into [`SwingWorker`](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html). – Matt Ball Mar 26 '12 at 20:05
1

You should do the processing in a separate thread, like MДΓΓ БДLL suggested. Basically, you'll have to implement your search related code in a class that implements Runnable, which "marks" a class to be executable within a thread.

To do so, you can use a SwingWorker:

SwingWorker<Integer[], Void> worker = new SwingWorker<Integer[], Void>() {
    public Integer[] doInBackground() {
        //do the computation here. This will be executed in a different thread; 
        //thus allowing the event dispatch thread (=GUI thread) to ensure responsiveness of the UI.
        //NEVER update your GUI here since this could cause strange errors that are sometimes hard to track down.
    }
    public void done() {
        try {
            Integer[] result = get(); //this is executed in the GUI thread after 
            //execution if the doInBackground method finished and fetches the result of 
            //that method. You should update your GUI here.
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        } catch (ExecutionException ex) {
            ex.printStackTrace();
        }
   }
}

For your second answer: It's rather hard to implement an algorithm in such a generic way that it's usable for different data types, especially since the tree you are using BFS, DFS and A-Star may contain any kind of data. I think you should find the algorithms in pseudocode in a textbook or lecture nodes; if not, look it up somewhere and try to implement it yourself.

mort
  • 12,988
  • 14
  • 52
  • 97
  • Thank you. Just one more question: have you any suggestion how to "slowdown" the search algorithm? I mean.. I want the GUI is not updated to fast (the user can see every configuration of 8-puzzle for at least 200ms). Any idea? – dragonmnl Mar 27 '12 at 07:53
  • You could use Thread.sleep(200) - it's a static method, just call it in the thread you want to slow down. This way, the executing thread will stop execution for 200ms. If my answer helped you, please upvote/accept it! – mort Mar 27 '12 at 10:08
  • I had to accept just one answer (I tried to accept both) and I selected the first one. But of course I voted your. Thank you! – dragonmnl Mar 28 '12 at 14:55