0

I'm trying to take a window screenshot to use that as background in the application.

This is my code:

try {
    Robot robot = new Robot();
    Rectangle captureSize = new Rectangle(new MainWindow().getX(), new MainWindow().getY(), MainWindow.getWIDTH(), MainWindow.getHEIGHT());
    RenderManager.backgroundGUIs = robot.createScreenCapture(captureSize);
    GUIManager.ThisGui = new GUI("inventory", null, false);
} catch(AWTException e) {
    System.err.println("Error taking screenshot!");
}

And these are the MainWindow().getY() and MainWindow().getX() methods:

public int getX() {
    return (int) frame.getLocationOnScreen().getX();
}

public int getY() {
    return frame.getY();
}

Now... it works fine, but there is a problem. frame.getLocationOnScreen().getX() and frame.getX() return the location with the window border, that hasn't to be in the screensot. Ok, i can manually calculate border size to subtract it, but from Windows 7 to Windows 8, from Windows 8 to Mac, etc. window border changes.

So... is there a method to get frame position or window border size to calculate what i need?

Thank you for your time and your answer!

Ilya Rochev
  • 157
  • 1
  • 3
  • 13
pianka
  • 140
  • 1
  • 2
  • 14
  • Are you just trying to create a transparent window? Most recent versions of java have this capability without the need for a screenshot: https://docs.oracle.com/javase/tutorial/uiswing/misc/trans_shaped_windows.html – copeg Apr 29 '15 at 18:05

4 Answers4

1

As simple as this:

x = frame.getRootPane().getX();

As well, you can make all other calculations according to the root pane, just dropping the frame's.

As expected, the root pane is the top-level container inside any Frame/Window/Dialog etc. It includes the menubar too.

Mordechai
  • 15,437
  • 2
  • 41
  • 82
  • Thank you, i just tried it and it works fine for the little border that is on the left, the bottom and the right of the window, but the top border is higher (there are title, close button...). How can i get that size? – pianka Apr 29 '15 at 18:12
  • @Mouse does this SO's accepted answer solve OP's dilemma? They'd like the menu bar omitted. http://stackoverflow.com/questions/7154633/jframe-getting-actual-content-size – CubeJockey Apr 29 '15 at 18:15
  • @Trobbins From the [JavaDoc](http://docs.oracle.com/javase/7/docs/api/java/awt/Container.html#getInsets()) it seems that the menubar is not omitted by checking insets. But i'm not sure. – Mordechai Apr 29 '15 at 18:20
  • Could OP isolate the top inset and run some subtraction to get the size of the core window? Not sure myself. – CubeJockey Apr 29 '15 at 18:23
  • @Trobbins explain yourself better. What do you mean "core window", with the border, but without titlebar? – Mordechai Apr 29 '15 at 18:28
  • I know as much as you do. I'm inferring OP would like only the contents of the window. Meaning, no title bar, no borders. – CubeJockey Apr 29 '15 at 18:31
0

Use this

frame.getContentPane().getSize();
MCHAppy
  • 1,012
  • 9
  • 15
  • I also checked that SO post (http://stackoverflow.com/questions/5097301/jframe-get-size-without-borders) and I think @MouseEvent 's answer is a bit better. – CubeJockey Apr 29 '15 at 18:07
  • i tried with getContentPane, but it returns the contentpane values in the window. i don't know why. Thank you – pianka Apr 29 '15 at 18:08
  • i'm sorry... how can MoseEvent helps me? – pianka Apr 29 '15 at 18:15
0

Check out the Screen Image class. You can create an image of any component displayed on the frame.

So you could use frame.getRootPane() as the component you want the image of.

Or frame.getContentPane() if you don't want the menu bar. I think this method returns a Container object so you will need to cast is to a JComponent.

camickr
  • 321,443
  • 19
  • 166
  • 288
0
int mouseX = MouseInfo.getPointerInfo().getLocation().getX() - frame.getX() - frame.getRootPane().getX();
int mouseY = MouseInfo.getPointerInfo().getLocation().getY() - frame.getY() - frame.getRootPane().getY();

This gives the top right point within the border / tab of a frame

Keegan Teetaert
  • 643
  • 6
  • 22