如何在JFrame中获取轴框的边界?

时间:2016-09-13 09:40:30

标签: java matlab swing

我在previous question上得到了答案。但是,要获得这些坐标(axis equal的情况下轴箱的大小和位置),我们需要进行一些相对繁琐的计算。但是,由于MATLAB图形基于Java,我们可以访问Java属性。 我发现如果我们在MATLAB中使用它:

jFrame = get(gcf,'JavaFrame');
BoxHeight = jFrame.getAxisComponent.getHeight;
BoxWidth = jFrame.getAxisComponent.getWidth;

我们可以获得图形窗口的宽度和高度(也许这可以与轴的组件相关联)。但在像素单位的情况下,它们与get(gcf, 'Position')get(gca, 'Position')的值不同。我不是Java专家(我也试图使用Altman findjobj找到这些属性,但未成功。)

因此,我有两个问题:

  1. 为什么从MATLAB和Java的属性派生的图窗口的高度和宽度(也许这是轴框)不一样(至少,它们是Java中的整数和MATLAB中的浮点数)?
  2. 例如:

    hf=figure('units','pixels'); ha=gca(hf);
    set(ha,'units','pixels');
    get(hf,'position')
    get(ha,'position')
    

    ans =

    488   342   560   420
    73.8000   47.2000  434.0000  342.3000`
    

    BoxHeight=525BoxWidth=700和转变始终为零(alignmentX=0.0alignmentY=0.0)。

    1. 如何在MATLAB 中使用Java 绘制窗轴的确切大小和位置?

1 个答案:

答案 0 :(得分:1)

在MATLAB中,axesPosition都有一个名为get(hf,'position')的属性,但它不是一回事:

get(ha,'position')将为您提供position of the figure window on screen hf=figure('units','pixels'); ha=gca(hf); set(ha,'units','pixels'); hfPos = get(hf,'position') haPos = get(ha,'position') 将返回position of the axes within the figure window

因此,代码的输出

hfPos = 520   378   560   420
haPos = 73.8000   47.2000  434.0000  342.3000

ans =

get(ha,'position')

必须像这样解释: this

但是,我不知道为什么get(gcf,'JavaFrame'); Warning: figure JavaFrame property will be obsoleted in a future release. For more information see the JavaFrame resource on the MathWorks web site. 会返回浮点数。

在我的系统上(Win7 Pro,MATLAB R2016a,Java 1.7.0_60-b19),以下代码发出警告:

None

因此我不会依赖它。相反,我会使用上面的信息来获得在MATLAB中绘制的轴的大小和位置。