下面列出了java.awt.Rectangle#getCenterX ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public Transferable createTransferable(JComponent c) {
if (c instanceof NWidget) {
NWidget comp = (NWidget) c;
return comp.getData();
}
if (!coping) {
Rectangle rec = gui.getSelection().getFirst().getBounds();
int a = (int) rec.getCenterX() - gui.getSelection().getFirst().getNiftyElement().getX();
int b = (int) rec.getCenterY() - gui.getSelection().getFirst().getNiftyElement().getY();
return new WidgetData(gui.getSelection().getFirst(), a, b);
}
if (copyTemplate != null) {
return new WidgetData(copyTemplate, 0, 0);
} else {
return null;
}
}
public void regroupVertices(FGController controller, FGVertex vertex) {
FGData functionGraphData = controller.getFunctionGraphData();
FunctionGraph functionGraph = functionGraphData.getFunctionGraph();
GroupHistoryInfo groupHistory = functionGraph.getGroupHistory(vertex);
if (groupHistory == null) {
throw new AssertException("Cannot find group history for regroup operation!");
}
Set<FGVertex> vertices = new HashSet<>(groupHistory.getVertices());
Point2D location = groupHistory.getGroupLocation();
if (location == null) {
FGView view = controller.getView();
VisualizationServer<FGVertex, FGEdge> viewer = view.getPrimaryGraphViewer();
Rectangle containingBounds =
GraphViewerUtils.getBoundsForVerticesInLayoutSpace(viewer, vertices);
location =
new Point2D.Double(containingBounds.getCenterX(), containingBounds.getCenterY());
}
fixupVerticesForUncollapsedGroupMembers(functionGraph, vertices);
String text = groupHistory.getGroupDescription();
groupVertices(controller, text, vertices, location, true);
}
public void drawCollapsedMarker(int x, int y, int width, int height) {
// rectangle
int rectangleWidth = MARKER_WIDTH;
int rectangleHeight = MARKER_WIDTH;
Rectangle rect = new Rectangle(x + (width - rectangleWidth) / 2, y + height - rectangleHeight - 3, rectangleWidth, rectangleHeight);
g.draw(rect);
// plus inside rectangle
Line2D.Double line = new Line2D.Double(rect.getCenterX(), rect.getY() + 2, rect.getCenterX(), rect.getMaxY() - 2);
g.draw(line);
line = new Line2D.Double(rect.getMinX() + 2, rect.getCenterY(), rect.getMaxX() - 2, rect.getCenterY());
g.draw(line);
}
public void showInCenter(JFrame parent) {
Rectangle parentBounds = parent.getBounds();
double x = parentBounds.getCenterX() - 450 / 2.0;
double y = parentBounds.getCenterY() - 300 / 2.0;
// setLocation((int)x, (int)y);
setBounds((int)x, (int)y, 450, 300);
setVisible(true);
}
static void testSplash(ImageInfo test) throws Exception {
SplashScreen splashScreen = SplashScreen.getSplashScreen();
if (splashScreen == null) {
throw new RuntimeException("Splash screen is not shown!");
}
Graphics2D g = splashScreen.createGraphics();
Rectangle splashBounds = splashScreen.getBounds();
int screenX = (int) splashBounds.getCenterX();
int screenY = (int) splashBounds.getCenterY();
if(splashBounds.width != IMAGE_WIDTH){
throw new RuntimeException(
"SplashScreen#getBounds has wrong width");
}
if(splashBounds.height != IMAGE_HEIGHT){
throw new RuntimeException(
"SplashScreen#getBounds has wrong height");
}
Robot robot = new Robot();
Color splashScreenColor = robot.getPixelColor(screenX, screenY);
float scaleFactor = getScaleFactor();
Color testColor = (1 < scaleFactor) ? test.color2x : test.color1x;
if (!compare(testColor, splashScreenColor)) {
throw new RuntimeException(
"Image with wrong resolution is used for splash screen!");
}
}
private Point calcCenter(Rectangle r) {
Point center = new Point((int) r.getCenterX(), (int) r.getCenterY());
center.x -= getScrollPane().getViewport().getViewRect().width / 2;
center.y -= getScrollPane().getViewport().getViewRect().height / 2;
// Ensure to be within area
center.x = Math.max(0, center.x);
center.x = Math.min(getScrollPane().getViewport().getViewSize().width - getScrollPane().getViewport().getViewRect().width, center.x);
center.y = Math.max(0, center.y);
center.y = Math.min(getScrollPane().getViewport().getViewSize().height - getScrollPane().getViewport().getViewRect().height, center.y);
return center;
}
private Point getViewportMidpoint(JViewport viewport) {
Point viewPosition = viewport.getViewPosition();
Rectangle viewportRectangle = new Rectangle(viewPosition, viewport.getSize());
Point viewportMidpoint = new Point((int) viewportRectangle.getCenterX(),
(int) viewportRectangle.getCenterY());
return viewportMidpoint;
}
/**
* Scrolls the view to the specified {@link Operator}, making it the center of the view if
* necessary, indicated by the flag.
*
* @param operator
* the operator to focus on
* @param toCenter
* flag to indicate whether to force centering
* @return whether the scrolling was successful
* @since 7.5
* @see #scrollToViewPosition(Point)
*/
public boolean scrollToOperator(Operator operator, boolean toCenter) {
Rectangle2D opRect = renderer.getModel().getOperatorRect(operator);
if (opRect == null) {
return false;
}
int pIndex = renderer.getProcessIndexOfOperator(operator);
if (pIndex == -1) {
return false;
}
Rectangle opViewRect = getOpViewRect(opRect, pIndex);
Rectangle viewRect = getViewPort().getViewRect();
if (!toCenter) {
if (viewRect.contains(opViewRect)) {
// if operator visible, do nothing
return false;
}
if (viewRect.intersects(opViewRect)) {
// if partially visible, just scroll it into view
opViewRect.translate(-viewRect.x, -viewRect.y);
getViewPort().scrollRectToVisible(opViewRect);
// return false nonetheless, see PortInfoBubble
return false;
}
}
Point opCenter = new Point((int) opViewRect.getCenterX(), (int) opViewRect.getCenterY());
scrollToViewPosition(opCenter);
return true;
}
private double get_distance(Rectangle r1, Rectangle r2) {
double x1 = r1.getCenterX();
double y1 = r1.getCenterY();
double x2 = r2.getCenterX();
double y2 = r2.getCenterY();
double delta_x = Math.abs(x1 - x2);
double delta_y = Math.abs(y1 - y2);
return Math.sqrt(delta_x * delta_x + delta_y * delta_y);
}
public void drawCollapsedMarker(int x, int y, int width, int height) {
// rectangle
int rectangleWidth = MARKER_WIDTH;
int rectangleHeight = MARKER_WIDTH;
Rectangle rect = new Rectangle(x + (width - rectangleWidth) / 2, y + height - rectangleHeight - 3, rectangleWidth, rectangleHeight);
g.draw(rect);
// plus inside rectangle
Line2D.Double line = new Line2D.Double(rect.getCenterX(), rect.getY() + 2, rect.getCenterX(), rect.getMaxY() - 2);
g.draw(line);
line = new Line2D.Double(rect.getMinX() + 2, rect.getCenterY(), rect.getMaxX() - 2, rect.getCenterY());
g.draw(line);
}
/**
* Shrink a bit a bounding bounds when checking for note overlap.
*
* @param box the bounding bounds
* @return the shrunk bounds
*/
public static Rectangle2D shrink (Rectangle box)
{
double newWidth = constants.shrinkHoriRatio.getValue() * box.width;
double newHeight = constants.shrinkVertRatio.getValue() * box.height;
return new Rectangle2D.Double(
box.getCenterX() - (newWidth / 2.0),
box.getCenterY() - (newHeight / 2.0),
newWidth,
newHeight);
}
public void drawCollapsedMarker(int x, int y, int width, int height) {
// rectangle
int rectangleWidth = MARKER_WIDTH;
int rectangleHeight = MARKER_WIDTH;
Rectangle rect = new Rectangle(x + (width - rectangleWidth) / 2, y + height - rectangleHeight - 3, rectangleWidth, rectangleHeight);
g.draw(rect);
// plus inside rectangle
Line2D.Double line = new Line2D.Double(rect.getCenterX(), rect.getY() + 2, rect.getCenterX(), rect.getMaxY() - 2);
g.draw(line);
line = new Line2D.Double(rect.getMinX() + 2, rect.getCenterY(), rect.getMaxX() - 2, rect.getCenterY());
g.draw(line);
}
private Point calcCenter(Rectangle r) {
Point center = new Point((int) r.getCenterX(), (int) r.getCenterY());
center.x -= getScrollPane().getViewport().getViewRect().width / 2;
center.y -= getScrollPane().getViewport().getViewRect().height / 2;
// Ensure to be within area
center.x = Math.max(0, center.x);
center.x = Math.min(getScrollPane().getViewport().getViewSize().width - getScrollPane().getViewport().getViewRect().width, center.x);
center.y = Math.max(0, center.y);
center.y = Math.min(getScrollPane().getViewport().getViewSize().height - getScrollPane().getViewport().getViewRect().height, center.y);
return center;
}
static void testSplash(ImageInfo test) throws Exception {
SplashScreen splashScreen = SplashScreen.getSplashScreen();
if (splashScreen == null) {
throw new RuntimeException("Splash screen is not shown!");
}
Graphics2D g = splashScreen.createGraphics();
Rectangle splashBounds = splashScreen.getBounds();
int screenX = (int) splashBounds.getCenterX();
int screenY = (int) splashBounds.getCenterY();
if(splashBounds.width != IMAGE_WIDTH){
throw new RuntimeException(
"SplashScreen#getBounds has wrong width");
}
if(splashBounds.height != IMAGE_HEIGHT){
throw new RuntimeException(
"SplashScreen#getBounds has wrong height");
}
Robot robot = new Robot();
Color splashScreenColor = robot.getPixelColor(screenX, screenY);
float scaleFactor = getScaleFactor();
Color testColor = (1 < scaleFactor) ? test.color2x : test.color1x;
if (!compare(testColor, splashScreenColor)) {
throw new RuntimeException(
"Image with wrong resolution is used for splash screen!");
}
}
@Override
public void mouseMoved(MouseEvent e) {
Rectangle selected = this.model.getRectangle();
if(e.getX()>selected.getMaxX()-5 && e.getX()<selected.getMaxX()+5
&& e.getY()>selected.getMaxY()-5
&& e.getY()<selected.getMaxY()+5
){
e.getComponent().setCursor(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR));
curDir=DIR_SE;
}else if(e.getX()==selected.getMinX() && (e.getY()<selected.getMaxY() && e.getY()>selected.getMinY() )){
e.getComponent().setCursor(Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR));
curDir=DIR_W;
}else if(e.getX()==selected.getMaxX() && (e.getY()<selected.getMaxY() && e.getY()>selected.getMinY() )){
e.getComponent().setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
curDir=DIR_E;
}
else if(e.getY()<selected.getMaxY()+5 && e.getY()>selected.getMaxY()-5
&& (e.getX()<selected.getMaxX() && e.getX()>selected.getMinX() )){
e.getComponent().setCursor(Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR));
curDir=DIR_S;
}
else if(e.getY()==selected.getMinY() && (e.getX()<selected.getMaxX() && e.getX()>selected.getMinX() )){
curDir=DIR_N;
e.getComponent().setCursor(Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR));
}else if(e.getY()<selected.getCenterY()+10 &&
e.getY()>selected.getCenterY()-10 && (e.getX()<(selected.getCenterX()+10) && e.getX()>selected.getCenterX()-10 )){
e.getComponent().setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
curDir = MOV;
}
else{
e.getComponent().setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
curDir=NOP;
}
}
private Point calcCenter(Rectangle r) {
Point center = new Point((int) r.getCenterX(), (int) r.getCenterY());
center.x -= getScrollPane().getViewport().getViewRect().width / 2;
center.y -= getScrollPane().getViewport().getViewRect().height / 2;
// Ensure to be within area
center.x = Math.max(0, center.x);
center.x = Math.min(getScrollPane().getViewport().getViewSize().width - getScrollPane().getViewport().getViewRect().width, center.x);
center.y = Math.max(0, center.y);
center.y = Math.min(getScrollPane().getViewport().getViewSize().height - getScrollPane().getViewport().getViewRect().height, center.y);
return center;
}
private Point calcCenter(Rectangle r) {
Point center = new Point((int) r.getCenterX(), (int) r.getCenterY());
center.x -= getScrollPane().getViewport().getViewRect().width / 2;
center.y -= getScrollPane().getViewport().getViewRect().height / 2;
// Ensure to be within area
center.x = Math.max(0, center.x);
center.x = Math.min(getScrollPane().getViewport().getViewSize().width - getScrollPane().getViewport().getViewRect().width, center.x);
center.y = Math.max(0, center.y);
center.y = Math.min(getScrollPane().getViewport().getViewSize().height - getScrollPane().getViewport().getViewRect().height, center.y);
return center;
}
static void testSplash(ImageInfo test) throws Exception {
SplashScreen splashScreen = SplashScreen.getSplashScreen();
if (splashScreen == null) {
throw new RuntimeException("Splash screen is not shown!");
}
Graphics2D g = splashScreen.createGraphics();
Rectangle splashBounds = splashScreen.getBounds();
int screenX = (int) splashBounds.getCenterX();
int screenY = (int) splashBounds.getCenterY();
if(splashBounds.width != IMAGE_WIDTH){
throw new RuntimeException(
"SplashScreen#getBounds has wrong width");
}
if(splashBounds.height != IMAGE_HEIGHT){
throw new RuntimeException(
"SplashScreen#getBounds has wrong height");
}
Robot robot = new Robot();
Color splashScreenColor = robot.getPixelColor(screenX, screenY);
float scaleFactor = getScaleFactor();
Color testColor = (1 < scaleFactor) ? test.color2x : test.color1x;
if (!compare(testColor, splashScreenColor)) {
throw new RuntimeException(
"Image with wrong resolution is used for splash screen!");
}
}
static void testSplash(ImageInfo test) throws Exception {
SplashScreen splashScreen = SplashScreen.getSplashScreen();
if (splashScreen == null) {
throw new RuntimeException("Splash screen is not shown!");
}
Graphics2D g = splashScreen.createGraphics();
Rectangle splashBounds = splashScreen.getBounds();
int screenX = (int) splashBounds.getCenterX();
int screenY = (int) splashBounds.getCenterY();
if (splashBounds.width != IMAGE_WIDTH) {
throw new RuntimeException(
"SplashScreen#getBounds has wrong width");
}
if (splashBounds.height != IMAGE_HEIGHT) {
throw new RuntimeException(
"SplashScreen#getBounds has wrong height");
}
Robot robot = new Robot();
Color splashScreenColor = robot.getPixelColor(screenX, screenY);
float scaleFactor = getScaleFactor();
Color testColor = (1 < scaleFactor) ? test.color2x : test.color1x;
if (!compare(testColor, splashScreenColor)) {
throw new RuntimeException(
"Image with wrong resolution is used for splash screen!");
}
}
@Override
protected Animator createAnimator() {
Rectangle viewSpaceRectangle =
GraphViewerUtils.translateRectangleFromVertexRelativeSpaceToViewSpace(viewer, vertex,
visibleArea);
//
// Get the point to which we will move if any of the cursor is obscured
//
Point newPoint =
new Point((int) viewSpaceRectangle.getCenterX(), (int) viewSpaceRectangle.getCenterY());
// get the point of the cursor, centered in the vertex (this prevents jumping from
// side-to-side as we move the vertex)
Rectangle vertexBounds = GraphViewerUtils.getVertexBoundsInViewSpace(viewer, vertex);
int vertexCenterX = vertexBounds.x + (vertexBounds.width >> 1);
newPoint.x = vertexCenterX;
// see if the cursor bounds are not completely in screen
Rectangle viewerBounds = viewer.getBounds();
if (!viewerBounds.contains(viewSpaceRectangle)) {
preCreatedDestinaton = newPoint;
return super.createAnimator();
}
if (!satelliteViewer.isDocked()) {
return null; // cannot obscure if not docked
}
if (!satelliteViewer.isShowing()) {
return null; // nothing to do
}
// see if the satellite is hiding the area
Rectangle satelliteBounds = satelliteViewer.getBounds();
if (!satelliteBounds.contains(viewSpaceRectangle)) {
return null; // nothing to do
}
preCreatedDestinaton = newPoint;
return super.createAnimator();
}