Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to obtain the bounds of the axis box in a JFrame?

Tags:

java

swing

matlab

I got the answer on my previous question. However, to get these coordinates (size and position of the axis box in the case of axis equal) we need to do some relatively cumbersome calculations. However, since MATLAB graphics is based on Java, we can get access to Java properties. I have found that if we use this in MATLAB:

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

we can obtain a width and height of figure windows (maybe this can be associated with the components of the axis). But they are differ from the value of get(gcf, 'Position') or get(gca, 'Position') in the case of pixel units. I'm not an expert in Java (I also unsuccessfully tried to find these properties using Altman's findjobj).

Thus, I have two questions:

  1. Why the height and width of the figure window (maybe this is the axis box) derived from the properties of MATLAB and Java are not the same (at least, they are integers in Java and floating-point numbers in MATLAB)?

For example:

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`

whereas BoxHeight=525, BoxWidth=700 and the shift is always zero (alignmentX=0.0 and alignmentY=0.0).

  1. How to get the exact size and position of the window axes plotted in MATLAB using Java?
like image 412
Alexander Korovin Avatar asked Sep 13 '16 09:09

Alexander Korovin


1 Answers

In MATLAB, both a figure and an axes have a property called Position, but it's not the same thing:

get(hf,'position') will give you the position of the figure window on screen.
get(ha,'position') will return the position of the axes within the figure window.

Therefore, the output of the code

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

ans =

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

has to be interpreted like this: this

However, I have no idea why get(ha,'position') returns floating point numbers.

On my system (Win7 Pro, MATLAB R2016a, Java 1.7.0_60-b19) the following code issues a warning:

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. 

Therefore I would not rely on using it. Instead, I would use the information above to obtain the size and position of the axes plotted in MATLAB.

like image 171
Martin S Avatar answered Nov 10 '22 13:11

Martin S