Help with Threads needed

Thread Solved

Join Date: May 2009
Posts: 11
Reputation: kolibrizas is an unknown quantity at this point 
Solved Threads: 1
kolibrizas's Avatar
kolibrizas kolibrizas is offline Offline
Newbie Poster

Help with Threads needed

 
0
  #1
20 Days Ago
Hello,
I am having a difficulty in passing a barrier of JAVA graphics generating. I hope you could explain some things to me, cause I have noone to help me and it is hard to get help in internet, because I feel like missing something important.

The thing is I cannot find an example of JAVA application which looks like similar to the one I am showing a picture to you.

Click image for larger version

Name:	application - how it looks.JPG
Views:	6
Size:	27.9 KB
ID:	12446

This is what I already managed to do and I was really happy about it. The problematic thing is, I am totally stuck with making it work in a beautiful way, which I want to be: being able to make a user of the program see step-by-step line drawing (once he clicks a button after placing points, the program should paint each line every half a second or so). That would be a demonstration of the algorithms used. Now the only thing I could do was just to painting all lines at once just after the button is clicked. I am trying to put a Thread.sleep(500) in a lines drawing loop, but it just ignores that and runs to the result. I also try to add that sleep in an algorithm and repaint() everything all the time, but it still skips all my efforts and I cannot do anything.

I put a component Graphic(my written class) called G in my Main class. That Graphic has paint() method which paints all the points and lines. On PW(stands for package wrapping) button clicked I get the result of Lines I need to draw. Here is how it looks:

  1. if (arg0.getSource() == pw)
  2. {
  3. PackageWrapping PW = new PackageWrapping(G);
  4. Vector<Line>BE = PW.lines(POINTS);
  5.  
  6. G.transfer(POINTS, BE);
  7. G.repaint();
  8. }


I do not have anyone to turn to, who could give me a working example, explain my mistakes or just give any good advice.

I really expect you could help me. If you need any deeper information related to my code, just let me know.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,560
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 196
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso
 
0
  #2
20 Days Ago
You could use a Timer to help you draw your lines at specific intervals. You could also use Thread.sleep(), as you mentioned - if that isn't working there must be something wrong in your implementation. Can you post all of your (relevant) code, such as your repaint() method, everywhere you call repaint(), and where you attempted to put Thread.sleep()? And have you looked at this?

http://java.sun.com/docs/books/tutor...ncy/sleep.html
Out.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 11
Reputation: kolibrizas is an unknown quantity at this point 
Solved Threads: 1
kolibrizas's Avatar
kolibrizas kolibrizas is offline Offline
Newbie Poster
 
0
  #3
19 Days Ago
Here is the code in PackageWrapping class:
  1. public void show(Vector<Point>POINTS)
  2. {
  3. Vector<Line>LINES = this.lines(POINTS);
  4. Vector<Line>PWLINES = new Vector<Line>();
  5. for (int i = 0; i < LINES.size(); i++)
  6. {
  7. PWLINES.add(LINES.get(i));
  8. this.G.transfer(POINTS, PWLINES);
  9. this.G.repaint();
  10. try
  11. {
  12. Thread.sleep(500);
  13. }
  14. catch (Exception e){}
  15. }
  16. }
And here is how it looks in Graphic class:
  1. public void paint(Graphics G)
  2. {
  3. G.setColor(Color.white);
  4. G.fillRect(0, 0, 400, 400);
  5.  
  6. G.setColor(Color.red);
  7. for(int i = 0; i < this.LINES.size(); i++)
  8. {
  9. Point A = this.LINES.get(i).A;
  10. Point B = this.LINES.get(i).B;
  11.  
  12. for (int j = 0; j < 5; j++)
  13. {
  14. G.drawLine(A.x-2+j, A.y, B.x-2+j, B.y);
  15. G.drawLine(A.x, A.y-2+j, B.x, B.y-2+j);
  16. }
  17. }
  18.  
  19. G.setColor(Color.blue);
  20. for(int i = 0; i < this.POINTS.size(); i++)
  21. {
  22. Point P = this.POINTS.get(i);
  23. G.fillOval(P.x-5, P.y-5, 10, 10);
  24. }
  25. }
And here is how it is in Main class:
  1. Graphic G;
  2. ....
  3. public Main()
  4. {
  5. ....
  6. G = new Graphic();
  7. G.setBounds(50, 35, 400, 400);
  8. CONTAINER.add(G);
  9. G.addMouseListener(this);
  10. .....
  11. G.repaint();
  12. }
  13. .....
  14. public void actionPerformed(ActionEvent arg0)
  15. {
  16. if (arg0.getSource() == pw)
  17. {
  18. PackageWrapping PW = new PackageWrapping(G);
  19. PW.show(POINTS);
  20. }
  21. }
Hope I was clear enough. The funny thing is, that after I click the button pw, the program gets stuck (I sometimes put some System.out.println("whatever...") just to see that it perfectly is written each 500ms!!!), the button is like it was pressed and not released... and after it makes everything it just then shows the final result(without showing the middle results that I want so much). I did not implement anything related to Threads however. Where do I have to do that?

  1. public class Main extends JFrame implements ActionListener, MouseListener
  1. public class PackageWrapping
  1. public class Graphic extends Canvas

Did I have to put anything more somewhere?

By the way, I've read a lot of articles related to Threads and they are working to me, but not working when I am trying to use them for my graphics!

Thank you for your help.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 11
Reputation: kolibrizas is an unknown quantity at this point 
Solved Threads: 1
kolibrizas's Avatar
kolibrizas kolibrizas is offline Offline
Newbie Poster
 
0
  #4
19 Days Ago
Another funny thing is that I found, that is: the loop(in the code below) is called the required amount of times, however that repaint() is not called here
  1. for (int i = 0; i < LINES.size(); i++)
  2. {
  3. PWLINES.add(LINES.get(i));
  4. this.G.transfer(POINTS, PWLINES);
  5. this.G.repaint();
  6. try
  7. {
  8. Thread.sleep(500);
  9. }
  10. catch (Exception e){}
  11. }
I found that adding a simple System.out.println in a repaint() method and in that loop. It just completes the last repaint() and thats it. If only I knew how to solve this part...
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,427
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 507
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster
 
1
  #5
19 Days Ago
The problem is that all of your code, including the thread.sleep() calls, are executing on the AWT event queue thread, which is the same thread that handles the repaint calls to update the display. You have to separate the animation updates from the painting. One way to do that would be to wrap the code you currently have in the show() method into a small Runnable class
  1. class LineAnimation implements Runnable{
  2. public void run() {
  3. Vector<Line> LINES = this.lines(POINTS);
  4. Vector<Line> PWLINES = new Vector<Line>();
  5. for (int i = 0; i < LINES.size(); i++) {
  6. PWLINES.add(LINES.get(i));
  7. this.G.transfer(POINTS, PWLINES);
  8. this.G.repaint();
  9. try {
  10. Thread.sleep(500);
  11. } catch (Exception e) {
  12. }
  13. }
  14. }
  15. }
and start a new thread with that Runnable in your show method
  1. Thread lineAnim = new Thread(new LineAnimation());
  2. lineAnim.start();

As BestJewSinceJC mentioned, a Swing Timer can also be used for this.
Last edited by Ezzaral; 19 Days Ago at 3:44 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 11
Reputation: kolibrizas is an unknown quantity at this point 
Solved Threads: 1
kolibrizas's Avatar
kolibrizas kolibrizas is offline Offline
Newbie Poster
 
0
  #6
19 Days Ago
  1. public void show(Vector<Point>POINTS)
  2. {
  3. Vector<Line> LINES = lines(POINTS);
  4. Thread lineAnim = new Thread(new LineAnimation(POINTS, LINES, this.G));
  5. lineAnim.start();
  6. }

and I made a class
  1. import java.util.Vector;
  2.  
  3. class LineAnimation implements Runnable
  4. {
  5. Vector<Point>POINTS;
  6. Vector<Line>LINES;
  7. Graphic G;
  8. public LineAnimation(Vector<Point>P, Vector<Line>L, Graphic G)
  9. {
  10. this.LINES = L;
  11. this.G = G;
  12. this.POINTS = P;
  13. }
  14.  
  15. public void run() {
  16.  
  17. Vector<Line> PWLINES = new Vector<Line>();
  18. for (int i = 0; i < LINES.size(); i++) {
  19. PWLINES.add(LINES.get(i));
  20. this.G.transfer(POINTS, PWLINES);
  21. this.G.repaint();
  22. try {
  23. Thread.sleep(500);
  24. } catch (Exception e) {
  25. }
  26. }
  27. }
  28. }

And it worked!!!!!!!

Thank you soooo much for your help!!!!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC