public class ComponentUtil {
public static final int LEFT = 0;
public static final int LEFT_TOP = 1;
public static final int LEFT_CENTER = 2;
public static final int LEFT_BOTTOM = 3;
public static final int RIGHT = 10;
public static final int RIGHT_TOP = 11;
public static final int RIGHT_CENTER = 12;
public static final int RIGHT_BOTTOM = 13;
public static final int TOP = 20;
public static final int TOP_LEFT = 21;
public static final int TOP_CENTER = 22;
public static final int TOP_RIGHT = 23;
public static final int BOTTOM = 30;
public static final int BOTTOM_LEFT = 31;
public static final int BOTTOM_CENTER = 32;
public static final int BOTTOM_RIGHT = 33;
public static final int INNER_LEFT_TOP = 4;
public static final int INNER_LEFT_BOTTOM = 5;
public static final int INNER_RIGHT_TOP = 6;
public static final int INNER_RIGHT_BOTTOM = 7;
public static final int RESIZE_TO_LEFT = 50;
public static final int RESIZE_TO_RIGHT = 51;
public static final int RESIZE_TO_TOP = 52;
public static final int RESIZE_TO_BOTTOM = 53;
private static Point getRefXY(Component refC, Component newC, int refDirection) {
return getRefXY(refC, newC, refDirection, 0);
}
private static Point getRefXY(Component refC, Component newC, int refDirection, int offset) {
int value = 0;
switch (refDirection) {
case LEFT: // 00
return new Point(refC.getX() - newC.getWidth() - offset, newC.getY());
case LEFT_TOP: // 01
return new Point(refC.getX() - newC.getWidth() - offset, refC.getY());
case LEFT_CENTER: // 02
value = (refC.getHeight() - newC.getHeight()) / 2;
return new Point(refC.getX() - newC.getWidth() - offset, refC.getY() + value);
case LEFT_BOTTOM: // 03
value = refC.getHeight() - newC.getHeight();
return new Point(refC.getX() - newC.getWidth() - offset, refC.getY() + value);
case RIGHT: // 10
return new Point(refC.getX() + refC.getWidth() + offset, newC.getY());
case RIGHT_TOP: // 11
return new Point(refC.getX() + refC.getWidth() + offset, refC.getY());
case RIGHT_CENTER: // 12
value = (refC.getHeight() - newC.getHeight()) / 2;
return new Point(refC.getX() + refC.getWidth() + offset, refC.getY() + value);
case RIGHT_BOTTOM: // 13
value = refC.getHeight() - newC.getHeight();
return new Point(refC.getX() + refC.getWidth() + offset, refC.getY() + value);
case TOP: // 20
return new Point(newC.getX(), refC.getY() - newC.getHeight() - offset);
case TOP_LEFT:// 21
return new Point(refC.getX(), refC.getY() - newC.getHeight() - offset);
case TOP_CENTER: // 22
value = (refC.getWidth() - newC.getWidth()) / 2;
return new Point(refC.getX() + value, refC.getY() - newC.getHeight() - offset);
case TOP_RIGHT: // 23
value = refC.getWidth() - newC.getWidth();
return new Point(refC.getX() + value, refC.getY() - newC.getHeight() - offset);
case BOTTOM: // 30
return new Point(newC.getX(), refC.getY() + refC.getHeight() + offset);
case BOTTOM_LEFT: // 31
return new Point(refC.getX(), refC.getY() + refC.getHeight() + offset);
case BOTTOM_CENTER: // 32
value = (refC.getWidth() - newC.getWidth()) / 2;
return new Point(refC.getX() + value, refC.getY() + refC.getHeight() + offset);
case BOTTOM_RIGHT: // 33
value = refC.getWidth() - newC.getWidth();
return new Point(refC.getX() + value, refC.getY() + refC.getHeight() + offset);
case INNER_LEFT_TOP: // 4
return new Point(offset, offset);
case INNER_LEFT_BOTTOM: // 5
return new Point(0 + offset, refC.getHeight() - newC.getHeight() - offset - 38); // 38
case INNER_RIGHT_TOP: // 6
return new Point(refC.getWidth() - newC.getWidth() - offset - 15, offset); // 15
case INNER_RIGHT_BOTTOM: // 7
return new Point(refC.getWidth() - newC.getWidth() - offset - 15, refC.getHeight() - newC.getHeight() - offset - 38);
default:
return refC.getLocation();
}
}
public static void setAlign(Component oldC, Component newC, int style) {
int value = 0;
switch (style) {
case RESIZE_TO_LEFT:
value = Math.abs(oldC.getX() - newC.getX());
if (newC.getX() < oldC.getX()) {
newC.setLocation(oldC.getX(), newC.getY());
} else {
newC.setSize(newC.getWidth() + value, newC.getHeight());
newC.setLocation(oldC.getX(), newC.getY());
}
break;
case RESIZE_TO_RIGHT:
value = Math.abs(oldC.getX() - newC.getX());
if (newC.getX() > oldC.getX()) {
newC.setLocation(oldC.getX(), newC.getY());
} else {
newC.setSize(oldC.getWidth() + value, newC.getHeight());
}
break;
case RESIZE_TO_TOP:
value = Math.abs(oldC.getY() - newC.getY());
if (oldC.getY() > newC.getY()) {
newC.setLocation(newC.getX(), oldC.getY());
} else {
newC.setSize(newC.getWidth(), value + newC.getHeight());
newC.setLocation(newC.getX(), oldC.getY());
}
break;
case RESIZE_TO_BOTTOM:
value = Math.abs(oldC.getY() - newC.getY());
if (oldC.getY() < newC.getY()) {
newC.setLocation(newC.getX(), oldC.getY());
} else {
newC.setSize(newC.getWidth(), value + newC.getHeight());
}
break;
default:
break;
}
}
public static void setLocation(Component refC, Component newC, int refDirection, int offset){
newC.setLocation(getRefXY(refC, newC, refDirection, offset));
}
public static void setLocation(Component refC, Component newC, int refDirection){
newC.setLocation(getRefXY(refC, newC, refDirection));
}
public static void setBounds(int width, int height, Component refC, Component newC, int refDirection, int offset) {
newC.setSize(width, height);
newC.setLocation(getRefXY(refC, newC, refDirection, offset));
}
public static void setBounds(int width, int height, Component refC, Component newC, int refDirection) {
setBounds(width, height, refC, newC, refDirection, 0);
}
public static void setBounds(Dimension size, Component refC, Component newC, int refDirection, int offset) {
newC.setSize(size);
newC.setLocation(getRefXY(refC, newC, refDirection, offset));
}
public static void setBounds(Dimension size, Component refC, Component newC, int refDirection) {
setBounds(size, refC, newC, refDirection, 0);
}
}
ComponentUtil
最新推荐文章于 2024-05-27 22:30:24 发布