MathGroup Archive 2000

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

Search the Archive

Re: Real-time plotting

  • To: mathgroup at smc.vnet.net
  • Subject: [mg22829] Re: Real-time plotting
  • From: tgayley at linkobjects.com (Todd Gayley)
  • Date: Fri, 31 Mar 2000 01:01:31 -0500 (EST)
  • Organization: LinkObjects
  • References: <8aqq22$a1i@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

On 16 Mar 2000 09:11:14 -0500, aram at sirius.com (Aram Schiffman) wrote:

>Hi all,
>
>Sorry if this is an oldie, but ...
>
>How might I conjure up a method for real-time plotting? What I have in
>mind is this: every time a function calulates a new point, it updates
>a plot (presumably, a listplot). When I've collected enough points to
>satisfy myself, I hit a key or click the mouse or enter the interrupt
>command, and it aborts. What I don't want to do is accumulate one new
>plot per new point (obviously, I guess). 
>
>Can anyone clue me in?
>
>Much obliged, in advance.
>

This is a good example of something that can be done easily with the freely-available
J/Link package (www.wolfram.com/solutions/mathlink/jlink). A nice answer has already been
provided that uses only the front end, and it might seem to be overkill (not to me) to use
J/Link for this, but it makes a nice example. Furthermore, if you wanted to do anything
more complex with this, you could run up against the limits of the front end very quickly,
whereas we are just scratching the surface of what J/Link can do. The suggestion of
clicking the plot to make the computation stop comes to mind.

Any time a problem comes up that suggests some auxiliary user-interface element (like an
updating plot window), J/Link is an obvious technology to use.

Before I go on to show how easy this is with J/Link, I should point out that I had to add
a feature to J/Link to make it this easy. It was working on this very question that
revealed that an obvious and useful class was missing from J/Link. This feature is the new
MathCanvas class, which makes it extremely simple to display Mathematica graphics or
typeset expressions in J/Link programs, either pure Java programs or, as here, Mathematica
programs that call into Java. It was always easy to _get_ these images in J/Link programs
(via the evaluateToImage() and evaluateToTypeset() methods), but MathCanvas provides an
even higher-level interface for obtaining and displaying them. Users cannot try this code
until the 1.0.2 release of J/Link is available, which should be in a matter of days.

The code starts off by simply creating two Java objects (MathFrame for the top-level
window, and MathCanvas to display the plot), and setting them up using the standard Java
API for GUI components. Then it calls setMathCommand to specify the Mathematica code to
evaluate to produce the image for the MathCanvas. The functions JavaBlock, JavaNew, and
JavaShow, as well as the @ notation, are standard J/Link fare.

RealTimePlot[] :=
    JavaBlock[
        Module[{frame, canvas, pts = {{Random[], Random[]}}},
            InstallJava[];
            frame = JavaNew["com.wolfram.jlink.MathFrame", "Real-Time Plotting"];
            canvas = JavaNew["com.wolfram.jlink.MathCanvas"];
            frame at add[canvas];
            frame at setSize[400, 400];
            canvas at setSize[400, 400];
            plotFunc[] :=
                  ListPlot[pts, PlotRange -> {{0, 1}, {0, 1}}];
            canvas at setMathCommand["plotFunc[]"];
            JavaShow[frame];
            While[True,
                AppendTo[pts, {Random[], Random[]}];
                canvas at recompute[];
                Pause[1]
            ]
        ]
    ]

This function produces a rock-solid, flicker-free animation, in contrast to the front-end
technique.

For those interested, here is how to expand this to allow the user to click on the plot to
gracefully stop the computation. All we do is add a few lines that create and manage a
MathMouseListener that calls back into Mathematica when the mouse is clicked on the image.
The call to ServiceJava[] is a new feature in J/Link 1.0.2 that allows a running
Mathematica program to periodically allow requests for computations that originate from
the Java user-interface thread. This lets Mathematica perform the "done = True" callback
when the mouse is clicked.

RealTimePlot2[] :=
    JavaBlock[
        Module[{frame, canvas, mml, done = False,
                      pts = {{Random[], Random[]}}},
            InstallJava[];
            frame = JavaNew["com.wolfram.jlink.MathFrame", "Real-Time Plotting"];
            canvas = JavaNew["com.wolfram.jlink.MathCanvas"];
            frame at add[canvas];
            frame at setSize[400, 400];
            canvas at setSize[400, 400];
            plotFunc[] :=
                  ListPlot[pts, PlotRange -> {{0, 1}, {0, 1}}];
            canvas at setMathCommand["plotFunc[]"];

            mml = JavaNew["com.wolfram.jlink.MathMouseListener"];
            mml at setHandler["mouseClicked", "mouseClickFunc[]"];
            mouseClickFunc[] := done = True;
            canvas at addMouseListener[mml];

            JavaShow[frame];
            While[!done,
                AppendTo[pts, {Random[], Random[]}];
                canvas at recompute[];
                Pause[1];
                ServiceJava[]
            ]
        ]
    ]


--Todd

-------------------------------------------------
Todd Gayley
Wolfram Research, Inc.
tgayley at wolfram.com



  • Prev by Date: Re: Image window via Mathlink
  • Next by Date: Re: Re: N-Dimensional line
  • Previous by thread: Re: Real-time plotting
  • Next by thread: Monte Carlo