From: Ampere Kui on
Consider using the getframe function if all you want to do is take a picture of the axes and put it into a Java object. You will also have to make the FigureComponentContainer object disappear for a brief moment to avoid capturing those artifacts in your figure into your screenshot.

The modified code is as follow:


% 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.

% The strategy is to make the FigureComponentContainer invisible for a
% brief moment while we try to grab a snapshot of the figure, then make the
% container reappear.
jAxisCanvas.getParent().getComponent(0).setVisible(false);
axis
f = getframe(gcf);
jAxisCanvas.getParent().getComponent(0).setVisible(true);

% Put picture into label.
jLabel.setIcon(javax.swing.ImageIcon(im2java(frame2im(f))));