Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I am trying to create a Java frontend for Mathematica. Following examples from J/Link guide and looking around the API manual I came up with this code:

public class Mathematica extends MathJFrame {
public Mathematica(String[] args) {
    initComponents();

     try {
        ml = MathLinkFactory.createKernelLink(args);
    } catch (MathLinkException e) {
        System.out.println("Fatal error opening link: " + e.getMessage());
    }

    try {
        ml.discardAnswer();

        ml.evaluate("2+2");
        ml.waitForAnswer();

        int result = ml.getInteger();
        System.out.println("2 + 2 = " + result);

        graphic1.setLink(ml);
        graphic1.setMathCommand("Plot[x,{x,0,1}]");
        graphic1.repaintNow();
        System.out.println(graphic1.getMathCommand());

    } catch (MathLinkException e) {
        System.out.println("Fatal error opening link: " + e.getMessage());
    } finally {
        ml.close();
    }
    private KernelLink ml = null;
}

Graphics.java:

public class Graphic extends MathGraphicsJPanel {
   public Graphic() {

   }
}

I know the link is working because I get 2 + 2 = 4 displayed in console but the panel where the graph output is expected is empty. Also, does J/Link not ship with sample programs anymore?

Thanks for any suggestions.

share|improve this question
    
Alex, welcome to Mathematica.SE! I must admit I know next to nothing about J/Link, but I noted there's no definition of the graphic1 object in your code. Shouldn't that be there? Also, in what format is graphic1 supposed to get graphics? Might it be a bitmap or so? –  Sjoerd C. de Vries Aug 1 '12 at 9:32
    
Have you seen JLink/tutorial/WritingJavaProgramsThatUseMathematica? It looks like one of the components that is needed is a MathCanvas which I don't see you using. –  Sjoerd C. de Vries Aug 1 '12 at 9:52
    
@SjoerdC.deVries Thank you! From the API manual: "MathGraphicsJPanel is the Swing counterpart to MathCanvas. Use a MathGraphicsJPanel if you are writing a Swing program and a MathCanvas if you are writing an AWT program." I left out the non-mathematica related code such as graphic1 = new Graphic(); and other irrelevant code netbeans adds to reduce question noise. –  alex-tech Aug 1 '12 at 10:48
add comment

1 Answer 1

up vote 1 down vote accepted

Looks like it was a bug in Mathematica. I just noticed that I was running 8.0.0 and updated to 8.0.4 and it is working now. Too bad the panel is not interactive. I assume I can rotate and interact with it only when viewing from mathematica's own notebook interface.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.