Legal Notice, Disclaimer, and Book Ordering Information:

(* Legal Notice, Disclaimer, and Book Ordering Information:

Last Modified: FIO Jan 10, 2003

Mathematica For Physics: 2nd Edition

Robert L. Zimmerman and Fredrick I. Olness

WebSite:  
  www.physics.smu.edu/~olness
  darkwing.uoregon.edu/~phys600/

MathSource Number: 0206-862

ISBN 0-201-53796-6
For ordering information, call 1-800-282-0693
Addison-Wesley Publishing Company

Communication with the authors:
Fredrick I. Olness:  olness@mail.physics.smu.edu
Robert L. Zimmerman: bob@zim.uoregon.edu

Copyright 2002, Addison-Wesley Publishing Company, Inc. The material in
this file may be distributed freely  so long as the content remains
unchanged, and the copyright and  reference notices are included. The
publisher grants permission for the noncommercial use of these programs
and program segments.  All other uses require the prior written consent
of the publisher.    

All rights are reserved with regard to the  material in the
"Mathematica For Physics" text, and may not be reproduced, stored in a
retrieval system, or transmitted,  in any form or by any means,
electronic, mechanical, photocopying,  recording, or otherwise, without
the prior written permission  of the publisher. 

The programs and applications presented in this book have been included
for  their instructional value. They have been tested with care but are
not  guaranteed for any particular purpose. The publisher does not
offer any  warranties or representations, nor does it accept any
liabilities with  respect to the programs or applications. 

 *)

Java Sample

This sample generates a simple Mathematica graphic in a external Java window. 

This Notebook requires JLink from Wolfram and Java from Sun.

Travelling Wave Animation

Implementation using Mathematica's New JLink Capability

Some Basic Setup

Off[General::"spell"]; 
Off[General::"spell1"];

Load JLink: available from Wolfram at www.wri.com

<< JLink`

Load Java: available from Sun at java.sun.com

InstallJava[]

UseFrontEndForRendering = False;

createWindow[]:=Module[{frame},
frame = 
              JavaNew["com.wolfram.jlink.MathFrame", "Vibrating \
String"];
drawArea = 
              JavaNew["com.wolfram.jlink.MathCanvas"];
drawArea@setUsesFE[UseFrontEndForRendering];
drawArea@setSize[800, 600];
\(JavaBlock[
              frame@setLayout[
                  JavaNew["java.awt.BorderLayout"]]; 
frame@
                add[drawArea, 
                  ReturnAsJavaObject[
                    BorderLayout`CENTER]]; 
frame@
                pack[]; 
frame@
                setSize[800, 600]; 
frame@
                setLocation[100, 100]; 
JavaShow[frame]]\);
frame
]

Define the plot: drawWave

colors = {red, green, blue} = {RGBColor[1, 0, 0], 
            RGBColor[0, 1, 0], RGBColor[0, 0, 1]};

\(\(\(\ \ \)\(\(drawWave[c_, 
        t_]\)\(:=\)\(\ \)\(
\)\(Plot[
{Cos[
              2\ π\ \((x - c\ t)\)], Cos[2\ π\ \((x + c\ t)\)], 
            Cos[2\ π\ \((x - c\ t)\)] + Cos[2\ π\ \((x + c\ t)\)]}\  // 
          Evaluate, {
x, \(-2\)\ , 
          2}, 
PlotStyle -> {blue, 
            green, {Thickness[0.004], 
              red}}, 
PlotRange -> {\(-2\), 
            2}, 
AspectRatio -> 
          Automatic, 
PlotLabel -> "Travelling Wave", 
DisplayFunction -> Identity]\)\(\ \ \)\)\)\)

Show[drawWave[0.5, 1], 
        DisplayFunction -> $DisplayFunction];

c = 0.25;

times = Range[0, 10, c];

Setup the animation

LoadJavaClass["java.lang.Thread"];

\(\(\(\ \)\(AnimationPlot[c_, 
        t_List] := 
JavaBlock[
Block[{frm}, 
frm = 
            createWindow[]; 
Map[\((obj = 
                  drawWave[c, #]; 
drawArea@
                  setMathCommand["obj"]; 
drawArea@
                  repaintNow[]; 
Thread`sleep[10];)\) &, 
            t\ ]
]
]\)\)\)

Draw in a Java window

AnimationPlot[c, times];