944,093 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 505
  • Java RSS
Nov 3rd, 2009
0

Help with Threads needed

Expand Post »
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:	26
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:

Java Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 22
Solved Threads: 1
Light Poster
kolibrizas is offline Offline
45 posts
since May 2009
Nov 3rd, 2009
0
Re: Help with Threads needed
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
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Nov 4th, 2009
0
Re: Help with Threads needed
Here is the code in PackageWrapping class:
Java Syntax (Toggle Plain Text)
  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:
Java Syntax (Toggle Plain Text)
  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:
Java Syntax (Toggle Plain Text)
  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?

Java Syntax (Toggle Plain Text)
  1. public class Main extends JFrame implements ActionListener, MouseListener
Java Syntax (Toggle Plain Text)
  1. public class PackageWrapping
Java Syntax (Toggle Plain Text)
  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.
Reputation Points: 22
Solved Threads: 1
Light Poster
kolibrizas is offline Offline
45 posts
since May 2009
Nov 4th, 2009
0
Re: Help with Threads needed
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
Java Syntax (Toggle Plain Text)
  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...
Reputation Points: 22
Solved Threads: 1
Light Poster
kolibrizas is offline Offline
45 posts
since May 2009
Nov 4th, 2009
1
Re: Help with Threads needed
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
Java Syntax (Toggle Plain Text)
  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
Java Syntax (Toggle Plain Text)
  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; Nov 4th, 2009 at 3:44 pm.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Nov 4th, 2009
0
Re: Help with Threads needed
Java Syntax (Toggle Plain Text)
  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
Java Syntax (Toggle Plain Text)
  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!!!!
Reputation Points: 22
Solved Threads: 1
Light Poster
kolibrizas is offline Offline
45 posts
since May 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Drag-and-drop GUI-testing for Windows 7
Next Thread in Java Forum Timeline: Correcting inefficiency in pseudocode





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC