- Undocumented Matlab - https://undocumentedmatlab.com -

Transparent Matlab figure window

Posted By Yair Altman On April 13, 2011 | 18 Comments

Every now and then, a user asks whether it is possible to make an entire Matlab window transparent (example [1]). This could be used, for example, for window fade-in/fade-out effects. The short answer is that there is no supported way of doing this with pure documented Matlab, but it is trivially easy to achieve using just a bit of Java magic powder (surprise, surprise).

Matlab figure window transparency

Following an idea I got from Malcolm Lidierth’s MUtilities [2] submission on the Matlab File Exchange, the solution for setting Matlab figure window transparency is quite simple: Get the figure’s underlying Java window reference handle, as in last week’s [3] article. Then use Java’s setWindowOpacity method [4] to set the window’s transparency value. Actually, setWindowOpacity sets the opacity level, rather than transparency, but they are obviously complementary and I personally find “transparency” to be more easily understandable.
By default, windows are created with an opacity of 1.0 (= not transparent). They can be set to any floating-point value between 0.0-1.0, where an opacity of 0.0 means full transparency, and any value in between means partial transparency (i.e., translucency):

jFigPeer = get(handle(gcf),'JavaFrame');
jWindow = jFigPeer.fFigureClient.getWindow;
com.sun.awt.AWTUtilities.setWindowOpacity(jWindow,0.7)

Semi-transparent (translucent) Matlab figure (click to enlarge) [5]
Semi-transparent (translucent) Matlab figure (click to enlarge)

Similarly, you can set the entire Matlab Desktop’s transparency/opacity value:

jDesktop = com.mathworks.mde.desk.MLDesktop.getInstance.getMainFrame;
com.sun.awt.AWTUtilities.setWindowOpacity(jDesktop, 0.8);

Note that the com.sun.awt.AWTUtilities class also enables other GUI effects [6] that would make a Matlab GUI developer’s mouth to start drooling: shaped windows, per-pixel transparency values, mirroring/reflection, window shadows, gradients [7] etc. Perhaps I’ll explore their adaptation for Matlab figures someday.

Fade-in / fade-out

Window fade-in/fade-out effects can easily be achieved using transparency: Simply invoke the setWindowOpacity method several times, with progressively higher or lower values. This is easily done in a simple blocking loop. For example, to fade-out a window:

for stepIdx = 1 : 5
   newAlpha = 1.0 - 0.2*stepIdx;
   com.sun.awt.AWTUtilities.setWindowOpacity(jWindow,newAlpha);
   jWindow.repaint;
   pause(0.2);  % seconds
end

Gradual window fade-out
Gradual window fade-out

A more general example dynamically computes the opacity step size/duration and also enables non-blocking fade effects using an asynchronous timer:

% Compute the required opacity-setting steps
fadeDuration = 1.5;  % seconds
oldAlpha = com.sun.awt.AWTUtilities.getWindowOpacity(jWindow);
newAlpha = 0.0;
deltaAlpha = newAlpha - oldAlpha;
maxStepAlpha = 0.03;
steps = fix(abs(deltaAlpha) / maxStepAlpha) + 1;
stepAlpha = deltaAlpha / steps;
stepDuration = fadeDuration / (steps-1);
% If blocking, do the fade effect immediately
if blockingFlag || steps==1
   for stepIdx = 1 : steps
      newAlpha = oldAlpha + stepAlpha*stepIdx;
      com.sun.awt.AWTUtilities.setWindowOpacity(jWindow,newAlpha);
      jWindow.repaint;
      if stepIdx < steps,  pause(stepDuration);  end
   end
else
   % non-blocking: fade in/out asynchronously using a dedicated timer
   start(timer('ExecutionMode','fixedRate', 'Period',0.1, 'TasksToExecute',steps, ...
               'TimerFcn', {@timerFcn,jWindow,oldAlpha,stepAlpha}));
end
% Timer function for non-blocking fade-in/fade-out effect
function timerFcn(hTimer,eventData,jFrame,currentAlpha,stepAlpha)  %#ok eventData
  stepIdx = hTimer.TasksExecuted;
  newAlpha = currentAlpha + stepAlpha*stepIdx;
  com.sun.awt.AWTUtilities.setWindowOpacity(jFrame,newAlpha);
  jFrame.repaint;
  if stepIdx == hTimer.TasksToExecute
      stop(hTimer);
      delete(hTimer);
  end
end  % timerFcn

Of course, you can also fade-in/out to intermediate values such as 0.3 or 0.8. If you fade-out completely (i.e., to a value of 0.0), it might be a good idea to actually close the figure window once it gets the totally-transparent value of 0.0.
I’ve prepared a Matlab utility that contains all these options, including optional blocking/non-blocking fade effects, in my setFigTransparency utility [8], which is available for download on the Matlab File Exchange. You may also wish to use Malcolm Lidierth’s MUtilities [2], which also has similar functionalities (plus some other goodies).

Limitations

Setting a figure window’s transparency requires using Java Run-time Engine (JRE) 1.6.0_10 (also called “Java 6 update 10”) or higher. This means that it’s supported on Matlab release 7.9 (R2009b) and higher by default, and on earlier releases using a JRE retrofit.
If you are using an earlier Matlab release, consider a retrofit of JRE 1.6.0_10 or any later version (e.g., the latest available version today is 1.6 update 24). The JRE can be downloaded from here [9], and you can configure Matlab to use it according to the instructions here [10]. As noted, Matlab R2009b (7.9) and onward, at least on Microsoft Windows, pre-bundle a JRE version that does support transparency/opacity and so do not require a retrofit.
You can check your current Java version in Matlab as follows:

>> version -java
ans =
Java 1.6.0_17-b04 with Sun Microsystems Inc. Java HotSpot(TM) Client VM mixed mode

Unfortunately, Matlab plot axes cannot be made transparent. If you have any axes in your GUI, the axes area will simply appear as a shaded axes, whose intensity depends on the selected alpha (transparency) value; the contents beneath the window will not be merged in the axes area as it is in the non-axes areas.
Finally, note that com.sun.awt.AWTUtilities is itself an undocumented Java class. It is bundled with the standard Java release since 2008 (1.6.0_10), and yet is not part of the official release because its API has not yet settled. In fact, in the upcoming Java 7 release, which is expected in a few months, and which I expect to be available in Matlab sometime in 2012, the set of transparency/opacity methods have migrated [11] to the fully-documented java.awt.Window class.

Blurred figure window

So here’s a riddle for you: using figure window transparency, can you guess how to make a Matlab figure appear blurred for disabled figures [3] (see the screenshot there)? There are several possible ways to do this – can you find the simplest? The first one to post a comment [12] with a correct answer gets a smiley… My answer will appear in next week’s article.

Upgraded website

Also, did you notice my new website design? It’s supposed to be much more readable (yes – also on Android [13]…). It now also runs on a multi-server cloud, which means more stability and faster response times. Do you like the new design? hate it? I would love to hear your feedback via comment [12] or email [14].

Categories: Figure window, GUI, Hidden property, Java, Medium risk of breaking in future versions


18 Comments (Open | Close)

18 Comments To "Transparent Matlab figure window"

#1 Comment By Brant A Jameson On April 14, 2011 @ 14:30

Very Cool,
I’ve had numerous applications for this, particularly when debugging/regression testing code. Thanks for the heads up!
-Brant

#2 Comment By Jan Simon On April 15, 2011 @ 06:03

Dear Yair,
I definitely love the new layout! I’m using an old laptop with a 1024*768 screen for the internet access. Now much more of the screen is used for the text and I have to scroll less.
Although this is really nice, I admit that I appreciate the contents of your pages even more. I needed a tree-view for a project in Matlab 2009a – no chance without your work! Thanks a lot, Jan

#3 Comment By Mory On April 15, 2011 @ 11:46

Nice trick.

Regarding your riddle, the code below seems to work. It could/should be improved with some callbacks to keep the overlay aligned.
Another option is to use a solid underlay, and make the main figure semi-transparent. This requires a little bit more bookkeeping to make sure that the function can undo the “blur”, but allows the main figure to continue receiving UI input.

function blurwindow(fnum)
%BLURWINDOW  Blurs a given window.
%    BLURWINDOW Blurs the current window
%    BLURWINDOW(FNUM) blurs the given window.
%
%    BLURWINDOW clear REMOVES THE BLUR

%Use a consistent, random figure number
FNUMBLUR = 1636033446;

%Input handling 
if nargin < 1
    fnum = gcf;
elseif ischar(fnum) && strcmpi(fnum,'clear')
    if ishandle(FNUMBLUR)
        close(FNUMBLUR);
    end
    return;
end

%Find the position of the figure to be blurred
figPos = get(fnum,'position');
figUnits = get(fnum,'units');

%Setup the overlay figure
figure(FNUMBLUR);
set(FNUMBLUR,'units',figUnits);
set(FNUMBLUR,'position',figPos);
set(FNUMBLUR,'NumberTitle','off';
drawnow;

%Make the overlay semi-transparent
jFigPeer = get(handle(FNUMBLUR),'JavaFrame');
jWindow = jFigPeer.fFigureClient.getWindow;
com.sun.awt.AWTUtilities.setWindowOpacity(jWindow,0.8)

#4 Comment By Yair Altman On April 16, 2011 @ 10:51

@Mory – not bad for a first attempt, so you definitely deserve a 🙂

The basic idea is to create an empty figure window in the exact size and position as the blurred window, overlay it on the blurred window, then set its opacity value to 0.8.

The full answer is somewhat more complex, as you’ve hinted. For example, we also need to synchronize the figure/blurring color, menubar/toolbar, and title. We also need to handle docking, resizing and figure movement. Finally we need to connect the two figures so that whenever one is closed then so is the other.

Anyone who can’t wait to next week’s article is welcome to look [21].

#5 Pingback By Controlling callback re-entrancy | Undocumented Matlab On August 10, 2011 @ 11:04

[…] months ago, I mentioned his work on another File Exchange contribution, MUtilities when I discussed transparent Matlab figure windows. Today, Malcolm discusses one of his lesser-known but extremely important isMultipleCall […]

#6 Comment By Alex On April 20, 2012 @ 10:01

Hi

I am not sure if this is possible but here we go. I am loading this image on a Matlab GUI and it’s quite an image so it takes some time to upload and I’ll like to add a waiting bar but I really don’t like the one provided by Matlab (Matlab it’s awesome but rates low in coolness and sleekness). I was reading your book and I would love to add what you presented on page 296-297 but I would like to present just the animation and not that gray background panel. I was trying to play with a method called paintAffordance but that doesn’t even exist in Java. Is there any way I could makes transparent the background of the BusyAffordance example transparent and just have the animation running and put it on top of the image I am trying to upload? Thanks.

#7 Comment By Yair Altman On April 21, 2012 @ 10:27

@Alex – unfortunately, Matlab does not (AFAIK) support transparent Java components. More on this in pages 358 (footnote) and 453 of my book.

#8 Comment By Malcolm Lidierth On April 22, 2012 @ 08:10

N.B. Swing/AWT transparency works fine with MATLAB on the Mac – but not Windows.
MATLAB are deprecating ‘none’ as a background colorspec option in their containers. Maybe they are addressing this.

#9 Comment By julien On June 23, 2012 @ 07:45

Hi Yair,
I would like to draw lines and rectangles above both axes and uicontrols. It would be good to use matlab figure’s GlassPane (I found it in JavaFrame’s hierarchy) but I failed to draw anything in it.
I also tried to add a transparent axes which overlays the entire figure but this way, I cannot draw above uicontrols.
Do you have suggestions ? Thanks in advance for your help !

#10 Comment By Yair Altman On June 23, 2012 @ 11:40

@Julien – there may be a way to do this directly using the figure hierarchy, but I never tried it. Try using one of the other figure containers, not necessarily the glass pane. As an alternative, you can use the overlay transparent window trick mentioned in this article to draw whatever you wish on top of the uicontrols, as explained in the [22] article.

#11 Comment By Dirk Engel On March 25, 2013 @ 04:06

Hi Yair,

I had the same idea of using the GlassPane to overlay a JComponent with semi-transparent background to blur the ContentPane. This would be a nice solution to your riddle which goes without a helper figure, but it seems that neither the GlassPane nor other child components of the LayeredPane (except the ContentPane) get painted at all. Maybe the paint method of the RootPane (com.mathworks.mwswing.desk.DTRootPane) is responsible for this. Any ideas?

#12 Comment By Yair Altman On March 25, 2013 @ 04:21

@Dirk – welcome back!

Unfortunately, all GUI controls added via Matlab, including uicontrols and javacomponents, use a heavyweight java.awt.Panel container, rather than a lightweight javax.swing.JPanel. Unfortunately, the heavyweight Panels, unlike lightweight JPanels, cannot be made transparent (at least on Windows). This means that even if the control or component is made transparent, this would still have no effect since their containing Panel is opaque. See an interesting discussion on this [23].

Note that while individual controls are opaque, the entire figure window can be made fully or partially transparent, as I have shown in the main article – the trick is not to update any specific control’s opacity but to work at the Java Frame level.

#13 Pingback By ScreenCapture utility | Undocumented Matlab On January 28, 2013 @ 08:14

[…] The solution of the rbbox-anywhere feature relies on using a temporary transparent window that spans the entire desktop area, capturing user rbbox clicks anywhere within the desktop area. […]

#14 Comment By Martin Höcker On June 12, 2013 @ 08:50

Looks great! For future reference, the code seem to need small modifications to work with Matlab 2013a. On my 32-bit Windows XP machine, I had to use:

jFigPeer = get(handle(gcf),'JavaFrame');
jWindow = jFigPeer.fHG1Client.getWindow;
com.sun.awt.AWTUtilities.setWindowOpacity(jWindow,0.7)

#15 Comment By Yair Altman On June 12, 2013 @ 09:42

@Martin – indeed so, and you will need to modify this again in HG2, as described [24]

#16 Comment By Malcolm Lidierth On June 12, 2013 @ 19:10

Note that AWTUtilities simply calls the setOpacity method on the Window in Java 7. There are some conditions:
The TRANSLUCENT translucency must be supported by the underlying system
The window must not be in full-screen mode (see GraphicsDevice.setFullScreenWindow(Window))

and also (new)

The window must be undecorated (see Frame.setUndecorated(boolean) and Dialog.setUndecorated(boolean))

MATLAB figures are decorated

#17 Comment By Razvan On September 7, 2013 @ 16:58

Hi Yair,

Is it possible to update setFigTransparency function in order to work in the latest MATLAB? It would be great if we could continue to use it after R2013b.

#18 Comment By Yair Altman On September 7, 2013 @ 21:58

@Razvan – unfortunately, unlike earlier Matlab releases, R2013b relies on Java 7. One of the changes that Oracle made in Java 7 was to prevent modification of an open window’s transparency, which was possible in Java 6. As consequence, setFigTransparency, blurFigure and any other Matlab utility that relies on this feature no longer work. AFAIK there is no fix for this.


Article printed from Undocumented Matlab: https://undocumentedmatlab.com

URL to article: https://undocumentedmatlab.com/articles/transparent-matlab-figure-window

URLs in this post:

[1] example: https://www.mathworks.com/matlabcentral/newsreader/view_thread/280394

[2] MUtilities: http://www.mathworks.com/matlabcentral/fileexchange/28326-mutilities

[3] last week’s: http://undocumentedmatlab.com/blog/disable-entire-figure-window/

[4] setWindowOpacity method: http://www.pushing-pixels.org/2008/02/27/translucent-and-shaped-windows-in-core-java.html

[5] Image: https://undocumentedmatlab.com/images/setFigTransparency.png

[6] other GUI effects: http://today.java.net/pub/a/today/2008/03/18/translucent-and-shaped-swing-windows.html

[7] gradients: http://java.sun.com/developer/technicalArticles/GUI/translucent_shaped_windows/

[8] setFigTransparency utility: http://www.mathworks.com/matlabcentral/fileexchange/30583-setfigtransparency-set-figure-transparency-fading

[9] here: http://java.com/en/download/manual.jsp

[10] here: http://www.mathworks.com/support/solutions/en/data/1-1812J/

[11] migrated: http://www.pushing-pixels.org/2009/05/03/translucent-and-shaped-windows-in-jdk-7.html

[12] post a comment: http://undocumentedmatlab.com/blog/transparent-matlab-figure-window/#respond

[13] Android: http://blogs.mathworks.com/desktop/2010/05/24/introducing-matlab-mobile-%E2%80%93-an-iphone-app-to-connect-remotely-to-your-matlab/#comments

[14] email: mailto:%20altmany%20@gmail.com?subject=Undocumented%20Matlab&body=Hi%20Yair,%20&cc=;&bcc=

[15] Blurred Matlab figure window : https://undocumentedmatlab.com/articles/blurred-matlab-figure-window

[16] Enable/disable entire figure window : https://undocumentedmatlab.com/articles/disable-entire-figure-window

[17] Figure window customizations : https://undocumentedmatlab.com/articles/figure-window-customizations

[18] Minimize/maximize figure window : https://undocumentedmatlab.com/articles/minimize-maximize-figure-window

[19] Detecting window focus events : https://undocumentedmatlab.com/articles/detecting-window-focus-events

[20] Frameless (undecorated) figure windows : https://undocumentedmatlab.com/articles/frameless-undecorated-figure-windows

[21] : http://www.mathworks.com/matlabcentral/fileexchange/30666-blurfigure-blurs-and-prevents-interaction-on-a-figure-window

[22] : https://undocumentedmatlab.com/blog/blurred-matlab-figure-window/

[23] : https://www.mathworks.com/matlabcentral/newsreader/view_thread/268556#702864

[24] : https://undocumentedmatlab.com/blog/hg2-update/#observations

Copyright © Yair Altman - Undocumented Matlab. All rights reserved.