下面列出了java.awt.Rectangle#getCenterY ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* �������ĵ�
*/
public void updateCenterPoint() {
minX = maxX = (int) squareList.get(0).getCenterX();
minY = maxY = (int) squareList.get(0).getCenterY();
for (Rectangle rect : squareList) {
if (rect.getCenterX() < minX) {
minX = (int) rect.getCenterX();
} else if (rect.getCenterX() > maxX) {
maxX = (int) rect.getCenterX();
}
if (rect.getCenterY() < minY) {
minY = (int) rect.getCenterY();
} else if (rect.getCenterY() > maxY) {
maxY = (int) rect.getCenterY();
}
}
center.x = (minX + maxX) / 2;
center.y = (minY + maxY) / 2;
x = minX - Constant.BLOCK_WIDTH / 2;
y = minY - Constant.BLOCK_HEIGHT / 2;
}
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();
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();
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();
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!");
}
}
/**
* Returns the Polygon for a triangle in the middle of the provided
* rect, pointing in the orientation
* (given as angle from upwards, or orientation vector)
* @param rect rectangle with the location
* @param orientation orientation of the sprite.
* @return a polygon (triangle) with the specified orientation.
*/
public static Polygon triPoints(Rectangle rect, Direction orientation)
{
Vector2d p1 = new Vector2d(rect.getCenterX()+orientation.x()*rect.getWidth()/3.0,
rect.getCenterY()+orientation.y()*rect.getHeight()/3.0);
Vector2d p2 = new Vector2d(rect.getCenterX()+orientation.x()*rect.getWidth()/4.0,
rect.getCenterY()+orientation.y()*rect.getHeight()/4.0);
Vector2d orthdir = new Vector2d(orientation.y(), -orientation.x());
Vector2d p2a = new Vector2d(p2.x-orthdir.x*rect.getWidth()/6.0,
p2.y-orthdir.y*rect.getHeight()/6.0);
Vector2d p2b = new Vector2d(p2.x+orthdir.x*rect.getWidth()/6.0,
p2.y+orthdir.y*rect.getHeight()/6.0);
return new Polygon(new int[]{(int)p1.x, (int)p2a.x, (int)p2b.x},
new int[]{(int)p1.y, (int)p2a.y, (int)p2b.y}, 3);
}
@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;
}
}
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);
}
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!");
}
}
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!");
}
}
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;
}
@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;
}
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 void init() {
int nds = scene.getNodes().size();
bounds = new Rectangle(magicSizeConstant + (magicSizeMultiplier * nds),
magicSizeConstant + (magicSizeMultiplier * nds)); //g.getMaximumBounds();
temp = bounds.getWidth() / 10;
forceConstant = 0.75 * Math.sqrt(bounds.getHeight() * bounds.getWidth() / nds);
GraphNode<I> rn = scene.getRootGraphNode();
NodeWidget rw = getWidget(rn);
rw.locX = bounds.getCenterX();
rw.locY = bounds.getCenterY();
rw.setFixed(true);
layoutCirculary(scene.getNodes(), rn);
}
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!");
}
}
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);
}
/** Returns the center position of the current view. */
public Point getCurrentViewCenter() {
Rectangle viewRect = getViewPort().getViewRect();
Point center = new Point((int) viewRect.getCenterX(), (int) viewRect.getCenterY());
return center;
}