aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Need help on animation using GUI Builder. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Need help on animation using GUI Builder." Watch "Need help on animation using GUI Builder." New topic
Author

Need help on animation using GUI Builder.

Aji Prasetyo
Ranch Hand

Joined: Jun 13, 2007
Posts: 65
Hello,

I am trying to make an JApplet animation using "NetBeans GUI Builder", but no luck so far. When I try to animate the GUI by looping it, only the last frame is shown to me, the rest is not painted.

Could somebody provide me with some guide on how to animate JApplet using GUI Builder, or better yet a sample program for me to look at.

Thank you.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
cant help with the netbeans part, but for the animation let's assume you're
trying to move a ball around the screen.

the ball has a size, color, position on screen.
(generally) in a while() you change the position of the ball, add a Thread.sleep(..)
and call repaint(). This gives you the effect of animation.

Your problem is you appear to be doing this in the EDT (event thread) which also
handles painting, so, any painting is blocked until the while() terminates,
then you get your 'last frame' showing.

solution is to move your while() into a separate thread
Aji Prasetyo
Ranch Hand

Joined: Jun 13, 2007
Posts: 65
Thank you for the reply.

(generally) in a while() you change the position of the ball, add a Thread.sleep(..) and call repaint(). This gives you the effect of animation.

I got that, thanks.

Your problem is you appear to be doing this in the EDT (event thread) which also handles painting, so, any painting is blocked until the while() terminates, then you get your 'last frame' showing.

Could you explain that to me again? What is event thread? I don't recall using any thread on my code, how come I do this in the event thread?

solution is to move your while() into a separate thread

Do you mean I need to use multi threading in order to play animation properly?

I am sorry, but could you give me an example in applet code? That really helps me to understand.

Thank you.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> What is event thread?

some info here
http://developers.sun.com/learning/javaoneonline/2006/desktop/TS-4855.pdf


> could you give me an example in applet code? That really helps me to understand.

run this - should be similar to your problem description, blank then after a
few seconds a blue ball appears.

same code, ball movement running via a separate thread
(ball just goes up/down - code brevity)

applets are funny things, run both above examples from separate files.
if you compile/run the first, then copy/paste/compile/run the second example
in the same file as the first, sometimes the old .class files are cached and
continue to be the ones run via the html page.
Aji Prasetyo
Ranch Hand

Joined: Jun 13, 2007
Posts: 65
Thank you very much.

From your example code I notice that in order to make another thread I need to implements Runnable and instantiate the Thread object, then do start() method.

This is part of code I still dont understand. Is this means that your code creates 2 thread, because I see 2 run() abstract method. Why is it?


The good news is now I can see my should-be-animating object throughout loop. But the bad news is that it doesn't animates, it is shown but not moving.
Actually, my program is using JNI native method to get the data array from sensor. The native method would call the java method to store the data into 2-dimensional int type of data array. Data array is then copied to a file through file streaming. from the file data is plotted into panel.
The problem is in all loop the data is written into file at only the first loop. Therefore because the data on the file is always the same in all loop, no animation can be seen.

I don't know how you can help me on this, but please help me.

Thank you.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
any updating of swing components should be done via the EDT.
SwingUtilities.invokeLater puts the call to repaint() at the end of the EDT.

can't help with the JNI stuff.
Perhaps another post, using JNI in the subject line, might attract some nibbles.
Aji Prasetyo
Ranch Hand

Joined: Jun 13, 2007
Posts: 65
Thanks for reply.

I just found out that when I comment-out the repaint(), the file can be updated properly within each loop. So the problem should be around the transfer of data from file into panel, not the JNI.

Do you know how to properly read data from a file for painting the panel? I mean why my program is only reading data form the first loop and not the rest?

I am very sorry for the trouble.

Any help is appreciated.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> I mean why my program is only reading data form the first loop and not the rest?

you would need to post the code - preferably a working example.
(actually, that would be a working 'non-working' example!)
Aji Prasetyo
Ranch Hand

Joined: Jun 13, 2007
Posts: 65
I just found out that when I comment-out the repaint(), the file can be updated properly within each loop. So the problem should be around the transfer of data from file into panel, not the JNI.

After I rewrite the code, I found out that the above problem is because I didn't put the repaint() method in the main applet code. if I move the repaint(), the file can be updated properly.

you would need to post the code - preferably a working example.
(actually, that would be a working 'non-working' example!)

I tried to rewrite the JNI so it can be run without the device and simpler.






It works fine when not include the JNI method.

But with JNI method, the program sequence is not as expected. Please take a look at the output from my program with JNI method.

This is 50 times loop. it is intended to gives "File rewrited..." and "Array plotted..." by each loop. "File rewrite..." is when the data is inserted into file, and "Array plotted..." is when the data is read from file and plotted.

I suppose it has to do with the threads. Is there a way to make those threads works the way I wanted to?

Thank you so much.
Aji Prasetyo
Ranch Hand

Joined: Jun 13, 2007
Posts: 65
I am sorry, perhaps I have made the problem's seems more difficult than it is.

Actually all I want to know is how to guarantee that repaint() method is executed in every loop?

Thank you.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
this looks highly suspicious, where drawPlots() reads a file

paintComponent() is called may times, for many reasons.
Each time it would be reading the file. This does not seem to be what you want.
run this, then drag the frame wider/taller to see how many times paintComponent() is called.



to run your code we would need a sample of what's in DataArrayTest.txt
Aji Prasetyo
Ranch Hand

Joined: Jun 13, 2007
Posts: 65
Thank you for the reply.

to run your code we would need a sample of what's in DataArrayTest.txt

I don't understand. I thought if you run the code I gave you, the DataArrayTest,txt file will be made for you in the projects root directory. Perhaps you mean, the one from the original code where the data comes from JNI-native method. Below is the DataArrayTest.txt contents from the original code (the one I had problem with). I am sorry, I don't know how to attach a file.


I tried your code. I understand what you mean, but that is what I am asking. on the contrary, my question is why in my code the paintComponent() method is only called 3 times in 11 times loop.
0 times.
1 times.
2 times.
File rewrited...
Array plotted...
3 times.
File rewrited...
4 times.
File rewrited...
5 times.
6 times.
File rewrited...
Array plotted...
7 times.
File rewrited...
8 times.
File rewrited...
9 times.
File rewrited...
10 times.
File rewrited...
Array plotted...
11 times.

Inside the loop first there is method for writing the file, then there is method for reading the file (by doing repaint() - the drawPlots(g) method). The problem is the writing method is executed so often than the reading method. I want to make the reading method (repaint()) executed directly after writing method. Is there a way to do it?

Thank you.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
flying a bit blind here because I'm not sure what the end result is supposed to be,
but here's your code, with a couple of changes
1)
changed to an application - much easier to work with than an applet (easy to convert back)
2)
seems to me drawPlots() should be the method in the separate thread - it reads
the data from the file.

if you run this, all that happens is 10 little red dots appear at 1 sec intervals

Aji Prasetyo
Ranch Hand

Joined: Jun 13, 2007
Posts: 65
Thanks for the reply.

I have examined your code. I noticed that the data shown are only from the last scanNo of the last run of program. that is not that I wanted.

paintComponent() is called may times, for many reasons.
Each time it would be reading the file. This does not seem to be what you want.

Actually, I would like to read the file and show(plot) the data everytime the file is changed(rewrite).

This is what I am doing. I try to put each data from every loop of program in separate file or collection. and run another thread that would read from the oldest file or collection and plot it without delay. Can it works? Can you show me how to do it?

Thank you so much.
Aji Prasetyo
Ranch Hand

Joined: Jun 13, 2007
Posts: 65
Hi again,

I would like to close this thread properly.
Many thanks to those that has put their efforts in helping me. I am very honored.

The code from Michael really helps me alot. I understand now to make another thread in order to calls repaint() periodically in infinite loop.

To conclude this is the sample applet code I've been working with. I believe there must be a lot of mistakes in there but I'm still learning.

Thank you very much and God bless you.

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Need help on animation using GUI Builder.