MathGroup Archive 2002

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: JLink and speeding up graphics display by java routine

  • To: mathgroup at
  • Subject: [mg36722] Re: [mg36661] JLink and speeding up graphics display by java routine
  • From: Todd Gayley <tgayley at>
  • Date: Fri, 20 Sep 2002 04:16:43 -0400 (EDT)
  • Sender: owner-wri-mathgroup at

At 01:09 AM 9/18/02, Blimbaum Jerry DLPC wrote:
>I am trying to write an animation using JLink.....basically as
>follows...(using Mathematica 4.1, JLink 2.01 and java 1.4.1)...
>frm = createWindow[];     (* basically to create a MathFrame and a  drawArea
>= MathCanvas *)
>Here is my drawing routine....
>  Map[(obj = drawMembrane[#];
>         drawArea@repaintNow[];
>         Pause[.0001];) &, t ]
>t = time list ={0,.25,etc}.... and drawMembrane is a function that uses the
>time value to make a graphics plot......unfortunately, this routine draws
>the next time plot very slowly, plus I also notice that without the Pause[]
>statement, Mathematica only draws the graph for the final time value...


You did a nice job with this "one-frame-at-a-time plot". (I'm replying here 
to this message and your previous one where you presented the full code.) 
The main reason that it is slow drawing the frames is that the Pause[] 
function only works with integer second intervals. If you say Pause[.0001] 
it pauses for a whole second, which inserts a 1-second delay between each 
frame. If I take out the Pause, I get about 5 frames per second on my 
laptop (for your Sin plot). This still seems slow to me, but it turns out 
that Java is just slow at taking GIF data and making it into pixels to be 
displayed. That step consumes virtually all the time for the animation.

If you make the image smaller, the animation runs much faster. Try making 
your window size 400 x 300 instead of 800 x 600. At that size, the 
animation runs at a good speed on my machine. If you want to insert a delay 
between frames, you need to use Java instead of calling Pause[]:

Map[(obj = drawMembrane[#];
     (* Add a 100 ms delay *)
     Thread`sleep[100];) &, t ]

You said that without the Pause[] you only get the last frame drawn, but 
that shouldn't have any effect. Perhaps you took out the call to 
repaintNow() along with the Pause?

>Following an example in the JLink documentation, I have written the
>following java routine....which I saved in the same dir as MathFrame,
>public class My_DisplayGraphicsViaJava {
>     public void displayGraphics(String cmds[]) {  // declare String Array
>for Graphics Objects
>           for (int i = 0; i < cmds.length-1; i++)
>                    drawArea.setMathCommand(cmds[i]);
>                       drawArea.repaintNow();
>                       Thread.sleep(200);}         }
>and then in Mathematica I have
>  LoadJavaClass["My_DisplayGraphicsViaJava"];
>My_DisplayGraphicsViaJava`displayGraphics["obj"];    (* where obj is the
>array of Graphics plots created *)
>But needless to say this dont work....LoadJavaClass responds by saying Class
>not what should I do to correct this?   Also , on the java
>routine, was i supposed to add:     Extends    MathFrame, will my
>java routine know what drawArea.setMathCommand is, I also
>supposed to compile my java routine?

You will definitely need to compile any Java classes you write! But writing 
this in Java won't change anything because the delay is in Java itself, not 
in J/Link or the MathLink communication. This Java class would need a lot 
of work before it would compile. You would need to either create drawArea 
in the Java class rather than in Mathematica code, or pass it as an 
argument to displayGraphics() (which needs to be static, by the way). 
Anyway, there is no point in thinking about any of that, since this 
displayGraphics() method just duplicates functionality that you have 
already (much more conveniently) scripted in Mathematica code.

One final general note for J/Link programmers: It's OK to create Java names 
with underscores in them, but you have to remember how they are handled in 
Mathematica. When you refer to a Java name in Mathematica code as a string, 
you keep the underscore:


But when a name appears as a symbol in Mathematica, J/Link maps it to a U 
because _ is not legal in a symbol name:


It's often easiest to just avoid using _ in the names of Java classes and 
methods that you write for use with Mathematica.

Todd Gayley
Wolfram Research

  • Prev by Date: Re: Different letters different solutions!!
  • Next by Date: How to display package contents?
  • Previous by thread: JLink and speeding up graphics display by java routine
  • Next by thread: Thanks to all; here's what looks to be best solution