Calculate screen coordinates for remote desktop application - java

I am currently developing a remote desktop application in Java, but i am stuck on calculating the screen coordinates for remote mouse clicking.
For example, the remote computer has a resolution of 1024x768, but the panel that renders the image and handles the clicks is only 800x600.How can I calculate the coordinates, so when I press my panel in the upper right corner, it also clicks on the remote computer there?
I tried
x = clickedX / (remoteX/clickedX)
and
x = clickedX * (remoteX/clickedX)
but it never seemed to work.
I appreciate your help.

If I properly understand what you want to do, you are scaling your targets resolution up/down displaying a smaller/bigger image of the remote desktop.
So you need to calculate the transformation with the scale factor you use.
int targetWidth = 1024;
int targetHeight = 768;
int myWidth = 800;
int myHeight = 600;
double scaleX = targetWidth/myWidth;
double scaleY = targetHeight/myHeight;
double targetMouseX = myMouseX * scaleX; //
double targetMouseY = myMouseY * scaleY; //
with targetMouseX & Y as your "output" and myMouseX&Y as your inputs, e.g. your mouse event.

Related

Android Button different position on other devices (using setX)

I want to move several buttons, rearranging them in a different position than the original one at a specific moment, while playing the app. I used the setX() method for this purpose, to move the button to the place I want. I know that this method takes pixels as imput (and pixels depend on the density of the device), so I took the density of the device and multiplied it by a certain number (the position in density pixels), so the output is that position in pixels for each device. I thought that would give me the same button position for all devices, but it doesn´t work. The buttons appear displaced on different devices. This is the method I used to convert density pixels to the corresponding pixels for each device:
public void Converter_Dp_to_Px(){
pxX = (int) (dpX * Resources.getSystem().getDisplayMetrics().density); //Pixels in X direction
pxY = (int) (dpY * Resources.getSystem().getDisplayMetrics().density); //Pixels in Y direction
}
Now I set values for dpX and dpY, convert them into pixels for each device, and place the button in that position with setX() and setY() methods:
dpX = 254;
Converter_Dp_to_Px();
dpY = 477;
Converter_Dp_to_Px();
button1.setX(pxX);
button1.setY(pxY);
I also tried not with absolute position, but with one with percentages, as follows:
int maxX = Resources.getSystem().getDisplayMetrics().heightPixels;
int maxY = Resources.getSystem().getDisplayMetrics().widthPixels;
mov_percenX = 0.37f;
mov_percenY = 0.63f;
button1.setX(button1.getX() + maxX * mov_percenX);
button1.setY(button1.getY() + maxY * mov_percenY);
But it doesn´t work anyway. I hope you can help me, thanks in advance.

What is the X and Y axis format on an iPhone screen/Android screen?

I've been researching the X and Y axis set up on an iPhone screen because I'm trying to use Appium to run automated tests on an application. Part of what I'm working on is swiping but I'm running into issues with moving points, X and Y points.
I don't know what the limits of the X and Y axis are on the iPhone screen or the Android screen. Due to that I'm not sure sure where my cursor is swiping from or to.
So far I've been able to do one swipe via the code below,
TouchAction action1 = new TouchAction((PerformsTouchActions) driver);
action1.press(PointOption.point(250,200))
.waitAction(new WaitOptions().withDuration(Duration.ofMillis(250))) //you can change wait durations as per your requirement
.moveTo(PointOption.point(50, 250))
.release()
.perform();
Thread.sleep(5000);
This runs one successful swipe to the left but the 2nd swipe, which runs successfully, but doesn't swipe the screen to the left. As in, the swipes it, but it doesn't move it past the required threshold to move it to the next page.
Let it be known that the above code is placed within a for loop as shown below.
for(int i = 0; i <= 3; i++){
TouchAction action1 = new TouchAction((PerformsTouchActions) driver);
action1.press(PointOption.point(250,200))
.waitAction(new WaitOptions().withDuration(Duration.ofMillis(250))) //you can change wait durations as per your requirement
.moveTo(PointOption.point(50, 250))
.release()
.perform();
Thread.sleep(5000);
}
Is there any documentation for iPhones or Androids that shows the set up of the X and Y axis, the limitations and so on etc
Edit:
The first piece of code is placed outside of the for loop. I believe that was the piece of code that swiped the screen properly. Once the for loop is entered, the screen is unable to swipe properly. I believe I'm not using the correct syntax in the for loop.
If anyone has any advice on how to properly incorporate the swipe feature into the for that would be of great help.
X, Y coordinate depend on the device/emulator you are using for
automation if device has large display X,Y value will be high.
I will recommend never use hardcoded value of X, Y during swipe or scrolling with the help of coordinate as it can throw exception points are out of device whenever you will be shifted on small screen Device .
Please have a look how you can get dynamic X,Y value with the help of Device’s height and width while scrolling to bottom
public void scrollToBottom() {
// we are scrolling to bottom so X will be constant so we are taking mid point in width.
int x = driver.manage().window().getSize().width / 2;
// starting of Y is from 20% of height as we have one bar in all device for showing network and battery status
int start_y = (int) (driver.manage().window().getSize().height * 0.2);
// end of Y is 80% of height
int end_y = (int) (driver.manage().window().getSize().height * 0.8);
// here scrolling length is (80% -20%) 60%.
TouchAction dragNDrop = new TouchAction(driver)
.press(PointOption.point(x,start_y)).waitAction(WaitOptions.waitOptions(Duration.ofMillis(500)))
.moveTo(PointOption.point(x, end_y))
.release();
dragNDrop.perform();
}
If you want to get X,Y coordinate of any Mobile Element you can get as mentioned below
MobileElement element = (MobileElement) driver.findElementByAccessibilityId("SomeAccessibilityID");
Point location = element.getLocation();
OR
int leftX = element.getLocation().getX();
int rightX = leftX + element.getSize().getWidth();
int middleX = (rightX + leftX) / 2;
int upperY = element.getLocation().getY();
int lowerY = upperY+element.getSize().getHeight();
int middleY = (upperY + lowerY) / 2;

How to ensure application window is visible at launch?

I have an application that is usually run on a dual-monitor setup. I also save the currently size and position of the window when it exits and load it again when launching the app.
My problem comes from situations where one of the monitors is either removed or the resolution changed. If I save the x and y position of the window and it was last visible on the now-absent monitor, it is out of view when the program is launched again.
Here is the code I use to in my Main.java:
double x = Settings.getWindowX();
double y = Settings.getWindowY();
double h = Settings.getWindowH();
double w = Settings.getWindowW();
primaryStage.setScene(mainScene);
primaryStage.setX(x);
primaryStage.setY(y);
primaryStage.setWidth(w);
primaryStage.setHeight(h);
My goal is to check if the window is within the visible boundaries of the available monitors and, if not, reset the x, y to 100, 100.
I'm not sure where to start with this.
The Screen API allows you to check the values you have are within the bounds of the current configuration.
E.g. to test if there is a physical graphics device that intersects the bounds saved in the configuration settings, you could do:
double x = Settings.getWindowX();
double y = Settings.getWindowY();
double h = Settings.getWindowH();
double w = Settings.getWindowW();
primaryStage.setScene(mainScene);
if (Screen.getScreensForRectangle(x, y, w, h).isEmpty()) {
// no screen intersects saved values...
// just center on primary screen:
primaryStage.centerOnScreen();
} else {
primaryStage.setX(x);
primaryStage.setY(y);
primaryStage.setWidth(w);
primaryStage.setHeight(h);
}

JavaFX center view on cursor position

I created my first JavaFX app that displays images. I want it to zoom the image to full size on mouse down (centered on cursor position) and to refit the image on mouse up.
All is working fine but the i don't know how to center on cursor position. My zoom method looks like that at the moment:
private void zoom100(double cursorx, double cursory){
double centery = imageView.getLayoutBounds().getHeight()/2;
double centerx = imageView.getLayoutBounds().getWidth()/2;
imageView.setFitHeight(-1); //zooms height to 100%
imageView.setFitWidth(-1); //zooms width to 100%
imageView.setTranslateX(centerx-cursorx); //moves x
imageView.setTranslateY(centery-cursory); //moves y
}
My idea is to set an offset with translate. I am not sure if translate is the correct approach. If it is the correct approach how to calculate the correct values? (centerx-cursorx) is wrong!
I uploaded the code here: https://github.com/dermoritz/FastImageViewer (it is working now - see my answer)
I didn't test it but I think the order is wrong. you calculate the center before you set the fit size. By setting the fit size your image view will change it's preferred size. therefore the center point won't match anymore.
I found a solution, but i am still not sure if this is a good way to go:
private void zoom100(double x, double y) {
double oldHeight = imageView.getBoundsInLocal().getHeight();
double oldWidth = imageView.getBoundsInLocal().getWidth();
boolean heightLarger = oldHeight>oldWidth;
imageView.setFitHeight(-1);
imageView.setFitWidth(-1);
//calculate scale factor
double scale=1;
if(heightLarger){
scale = imageView.getBoundsInLocal().getHeight()/oldHeight;
}else {
scale = imageView.getBoundsInLocal().getWidth()/oldWidth;
}
double centery = root.getLayoutBounds().getHeight() / 2;
double centerx = root.getLayoutBounds().getWidth() / 2;
double xOffset = scale * (centerx - x);
double yOffset = scale *(centery - y);
imageView.setTranslateX(xOffset);
imageView.setTranslateY(yOffset);
}
The picture must be centered within "root". I think there should be something more direct - only using properties of imageview?!
Meanwhile the app works using this code: https://github.com/dermoritz/FastImageViewer

How to match user click and the sprite object position in libGDX framework

I am using libGDX java framework for developing a practice game in Eclipse.
My game is in landscape mode and I am using sprite image for game assets .Actually i am trying to follow the kilobolt ZombieBird tutorial
I have set orthographic camera like this -- >
cam = new OrthographicCamera();
cam.setToOrtho(true, 250, 120);
I have done this because my background texture region is of 250 x 120 px in the sprite image.
So basically my sprite image is small in size and it is getting scaled according to the device but all the computing is done relative to 250 x 140 px like for changing the position of the object i have defined Vector2 position = new Vector2(x, y); and if i write position.x = 260; the sprite will go outside the screen even if my device width is 500px .
Problem :
Now i have to make the moving sprite vanish when someone clicks on it (just imagine zombies moving around and if i click on them they die) .So i am using the following code for matching user click co-ords with the object co-ords.
int x1 = Gdx.input.getX();
int y1 = Gdx.input.getY();
if(position.x == x1 && position.y == y1){
// do something that vanish the object clicked
}
The problem is position.x and position.y returns the co-ords relative to the ortho cam width and height which is 250x120 px and the click co-ords are relative to the device width and height which maybe anything according to the device. Because of this even if i click right on the object the click co-ords and the object position co-ords have a huge difference in their values.So i would never get matching values .
Is there any solution for this or am i doing it wrong ?
You have to unproject the device coordinates using the camera. The camera has a built in function to do this, so it's fairly simple. Furthermore, to determine if the sprite is clicked, you have to check to see if the point clicked is anywhere inside the sprite, not just equal to the sprite's position. Do something like this:
int x1 = Gdx.input.getX();
int y1 = Gdx.input.getY();
Vector3 input = new Vector3(x1, y1, 0);
cam.unproject(input);
//Now you can use input.x and input.y, as opposed to x1 and y1, to determine if the moving
//sprite has been clicked
if(sprite.getBoundingRectange().contains(input.x, input.y)) {
//Do whatever you want to do with the sprite when clicked
}
As an alternative to answer by kabb, you can just use math to convert screen co-ordinates to cam co-ordinates:
//Example:
float ScreenWidth = Gdx.graphics.getWidth();
float ScreenHeight = Gdx.graphics.getHeight();
// on a 1080p screen this would return ScreenWidth = 1080, ScreenHeight = 1920;
//now you get the screen co-ordinates and convert them to cam co-ordinates:
float x1 = Gdx.input.getX();
float y1 = Gdx.input.getY();
float x1cam = (x1/ScreenWidth)*CamWidth
float y1cam = (y1/ScreenHeight)*CamHeight
// now you can use the if statement in kabbs answer

Categories