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.
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
(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.
> 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.
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.
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 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?
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
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.
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?
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
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?