Skip to content

Commit 6f05001

Browse files
hansonrhansonr
authored andcommitted
fix for JApplet.setLocationRelativeTo
1 parent c3e7d99 commit 6f05001

File tree

2 files changed

+78
-72
lines changed

2 files changed

+78
-72
lines changed

sources/net.sf.j2s.java.core/src/java/awt/Window.java

Lines changed: 67 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -3045,81 +3045,76 @@ void resetGC() {
30453045
// }
30463046
}
30473047

3048-
/**
3049-
* Sets the location of the window relative to the specified
3050-
* component.
3051-
* <p>
3052-
* If the component is not currently showing, or <code>c</code>
3053-
* is <code>null</code>, the window is placed at the center of
3054-
* the screen. The center point can be determined with {@link
3055-
* GraphicsEnvironment#getCenterPoint GraphicsEnvironment.getCenterPoint}
3056-
* <p>
3057-
* If the bottom of the component is offscreen, the window is
3058-
* placed to the side of the <code>Component</code> that is
3059-
* closest to the center of the screen. So if the <code>Component</code>
3060-
* is on the right part of the screen, the <code>Window</code>
3061-
* is placed to its left, and vice versa.
3062-
*
3063-
* @param c the component in relation to which the window's location
3064-
* is determined
3065-
* @see java.awt.GraphicsEnvironment#getCenterPoint
3066-
* @since 1.4
3067-
*/
3068-
public void setLocationRelativeTo(Component c) {
3069-
Container root=null;
3070-
3071-
if (c != null) {
3072-
if (c.isWindowOrJSApplet()) {
3073-
root = (Container)c;
3074-
} else {
3075-
Container parent;
3076-
for(parent = c.getParent() ; parent != null ; parent = parent.getParent()) {
3077-
if (parent.isWindowOrJSApplet()) {
3078-
root = parent;
3079-
break;
3080-
}
3081-
}
3082-
}
3083-
}
3084-
3085-
if((c != null && !c.isShowing()) || root == null ||
3086-
!root.isShowing()) {
3087-
Dimension paneSize = getSize();
3048+
/**
3049+
* Sets the location of the window relative to the specified component.
3050+
* <p>
3051+
* If the component is not currently showing, or <code>c</code> is
3052+
* <code>null</code>, the window is placed at the center of the screen. The
3053+
* center point can be determined with {@link GraphicsEnvironment#getCenterPoint
3054+
* GraphicsEnvironment.getCenterPoint}
3055+
* <p>
3056+
* If the bottom of the component is offscreen, the window is placed to the side
3057+
* of the <code>Component</code> that is closest to the center of the screen. So
3058+
* if the <code>Component</code> is on the right part of the screen, the
3059+
* <code>Window</code> is placed to its left, and vice versa.
3060+
*
3061+
* @param c the component in relation to which the window's location is
3062+
* determined
3063+
* @see java.awt.GraphicsEnvironment#getCenterPoint
3064+
* @since 1.4
3065+
*/
3066+
public void setLocationRelativeTo(Component c) {
3067+
Container root = null;
3068+
3069+
if (c != null) {
3070+
if (c.isWindowOrJSApplet()) {
3071+
root = (Container) c;
3072+
} else {
3073+
Container parent;
3074+
for (parent = c.getParent(); parent != null; parent = parent.getParent()) {
3075+
if (parent.isWindowOrJSApplet()) {
3076+
root = parent;
3077+
break;
3078+
}
3079+
}
3080+
}
3081+
}
30883082

3089-
Point centerPoint = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
3090-
setLocation(centerPoint.x - paneSize.width / 2,
3091-
centerPoint.y - paneSize.height / 2);
3092-
} else {
3093-
Dimension invokerSize = c.getSize();
3094-
Point invokerScreenLocation = c.getLocationOnScreen();
3095-
3096-
Rectangle windowBounds = getBounds();
3097-
int dx = invokerScreenLocation.x+((invokerSize.width-windowBounds.width)>>1);
3098-
int dy = invokerScreenLocation.y+((invokerSize.height - windowBounds.height)>>1);
3099-
Rectangle ss = root.getGraphicsConfiguration().getBounds();
3100-
3101-
// Adjust for bottom edge being offscreen
3102-
if (dy+windowBounds.height>ss.y+ss.height) {
3103-
dy = ss.y + ss.height-windowBounds.height;
3104-
if (invokerScreenLocation.x - ss.x + invokerSize.width / 2 <
3105-
ss.width / 2) {
3106-
dx = invokerScreenLocation.x+invokerSize.width;
3107-
}
3108-
else {
3109-
dx = invokerScreenLocation.x-windowBounds.width;
3110-
}
3111-
}
3083+
if ((c != null && !c.isShowing()) || root == null || !root.isShowing()) {
3084+
Dimension paneSize = getSize();
3085+
Point centerPoint = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
3086+
setLocation(centerPoint.x - paneSize.width / 2, centerPoint.y - paneSize.height / 2);
3087+
} else {
3088+
Dimension invokerSize = c.getSize();
3089+
Point invokerScreenLocation = c.getLocationOnScreen();
3090+
3091+
Rectangle windowBounds = getBounds();
3092+
int dx = invokerScreenLocation.x + ((invokerSize.width - windowBounds.width) >> 1);
3093+
int dy = invokerScreenLocation.y + ((invokerSize.height - windowBounds.height) >> 1);
3094+
Rectangle ss = root.getGraphicsConfiguration().getBounds();
3095+
3096+
// Adjust for bottom edge being offscreen
3097+
if (dy + windowBounds.height > ss.y + ss.height) {
3098+
dy = ss.y + ss.height - windowBounds.height;
3099+
if (invokerScreenLocation.x - ss.x + invokerSize.width / 2 < ss.width / 2) {
3100+
dx = invokerScreenLocation.x + invokerSize.width;
3101+
} else {
3102+
dx = invokerScreenLocation.x - windowBounds.width;
3103+
}
3104+
}
31123105

3113-
// Avoid being placed off the edge of the screen
3114-
if (dx+windowBounds.width > ss.x + ss.width) {
3115-
dx = ss.x + ss.width - windowBounds.width;
3116-
}
3117-
if (dx < ss.x) dx = ss.x;
3118-
if (dy < ss.y) dy = ss.y;
3106+
// Avoid being placed off the edge of the screen
3107+
if (dx + windowBounds.width > ss.x + ss.width) {
3108+
dx = ss.x + ss.width - windowBounds.width;
3109+
}
3110+
if (dx < ss.x)
3111+
dx = ss.x;
3112+
if (dy < ss.y)
3113+
dy = ss.y;
31193114

3120-
setLocation(dx, dy);
3121-
}
3122-
}
3115+
setLocation(dx, dy);
3116+
}
3117+
}
31233118

31243119
/**
31253120
* Overridden from Component. Top-level Windows should not propagate a

sources/net.sf.j2s.java.core/src/javax/swing/JApplet.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.awt.Graphics2D;
3838
import java.awt.HeadlessException;
3939
import java.awt.LayoutManager;
40+
import java.awt.Point;
4041

4142
/**
4243
* An extended version of <code>java.applet.Applet</code> that adds support for
@@ -641,4 +642,14 @@ protected String paramString() {
641642
// protected class AccessibleJApplet extends AccessibleApplet {
642643
// // everything moved to new parent, AccessibleApplet
643644
// }
645+
646+
/**
647+
* SwingJS needs this for the applet
648+
* because the jQuery.offset() call does not work for it directly.
649+
*/
650+
@Override
651+
public Point getLocationOnScreen() {
652+
return (isShowing() ? getRootPane().getLocationOnScreen() : null);
653+
}
654+
644655
}

0 commit comments

Comments
 (0)