| | |
Help with Threads needed
Thread Solved |
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.

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:
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.
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.
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)
if (arg0.getSource() == pw) { PackageWrapping PW = new PackageWrapping(G); Vector<Line>BE = PW.lines(POINTS); G.transfer(POINTS, BE); G.repaint(); }
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.
•
•
Join Date: Sep 2008
Posts: 1,560
Reputation:
Solved Threads: 196
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
http://java.sun.com/docs/books/tutor...ncy/sleep.html
Out.
0
#3 19 Days Ago
Here is the code in PackageWrapping class:
And here is how it looks in Graphic class:
And here is how it is in Main class:
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?
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.
Java Syntax (Toggle Plain Text)
public void show(Vector<Point>POINTS) { Vector<Line>LINES = this.lines(POINTS); Vector<Line>PWLINES = new Vector<Line>(); for (int i = 0; i < LINES.size(); i++) { PWLINES.add(LINES.get(i)); this.G.transfer(POINTS, PWLINES); this.G.repaint(); try { Thread.sleep(500); } catch (Exception e){} } }
Java Syntax (Toggle Plain Text)
public void paint(Graphics G) { G.setColor(Color.white); G.fillRect(0, 0, 400, 400); G.setColor(Color.red); for(int i = 0; i < this.LINES.size(); i++) { Point A = this.LINES.get(i).A; Point B = this.LINES.get(i).B; for (int j = 0; j < 5; j++) { G.drawLine(A.x-2+j, A.y, B.x-2+j, B.y); G.drawLine(A.x, A.y-2+j, B.x, B.y-2+j); } } G.setColor(Color.blue); for(int i = 0; i < this.POINTS.size(); i++) { Point P = this.POINTS.get(i); G.fillOval(P.x-5, P.y-5, 10, 10); } }
Java Syntax (Toggle Plain Text)
Graphic G; .... public Main() { .... G = new Graphic(); G.setBounds(50, 35, 400, 400); CONTAINER.add(G); G.addMouseListener(this); ..... G.repaint(); } ..... public void actionPerformed(ActionEvent arg0) { if (arg0.getSource() == pw) { PackageWrapping PW = new PackageWrapping(G); PW.show(POINTS); } }
Java Syntax (Toggle Plain Text)
public class Main extends JFrame implements ActionListener, MouseListener
Java Syntax (Toggle Plain Text)
public class PackageWrapping
Java Syntax (Toggle Plain Text)
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.
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
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...
Java Syntax (Toggle Plain Text)
for (int i = 0; i < LINES.size(); i++) { PWLINES.add(LINES.get(i)); this.G.transfer(POINTS, PWLINES); this.G.repaint(); try { Thread.sleep(500); } catch (Exception e){} }
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 and start a new thread with that Runnable in your show method
As BestJewSinceJC mentioned, a Swing Timer can also be used for this.
Java Syntax (Toggle Plain Text)
class LineAnimation implements Runnable{ public void run() { Vector<Line> LINES = this.lines(POINTS); Vector<Line> PWLINES = new Vector<Line>(); for (int i = 0; i < LINES.size(); i++) { PWLINES.add(LINES.get(i)); this.G.transfer(POINTS, PWLINES); this.G.repaint(); try { Thread.sleep(500); } catch (Exception e) { } } } }
Java Syntax (Toggle Plain Text)
Thread lineAnim = new Thread(new LineAnimation()); 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.
0
#6 19 Days Ago
Java Syntax (Toggle Plain Text)
public void show(Vector<Point>POINTS) { Vector<Line> LINES = lines(POINTS); Thread lineAnim = new Thread(new LineAnimation(POINTS, LINES, this.G)); lineAnim.start(); }
and I made a class
Java Syntax (Toggle Plain Text)
import java.util.Vector; class LineAnimation implements Runnable { Vector<Point>POINTS; Vector<Line>LINES; Graphic G; public LineAnimation(Vector<Point>P, Vector<Line>L, Graphic G) { this.LINES = L; this.G = G; this.POINTS = P; } public void run() { Vector<Line> PWLINES = new Vector<Line>(); for (int i = 0; i < LINES.size(); i++) { PWLINES.add(LINES.get(i)); this.G.transfer(POINTS, PWLINES); this.G.repaint(); try { Thread.sleep(500); } catch (Exception e) { } } } }
And it worked!!!!!!!
Thank you soooo much for your help!!!!
![]() |
Similar Threads
- Reading in a *.csv file and loading the data into an Array (Java)
- old comp wont start windows....black screen upon boot (Troubleshooting Dead Machines)
- Win2000Pro Desktop Black Screen Message Virus Warning Fix? (Viruses, Spyware and other Nasties)
- LogicHosting.com & LogicHosting.net (Domain Names for Sale)
- Quad core rant. (Motherboards, CPUs and RAM)
- Wireless Board (DaniWeb Community Feedback)
Other Threads in the Java Forum
- Previous Thread: Drag-and-drop GUI-testing for Windows 7
- Next Thread: Correcting inefficiency in pseudocode
| Thread Tools | Search this Thread |
-xlint actionlistener android api applet application array arrays automation bi binary blackberry block bluetooth character class client code compile compiler component consumer database desktop developmenthelp eclipse error fractal freeze ftp game gameprogramming givemetehcodez graphics gui health html ide image integer j2me j2seprojects java javac javaee javaprojects jetbrains jni jpanel jtable julia learningresources lego linked linux list login loops mac main map method methods mobile netbeans notdisplaying number online printf problem program project properties qt recursion researchinmotion rotatetext rsa scanner screen server set singleton sms sort sql string swing system textfields threads time title tree tutorial-sample update variablebinding windows working xor






