Removing a panel without affecting buttons and axis

16 次查看(过去 30 天)
I'm making a game with a group and it currently plays well but looks horrendous, i'm trying to neaten it up and make it look better by adding a background image, however the guy who made the code has a panel behind all the axis and buttons. How can I remove this without affecting the axis and buttons, or is there a way to make it transparent?
Thanks

回答(2 个)

Yair Altman
Yair Altman 2018-1-10
Here is an undocumented way to set the panel's background to transparent - this works on HG2 (R2014b onward):
jPanel = hPanel.JavaFrame.getPrintableComponent; % hPanel is the Matlab handle to the uipanel
jPanel.setOpaque(0);
jPanel.getComponent(0).setOpaque(0);
jPanel.getParent.setOpaque(0);
jPanel.repaint
In HG1 (R2014a or earlier) you can simply set the hPanel's BackgroundColor to 'none'
Yair Altman
  3 个评论
Walter Roberson
Walter Roberson 2018-4-24

It works for me, showing whatever is underneath.

>> imshow('cameraman.tif')
>> hPanel = uipanel('units','norm','position',[0 0 .5 .5])
hPanel = 
  Panel with properties:
                Title: ''
      BackgroundColor: [0.94 0.94 0.94]
             Position: [0 0 0.5 0.5]
                Units: 'normalized'
    Show all properties
  >> jPanel = hPanel.JavaFrame.getPrintableComponent;  % hPanel is the Matlab handle to the uipanel
  jPanel.setOpaque(0);
  jPanel.getComponent(0).setOpaque(0);
  jPanel.getParent.setOpaque(0);
  jPanel.repaint
Jonas
Jonas 2023-5-8
in 2022a the JavaFrame returns an empty object. how to do this in 2022a?

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2017-12-20
The uipanel can be set to 'Color', 'none' to make it transparent.
It is often very convenient to use a uipanel to position buttons and axes properly, so removing the uipanel would not be my first choice.
  3 个评论
Walter Roberson
Walter Roberson 2018-1-6
You appear to be correct on both points.
Also, as is the undocumented case with several of the HG2 color, you can set the BackGround color to a 4 element vector instead of a 3 element vector. In some controls, the 4th element is alpha value. However for uipanel the 4th element is accepted but has no effect.
The implementation appears to involve a java frame of class com.mathworks.hg.peer.ui.UIPanelPeer which does not appear to support alpha values. Or rather, you can set the alpha component and it will remember it if you know how to ask for it, but it does not appear to use the alpha.
Yair Altman
Yair Altman 2018-1-10
In HG1 (up to R2014a) you could set BackgroundColor to 'none'; in HG2 (R2014b or newer) this is no longer possible and you have to use the underlying Java component. See my answer for details.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by