Java - How to detect the vertical displacement of some multimonitor setups? - java

In many multimonitor setups featuring monitors of varying stand heights, the vertical positions of the monitors can be adjusted in software so that their graphics align correctly. A vertical displacement is added to the Windows mouse coordinates to give this effect; shown in purple in the diagram:
I am building a program that needs to know when the user's mouse has reached the top of any of their monitors. I tried using MouseInfo.getPointerInfo().getLocation().y==0, but this will not work when the monitors have been displaced in software because the top of the monitor may not always be zero.
Is there a reliable and efficient way to identify this offset across multiple displays?

Thanks to #Siguza's comment, I was able to do what I was trying to accomplish. Simply put, you can find the Rectangle object for the monitor that the mouse is on by using this code: MouseInfo.getPointerInfo().getDevice().getDefaultConfiguration().getBounds()
The Rectangle can also be found for each monitor using the monitor's numerical index within Windows. The code GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[0].getDefaultConfiguration().getBounds() will return the Rectangle for the main monitor (index 0), other monitors can be accessed by changing the index of the GraphicsDevice
The vertical displacement of the monitor is in the .y property of the Rectangle object; this value can be used to identify the topmost coordinate of the monitor.
#Siguza's Comment:
Haven't tested it, but according to the docs, GraphicsConfiguration.getBounds() sounds like what you're looking for. You should be able to get all monitors with GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenD‌​evices(), and their configurations with .getConfigurations(), but I idk how to choose from that array.

Related

Is there any possibilities to get coordinates of the specific area on the background image?

So i am new to libgdx and i am thinking about making 2D quest. I want to know how to interact with the objects on the backround image. For example, i have this location
Is it possible to get the window's coordinates so that if the player is next to it (I want to check the interaction possibilities by the player's coordinates and the interactive object's coordinates), he can press "E" or do something like that?
I was thinking about putting certain images in the background, but I don't know if that's a good idea. I also saw something about TextureRegion but I don't think it can help me because I still don't know how to get the coordinates of a specific area in the background.
Colour code the objects. From the appearance of your game its retro adventure so you are using a restricted palette. A restricted palette has advantages! So if you have windows with two colours, doors with 2 different and separate colours, objects with 2 different separate colours, then you can check when the player gets near a window, or whichever, by seeing what active colours are present on a radius around the player location.
So e.g. the floor and walls are coloured of course but they never trigger anything. This does limit you to one window per room, or one type of object per room (unless you introduce a new window colour pair to distinguish them). The code would be just sample all the pixels in the area around the player maybe a rectangle would be better.

How does default location work? / What is the default monitor?

I'm working on some app that gets the users width for their multiple monitors, assume they don't have any monitors on top, and then provide them with multiple buttons/frames, so that they can black out whichever monitors they wish.
MY ISSUE
All's well and good, apart from location compatability stuff.
If someone has 4 1920 width monitors, how can I position my reference/starting frames at the leftest monitor, so that I can say "proceed 1920 pixels, make a frame, ..."?
You'd think "Use setLocationtoNull, but how does that work?? You'd think it's simply the middle monitor, length/2; but what is the middle monitor!? It can't be totalScreensLength/2, because that'd mean the program is split in half between the monitors! (so it's most likely the middle of the main manitor).
And not using any setLocations, the program is at the top left of the main monitor.... BUT WHAT IS THE MAIN MONITOR??
If I knew how java figured which monitor was "main", that would solve my issue (and no, knowing merely the main monitors resolution via Toolkit.getScreenSize() would not be enough info).
//creating invis black frames
for(int c=0;c<inArr.length;c++){
int xLoc=0;
for(int i=0;i<c;i++){
/**/ xLoc+=resList[i];
}System.out.println(xLoc);
JFrame blackOut=new JFrame();
blkList.add(blackOut);
blackOut.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
blackOut.getContentPane().setBackground(Color.BLACK);
blackOut.setUndecorated(true);
blackOut.setSize(resList[c],5000);
/**/blackOut.setLocation(xLoc-1280,-2000);
blackOut.setVisible(false);
}
This is the code that makes the program work, because it's hardcoded for my setup of 1280,1920,1440.
The issue is at blackOut.setLocation(xLoc-1280,-2000); I must know where exactly the starting frame would be, so I can move the frame to the left, so 'scaling' works. This is what's happening here:
the frame starts at the top left of my middle, 1920 monitor, so I move the frame 1280 pixels left so it is at the top left of my leftest monitor.
Sorry if this is too verbose, just want to give enough detail.
Also, recently discovered this and went into its documentation, but I don't understand how I could use the data, and am unsure if it's able to provide me the information necessary (know what monitor is main, and be able to differentiate monitors, even if they're the same resolution)
This might be a partial answer.
From the linked discovery of yours, you can get the default configuration and bounds of each device by calling graphicsDevice.getDefaultConfiguration().getBounds(). This includes the coordinates / offset of each screen as well as the individual screen size.
In the implementation for GraphicsEnvironment.getScreenDevices() by SunGraphicsEnvironment screen devices are created by iterating over the number of screens and creating a device for each index - the index is passed to each device.

JUNG edge label and shape

Is it possible to change the edge shape in JUNG? For example, I would like to have the edge change it's color gradually in a way similar to a progress bar. What about the edge label font size?
Thanks.
Yes, sort of... Also - I'm not sure which version of JUNG you're using, but this works in the latest JUNG 2 release (I realize JUNG 3 might be under development currently, but last time I checked, it wasn't stable enough to be used for production-level code).
1. Labelling: First, you need to implement the Transformer<EdgeType,Font> interface that converts your edge instances into Font instances. Then call [VisualizationViewer instance].getRenderContext().setEdgeFontTransformer([Transformer<EdgeType,Font> instance]).
2. Color/Stroke Customization: This is a little trickier, because the only way you can have this change color gradually (that I am aware of) is by creating a Transformer<EdgeType,Paint> that returns different paints for edge type instances over time. There are several transformers used for edges - these control the draw, the fill, and the Stroke, and have similar method names like the one mentioned for the labeller in step 1. You will either need to control when the graph panel repaints manually or ensure that JUNG's animation renderer is turned on so that repaints happen continuously.

Cropping out whitespace from a user drawn image

A portion of my app involves the user drawing images that will be later strung together in a PDF. The user is free to use the entire screen to draw. Once the user is done drawing, I'd like to trim off all of the white space before adding the image to a PDF. This is where I am having problems. I've thought of two different methods to determine the location of trimmable white space and both seem clumsy.
My first thought was having the motion event of the stylus record if the event has gone outside of the box so far. If it has, I would expand the box to accommodate this. Unfortunately I could see polling every time there is a motion event being bad for performance. I can't just look at up and down events because the user could draw something like the letter V.
Then I thought I could look at all the pixels (using getPixel()) and see where the highest, lowest, rightmost and leftmost black pixels are. Again this seems like a really inefficient way to find the box. I'm sure I could skip some pixels to improve performance, but I can't skip too many.
Is there a standard way of doing what I want to do? I haven't been able to find anything.
You can inside your editor, where you record that this pixel has been drawn upon, update the maximum and minimum X and Y, and then use them later to crop the image.
If the user is drawing, aren't you already handling the onTouchEvent callback in order to capture the drawing events? If so, it shouldn't be a big deal to keep a minX, maxX, minY and maxY and check each recorded drawing event against these values.

Java Swing; How do I make it so the program starts on the right most hand of the screen?

By default, my Swing program starts at the top left hand side of the monitor. Is there a way to make it so it pops up at the right hand side?
What about dual monitors? Can I get it to pop on the right monitor?
Thanks.
Carlo
There are many questions/answers about using GraphicsEnvironment to do similar things.
You can use GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices() to get all the monitors, then call GraphicsDevice#getConfigurations and then GraphicsConfiguration#getBounds to get the size and location of each monitor, then use your high-school geometry to find the "right" monitor. (Note that a true multi-monitor setup can put them in arbitrary location.)
Assuming you don't want to cover up the taskbar, you should use getMaximumWindowBounds.
Java API
http://www.javabeginner.com/java-swing/java-jframe-class-example
Centering JFrame’s
By default, a Jframe is displayed in
the upper-left corner of the screen.
To display a frame at a specified
location, you can use the
setLocation(x, y) method in the JFrame
class. This method places the
upper-left corner of a frame at
location (x, y).
Your pseudo code for the top right corner looks something like this:
yourJFrame.setLocation(
GraphicsEnvironment.getMaximumWindowBounds().getWidth() -
yourJFrame.getWidth(), 0);
For displaying at another initial location, take a look at JFrame.setLocation()
For displaying on another screen, take a look at avaible infos from GraphicsEnvironment.getScreenDevices().

Categories

Resources