From: Michael Stachowsky on
I'm experimenting with trying to obtain the graphics from the axis canvas object and painting its contents into another java swing component. To do this, I'm trying to get the buffered image from the axis canvas and placing it as an icon in a JLabel.

The result, instead of the plot on the figure, is just a black square. Any thoughts? The experimental code I'm using is below. This was tested on R2009b (Matlab 7.9.0), but I've also tested it on R2010a, but it still won't work. Any help would be greatly appreciated,

Thanks,

Mike (Experimental code follows):

% EXPERIMENT5 An experiment to steal the graphics from AxisCanvas object.
clear all
clc

% Create a panel and use it to get the AxisCanvas object.
hPanel = uicomponent('style', 'JPanel');
jPanel = get(hPanel, 'JavaComponent');
jAxisCanvas = jPanel.getParent().getParent().getParent().getComponent(1);

% Create a label on a special JFrame which will show the image.
hFrame = uicomponent('style', 'JFrame');
jFrame = get(hFrame, 'Rootpane');
jFrame.setLayout(java.awt.BorderLayout() );

hLabel = uicomponent('style', 'JLabel');
jLabel = get(hLabel, 'JavaComponent');
jLabel.setBackground(java.awt.Color(uint8(102),uint8(102),uint8(102) ));
jFrame.add(jLabel, java.awt.BorderLayout.CENTER);

jFrame.validate();

% Actual experiment -- steal the graphics from AxisCanvas object and
% attempt to render it as an icon in JLabel.
axis
w = jAxisCanvas.getWidth();
h = jAxisCanvas.getHeight();
type = java.awt.image.BufferedImage.TYPE_INT_RGB;

jBufferedImage = java.awt.image.BufferedImage(w,h,type);
jGraphics2D = jBufferedImage.createGraphics();

jAxisCanvas.paint(jGraphics2D);
jGraphics2D.dispose();

% This results in a black rectangle in JLabel.
jLabel.setIcon(javax.swing.ImageIcon(jBufferedImage));
jLabel.setSize(jAxisCanvas.getSize() );

% This doesn't work either.
% jAxisCanvas.paint(jLabel.getGraphics() );