• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help on painting using JNI.  RSS feed

 
Aji Prasetyo
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,

I am making a GUI using JNI. JNI is used to get data which will be plotted into panel. the data is in form of 2-dimensional int type array. First this data will be stored in a file using file streaming. When repaint() is called, the paintComponent() method will read the file and plot the data into a panel.

Here is the output I got:


This is 50 times of loop. the sequence should be in every loop "File rewrited..." is followed directly by "Array plotted...". By the way "File rewrited..." is when the data is written into a file, and "Array plotted..." is when the file is read and plotted into panel. As you can see the sequence per loop is not as expected.

I think the problem is around the JNI thread and the repaint thread. I don't know much about multi threading and how JNI / repaint() thread behaves.

So if anyone know about this or know how to control the thread sequence, please help me.

Thank you so much.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's hard to say what's going on without seeing the code, but you either need to make sure that the file is not overwritten by the JNI code (maybe by waiting until the applet has finished painting it; it could signal that it's done by deleting the file), or by using different file names (e.g. file_0001.txt, file_0002.txt). Then the reading/painting code just needs a bit of logic to select the file with the lowest number.
 
Timothy Wall
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ulf Dittmer:
It's hard to say what's going on without seeing the code, but you either need to make sure that the file is not overwritten by the JNI code (maybe by waiting until the applet has finished painting it; it could signal that it's done by deleting the file), or by using different file names (e.g. file_0001.txt, file_0002.txt). Then the reading/painting code just needs a bit of logic to select the file with the lowest number.


I'd recommend you don't do anything but paint an image in paintComponent. The file read and image generation should be done on a thread other than the event dispatch thread. When you've finished generating the image, then you can pass it to your component and call repaint(). Otherwise, your UI will stutter and jerk according to your system's disk activity.
 
Aji Prasetyo
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the reply.

I'd recommend you don't do anything but paint an image in paintComponent. The file read and image generation should be done on a thread other than the event dispatch thread. When you've finished generating the image, then you can pass it to your component and call repaint(). Otherwise, your UI will stutter and jerk according to your system's disk activity.

I am sorry, but are you suggesting "double buffering"? How is it by separating the thread from event dispatch thread can guarantee that the repaint() method is run without delay?

Actually, right now I am trying to put the data acquired from JNI in a collection. each set of data acquired is putted in separate collection. this is done in a thread other than the event dispatch thread.

and than the other thread will try to read the oldest collection and plot the data without delay. After that the collection will be deleted.

could somebody provide me some sample codes on how to do this?

Thank you.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!