Products & Services Solutions Academia Support User Community Company

Learn more about MATLAB Builder JA   

Buffered Image Creation Example

Purpose

This example demonstrates how to create a buffered image from a graphical representation of the surf(peaks) function in MATLAB.

The hardcopy function is used to output the figure window as an array:

	function w = getSurfsFigure
		f = figure;
		set(f,'Visible', 'off');
 
		f = surf(peaks);
		w = hardcopy(gcf,'-dOpenGL','-r0');
	end

Procedure

  1. You create a Java application by using the Deployment Tool GUI to build a Java class that wraps around your MATLAB code.

    To compile or build the Java application using the Deployment Tool, use the following information as you work through this example in Building the Java Component:

    Project NamePeaksdemo
    Class NamePeaks
    File to compilegetSurfsFigure.m

  2. From the output distrib folder of your build, copy Peaksdemo.jar to the folder where you are building your application.

  3. Create the SurfPeaks.java code:

    //imported classes from JRE
    import java.awt.Image;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.ImageIcon;
    
    //imported classes from javabuilder.jar
    import com.mathworks.toolbox.javabuilder.Images;
    import com.mathworks.toolbox.javabuilder.MWArray;
    import com.mathworks.toolbox.javabuilder.MWNumericArray;
    import com.mathworks.toolbox.javabuilder.MWException;
    
    //Import for the Deployment Project Peaksdemo.jar
    import Peaksdemo.Peaks;
    
    //The goal of this class is to show how you would deal with 
    //  static images coming from a java 
    //          deployment of a MATLAB figure.  
    //This can be run as a standalone java application.  
    //
    //The MATLAB function being deployed is surf(peaks).  
    //The hardcopy function is used to 
    //  output the figure window as an array.
    //MATLAB Code: 
    //********************************************
    //    function w = getSurfsFigure
    //        f = figure;
    //        surf(peaks);
    //        w = hardcopy(gcf,'-dOpenGL','-r0');
    //        close(f);
    //    end
    //********************************************
    //
    //For this example you must have the deployment project 
    //  jar and the javabuilder.jar on your classpath.
    //
    //Note: 
    //For this example there is minimal error handling.  
    //Typically you would want to integrate this with whatever 
    //  logging is currently in place for your java layer.
    public class SurfPeaks
    {
        //This initializes and executes the JFrame.  
        public static void main(String args[])
        {
          ImageIcon icon = new ImageIcon(getSurfImage());
          JLabel label =  new JLabel(icon);
          JFrame frame = new JFrame();
          frame.setSize(icon.getIconWidth(),icon.getIconHeight());
          frame.setContentPane(label);
          frame.setVisible(true);        
        }
        
        //This method is basically our "business logic" method.  
        //It is responsible for instantiating our 
        //   MATLAB deployment, 
        //passing in any needed inputs, and dealing with 
        //   any outputs. 
        //In this example we have no inputs, and the only 
        //   output is the 
        //figure in hardcopy format 
        //     (three dimensional MWNumericArray).
        private static Image getSurfImage()
        {
            try
            {
                //Our deployment uses native resources and 
                //should be disposed of as soon as possible. 
                Peaks matlabModel = new Peaks();
                try
                {
                    //If we had any inputs to our method 
                    //they would be passed in here.  
                    Object[] results = 
                          matlabModel.getSurfsFigure(1);
                    
                    //This array uses native resources and 
                    //should be disposed of as soon as possible.
                    MWArray mwArray = (MWArray)results[0];
                    try
                    {
                        //Since we want this method to 
                        //                 return only 
                        //     non MATLAB data 
                        //  we convert the MATLAB figure to a 
                        //     buffered image and return it.
                        return Images.renderArrayData
                                 ((MWNumericArray)mwArray);
                    }
                    finally
                    {
                        MWArray.disposeArray(mwArray);
                    }
                }
                finally
                {
                    matlabModel.dispose();
                }
            }
            catch(MWException mwe)
            {
                mwe.printStackTrace();
                return null;
            }
        }    
    }
    
  4. Compile the program using javac and the following command:

    javac -classpath javabuilder.jar;Peaksdemo.jar SurfPeaks.java

    Ensure that javabuilder.jar and Peaksdemo.jar (compiled earlier) are both in the folder you compile from (or define their full paths).

  5. Run SurfPeaks.class using the following java command. Ensure that javabuilder.jar and Peaksdemo.jar (compiled earlier) are both in the folder you compiled in (or define their full paths).

    java -classpath javabuilder.jar;Peaksdemo.jar;. SurfPeaks 

      Note   If you are running on the Mac 64-bit platform, you must add the -d64 flag in the Java command. See Limitations and Restrictions for more specific information.

The following Surf Peaks graphic opens.

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2010- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS