Detecting Screen Orientation and Angle

I can easily detect the screen orientation by doing the following:


int height = GraphicsEnvironment.getDefaultScreenDevice().getDisplayMode().getHeight();
int width  = GraphicsEnvironment.getDefaultScreenDevice().getDisplayMode().getWidth();
		
String orientation = height/width >= 1? "Portrait" : "Landscape";

The problem is that I can’t know if the screen has been turned 90º, -90º or 180º.

I came unto this problem testing fullscreen mode on my portrait-oriented monitor, and finding out that all the input (mouse) is rotated. Knowing the angle is necessary thus to filter the input correctly.

From what I’ve seeing googling around, mobile platforms like Android have an actual .getRotation() method. How can I check this on standard Java?

Library independence is appreciated if possible :wink:

You can’t. Most peoples desktops don’t rotate. Hardware like the accelerometer and tilt sensor are only ever going to be exposed in specialized kits like Android or J2ME, and frankly, not so much J2ME.

You could also do this on android:


getResources().getConfiguration().orientation

for rotation:


int rot =((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation();

there might be a shorter way xD

For Desktop you should never think that someone flips his monitor xD How should the monitor check this? You only need the code for Android and even there… just cap the view to portrait and landscape like so:


android:screenOrientation="portrait"/"landscape"

(In the AndroidMenifest.xml file)

The monitor itself doesn’t check it, but the display drivers do, as many Graphic cards give the option to rotate the display (not all, and not all the angles).

In my case I work with a rotated monitor, and happen to have a convertible laptop/tablet, so yeah, it might be of marginal interest, but since I’m building the screen management infrastructure for my project, I’d like to investigate possibilities now.

To the very least, having a rotation stub will help if I port the engine to Android or similar.

http://img99.imageshack.us/img99/3109/tatesetup.jpg

It’s client problem. They can easily turn the rotation for their monitor. ctrl + alt + arrow does that for me.

Why need to rotate anyway? :cranky:

@Cero
Those are all vertical shooters… Do you have any other examples? If not then you still have 98% of people playing games without rotating there monitors and without games that expect that. Or does… uhm… Deus Ex check if you flip you monitor and react to it?

Oskuro
If you use libGDX you won’t havem uch problems porting it to android anyway. And even there… I don’t remember much games doing that. Most games a portrait or landscape and if I flip my device nothing happens :confused:
Do you create are game where it is really necessary?

It’s not a matter of actual application to a game, but rather curiosity, as the issue has come up while I was testing my screen manager code.

Still, this would be essential in mobile devices, I’ve experienced quite a few times games on tablets that do not rotate as I physically rotate the device, even though the API does offer the information (Since other games do rotate), and it’s a bit obnoxious to have the user hold the device in an specific orientation, when the whole idea of a mobile device is ease of use.

But again, not concerned with what use I’d give to it. I just enjoy a lot solving programming problems :slight_smile:

Portrait orientation on desktop (i.e. flipped monitors) is all the rave these days – lots of people in my office have one or both of their monitors rotated. Good for programming, heavy reading, Reddit, etc.

I know, I do precisely that (That’s why I noticed the issue, I ran my fullscreen code on my work’s computer).

I’m investigating. To the very least, I’ll probably add a “rotate” button so the user can manually adjust the screen if needed, but having some autodetection would be nice.

I know all about rotating monitors, my last one had that trick too. Fact remains that there’s still no standard Java API to deal with it, certainly not in the J2SE JDK.

Possibly with LWJGL you can poll for resolution changes, though I tend to doubt Display deals with that gracefully. Maybe JOGL does?

The thing is that the API for rotation details isn’t well defined. It depends heavily on the display drivers and how much information they rely to the system, if they have the rotation capability at all.

I actually had a fun time coding scripts for screen rotation in Ubuntu (It wouldn’t auto-detect screen rotation on my combo laptop/tablet, so I do it manually), so this isn’t exactly new… But I doubt there’s an easy OS agnostic solution.

I kind of suspect that in future JDKs this might pop up. Ginourmous monitors that get rotated and graphics cards with easy rotation interfaces are increasingly common these days, hopefully these things will get standarized.

My thinking is: so what? I mean do you really want to interactively rotate your monitor? If not…put an option in your graphics configuration options screen and stick a fork in issue until someone solves the problem for you.

Ok, I don’t want to be rude, but this is something I often see in programming threads when someone asks something, so let me rant for a moment…

I’m not pondering if I should do it, I’m pondering how to do it.

I appreciate the discussion about the relevance of the issue at hand, but when it has been said a couple times that I’m interested in attempting to solve this just for the sake of doing it, rather than for any practical purpose, it becomes extremely obnoxious to have people keep insisting about whether I should or shouldn’t do it.

Ok, rant over.

Well, I’m very unsure if screens have auto- rotate, except for the tablet devices.

However, there might be a temporary hack you can use in the meantime until a better solution can be found. The only information readily available continuously is the Java Toolkit screen size. You might be able to get away with it by comparing the Dimensions.


...
    Toolkit toolkit = Toolkit.getDefaultToolkit();
    Dimension scrnsize = toolkit.getScreenSize();
    if(scrnsize.getWidth() > scrnsize.getHeight())
    {
         // Do normal x-axis mouse
    }
    else
    {
        //  Do inverted y-axis mouse
    }     
...

Of course, this comparison will fail if the width and the height is the same, but then, you can always have the user just set an option for it. Hopefully, this helps a little bit…

I have 1 monitor that is rotated +90deg and one monitor rotated -90deg.

I have never had problems with ‘rotated’ mouse coordinates in AWT. (as you’re posting this in the Java2D board)

Client OS automatically handles everything for you. Nothing to see here. All you need to know is the width and height of the display. It is not a case of what you “should” do or “can” do, there is simply nothing to do.

Cas :slight_smile:

Your original question was this:

So I’ll just go back to my original answer: You Can’t.

I qualified my statement with “My thinking is…”. What I see alot in various programming forums is people insisting on wasting time on things that are pointless. The subsystem they’re describing has no added value and won’t provide any meaningful learning experience to the person in question. I’m not gonna beat people over the head, but I think it useful to point out that IHMO what they are attempt to do is a waste of time.