下面列出了java.awt.Rectangle#setLocation ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Creates a png of the given component <b>by capturing a screenshot of the image</b>. This
* differs from creating the image by rendering it via a {@link Graphics} object.
*
* @param c the component
* @return the new image
* @throws AWTException if there is a problem creating the image
*/
public static Image createScreenImage(Component c) throws AWTException {
yieldToSwing();
Rectangle r = c.getBounds();
Point p = r.getLocation();
if (!(c instanceof Window)) {
SwingUtilities.convertPointToScreen(p, c.getParent());
}
r.setLocation(p);
Robot robot = new Robot();
sleep(100);
Image image = robot.createScreenCapture(r);
sleep(100);
return image;
}
/**
* Overwritting intersects to check if we are on ground.
* @return true if it directly intersects with sp (as in the normal case), but additionally checks for on_ground condition.
*/
public boolean groundIntersects (VGDLSprite sp)
{
boolean normalIntersect = this.rect.intersects(sp.rect);
boolean otherHigher = sp.lastrect.getMinY() > (this.lastrect.getMinY()+(this.rect.height/2));
boolean goingDown = this.rect.getMinY() > this.lastrect.getMinY();
if(!on_ground && sp.solid)
{
//No need to keep checking. Actually, we shouldn't (we won't intersect with all sprites!).
Rectangle test_rect = new Rectangle(this.rect);
test_rect.setLocation(this.rect.x,this.rect.y+3);
this.on_ground = test_rect.intersects(sp.rect) && otherHigher && goingDown;
}
return normalIntersect;
}
/**
* Draw the entity.
*
* @param g2d
* The graphics to drawn on.
*/
@Override
public void draw(final Graphics2D g2d) {
applyChanges();
final Rectangle r = getDrawingArea();
if (isContained()) {
r.setLocation(0, 0);
} else {
if (!isOnScreen(g2d, r)) {
return;
}
}
final Composite oldComposite = g2d.getComposite();
try {
g2d.setComposite(entityComposite);
draw(g2d, r.x, r.y, r.width, r.height);
} finally {
g2d.setComposite(oldComposite);
}
}
public WreckSprite(BoardView1 boardView1, final Entity entity, int secondaryPos) {
super(boardView1);
this.entity = entity;
this.secondaryPos = secondaryPos;
String shortName = entity.getShortName();
Font font = new Font("SansSerif", Font.PLAIN, 10); //$NON-NLS-1$
modelRect = new Rectangle(47, 55, bv.getFontMetrics(font).stringWidth(
shortName) + 1, bv.getFontMetrics(font).getAscent());
Rectangle tempBounds = new Rectangle(bv.hex_size).union(modelRect);
if (secondaryPos == -1) {
tempBounds.setLocation(bv.getHexLocation(entity.getPosition()));
} else {
tempBounds.setLocation(bv.getHexLocation(entity
.getSecondaryPositions().get(secondaryPos)));
}
bounds = tempBounds;
image = null;
}
private void computePopupBounds (Rectangle result, JLayeredPane lPane, int modelSize) {
Dimension cSize = comboBar.getSize();
int width = getPopupWidth();
Point location = new Point(cSize.width - width - 1, comboBar.getBottomLineY() - 1);
if (SwingUtilities.getWindowAncestor(comboBar) != null) {
location = SwingUtilities.convertPoint(comboBar, location, lPane);
}
result.setLocation(location);
// hack to make jList.getpreferredSize work correctly
// JList is listening on ResultsModel same as us and order of listeners
// is undefined, so we have to force update of JList's layout data
jList1.setFixedCellHeight(15);
jList1.setFixedCellHeight(-1);
// end of hack
jList1.setVisibleRowCount(modelSize);
Dimension preferredSize = jList1.getPreferredSize();
preferredSize.width = width;
preferredSize.height += statusPanel.getPreferredSize().height + 3;
result.setSize(preferredSize);
}
/**
* Draw the top layer parts of an entity. This will be on down after all
* other game layers are rendered.
*
* @param g2d
* The graphics to drawn on.
*/
@Override
public void drawTop(final Graphics2D g2d) {
final Rectangle r = getArea();
if (isContained()) {
r.setLocation(0, 0);
} else {
if (!isOnScreen(g2d, r)) {
return;
}
}
final Composite oldComposite = g2d.getComposite();
try {
g2d.setComposite(entityComposite);
drawTop(g2d, r.x, r.y, r.width, r.height);
} finally {
g2d.setComposite(oldComposite);
}
}
void setContentBounds(WindowDimensions dims) {
XToolkit.awtLock();
try {
// Bounds of content window are of the same size as bounds of Java window and with
// location as -(insets)
Rectangle newBounds = dims.getBounds();
Insets in = dims.getInsets();
if (in != null) {
newBounds.setLocation(-in.left, -in.top);
}
if (insLog.isLoggable(PlatformLogger.Level.FINE)) {
insLog.fine("Setting content bounds {0}, old bounds {1}",
newBounds, getBounds());
}
// Fix for 5023533:
// Change in the size of the content window means, well, change of the size
// Change in the location of the content window means change in insets
boolean needHandleResize = !(newBounds.equals(getBounds()));
boolean needPaint = width <= 0 || height <= 0;
reshape(newBounds);
if (needHandleResize) {
insLog.fine("Sending RESIZED");
handleResize(newBounds);
}
if (needPaint) {
postPaintEvent(target, 0, 0, newBounds.width, newBounds.height);
}
} finally {
XToolkit.awtUnlock();
}
}
void setContentBounds(WindowDimensions dims) {
XToolkit.awtLock();
try {
// Bounds of content window are of the same size as bounds of Java window and with
// location as -(insets)
Rectangle newBounds = dims.getBounds();
Insets in = dims.getInsets();
if (in != null) {
newBounds.setLocation(-in.left, -in.top);
}
if (insLog.isLoggable(PlatformLogger.Level.FINE)) {
insLog.fine("Setting content bounds {0}, old bounds {1}",
newBounds, getBounds());
}
// Fix for 5023533:
// Change in the size of the content window means, well, change of the size
// Change in the location of the content window means change in insets
boolean needHandleResize = !(newBounds.equals(getBounds()));
reshape(newBounds);
if (needHandleResize) {
insLog.fine("Sending RESIZED");
handleResize(newBounds);
}
} finally {
XToolkit.awtUnlock();
}
validateSurface();
}
public void performSelection(Rectangle rectangle) {
if (rectangle.width < 0) {
rectangle.x += rectangle.width;
rectangle.width *= -1;
}
if (rectangle.height < 0) {
rectangle.y += rectangle.height;
rectangle.height *= -1;
}
boolean changed = false;
for (InputBlock b : this.getNodes()) {
BlockWidget w = (BlockWidget) findWidget(b);
Rectangle r = new Rectangle(w.getBounds());
r.setLocation(w.getLocation());
if (r.intersects(rectangle)) {
if (!selection.contains(w)) {
changed = true;
selection.add(w);
w.setState(w.getState().deriveSelected(true));
}
} else {
if (selection.contains(w)) {
changed = true;
selection.remove(w);
w.setState(w.getState().deriveSelected(false));
}
}
}
if (changed) {
selectionChanged();
}
}
public void performSelection(Rectangle rectangle) {
if (rectangle.width < 0) {
rectangle.x += rectangle.width;
rectangle.width *= -1;
}
if (rectangle.height < 0) {
rectangle.y += rectangle.height;
rectangle.height *= -1;
}
boolean changed = false;
for (InputBlock b : this.getNodes()) {
BlockWidget w = (BlockWidget) findWidget(b);
Rectangle r = new Rectangle(w.getBounds());
r.setLocation(w.getLocation());
if (r.intersects(rectangle)) {
if (!selection.contains(w)) {
changed = true;
selection.add(w);
w.setState(w.getState().deriveSelected(true));
}
} else {
if (selection.contains(w)) {
changed = true;
selection.remove(w);
w.setState(w.getState().deriveSelected(false));
}
}
}
if (changed) {
selectionChanged();
}
}
void setContentBounds(WindowDimensions dims) {
XToolkit.awtLock();
try {
// Bounds of content window are of the same size as bounds of Java window and with
// location as -(insets)
Rectangle newBounds = dims.getBounds();
Insets in = dims.getInsets();
if (in != null) {
newBounds.setLocation(-in.left, -in.top);
}
if (insLog.isLoggable(PlatformLogger.Level.FINE)) {
insLog.fine("Setting content bounds {0}, old bounds {1}",
newBounds, getBounds());
}
// Fix for 5023533:
// Change in the size of the content window means, well, change of the size
// Change in the location of the content window means change in insets
boolean needHandleResize = !(newBounds.equals(getBounds()));
reshape(newBounds);
if (needHandleResize) {
insLog.fine("Sending RESIZED");
handleResize(newBounds);
}
} finally {
XToolkit.awtUnlock();
}
validateSurface();
}
@Override
public Rectangle getBounds() {
Dimension dim = new Dimension(bv.hex_size.width,
bv.hex_size.height);
bounds = new Rectangle(dim);
bounds.setLocation(this.bv.getHexLocation(flare.position));
return bounds;
}
public void performSelection(Rectangle rectangle) {
if (rectangle.width < 0) {
rectangle.x += rectangle.width;
rectangle.width *= -1;
}
if (rectangle.height < 0) {
rectangle.y += rectangle.height;
rectangle.height *= -1;
}
boolean changed = false;
for (InputBlock b : this.getNodes()) {
BlockWidget w = (BlockWidget) findWidget(b);
Rectangle r = new Rectangle(w.getBounds());
r.setLocation(w.getLocation());
if (r.intersects(rectangle)) {
if (!selection.contains(w)) {
changed = true;
selection.add(w);
w.setState(w.getState().deriveSelected(true));
}
} else {
if (selection.contains(w)) {
changed = true;
selection.remove(w);
w.setState(w.getState().deriveSelected(false));
}
}
}
if (changed) {
selectionChanged();
}
}
public GhostEntitySprite(BoardView1 boardView1, final Entity entity) {
super(boardView1);
this.entity = entity;
String shortName = entity.getShortName();
Font font = new Font("SansSerif", Font.PLAIN, 10); //$NON-NLS-1$
modelRect = new Rectangle(47, 55, bv.getFontMetrics(font).stringWidth(
shortName) + 1, bv.getFontMetrics(font).getAscent());
Rectangle tempBounds = new Rectangle(bv.hex_size).union(modelRect);
tempBounds.setLocation(bv.getHexLocation(entity.getPosition()));
bounds = tempBounds;
image = null;
}
public void performSelection(Rectangle rectangle) {
if (rectangle.width < 0) {
rectangle.x += rectangle.width;
rectangle.width *= -1;
}
if (rectangle.height < 0) {
rectangle.y += rectangle.height;
rectangle.height *= -1;
}
boolean changed = false;
for (InputBlock b : this.getNodes()) {
BlockWidget w = (BlockWidget) findWidget(b);
Rectangle r = new Rectangle(w.getBounds());
r.setLocation(w.getLocation());
if (r.intersects(rectangle)) {
if (!selection.contains(w)) {
changed = true;
selection.add(w);
w.setState(w.getState().deriveSelected(true));
}
} else {
if (selection.contains(w)) {
changed = true;
selection.remove(w);
w.setState(w.getState().deriveSelected(false));
}
}
}
if (changed) {
selectionChanged();
}
}
public IsometricSprite(BoardView1 boardView1, Entity entity, int secondaryPos, Image radarBlipImage) {
super(boardView1);
this.entity = entity;
this.radarBlipImage = radarBlipImage;
this.secondaryPos = secondaryPos;
String shortName = entity.getShortName();
Font font = new Font("SansSerif", Font.PLAIN, 10); //$NON-NLS-1$
modelRect = new Rectangle(47, 55, bv.getFontMetrics(font).stringWidth(
shortName) + 1, bv.getFontMetrics(font).getAscent());
int altAdjust = 0;
if (bv.useIsometric()
&& (entity.isAirborne() || entity.isAirborneVTOLorWIGE())) {
altAdjust = (int) (bv.DROPSHDW_DIST * bv.scale);
} else if (bv.useIsometric() && (entity.getElevation() != 0)
&& !(entity instanceof GunEmplacement)) {
altAdjust = (int) (entity.getElevation() * BoardView1.HEX_ELEV * bv.scale);
}
Dimension dim = new Dimension(bv.hex_size.width, bv.hex_size.height
+ altAdjust);
Rectangle tempBounds = new Rectangle(dim).union(modelRect);
if (secondaryPos == -1) {
tempBounds.setLocation(bv.getHexLocation(entity.getPosition()));
} else {
tempBounds.setLocation(bv.getHexLocation(entity
.getSecondaryPositions().get(secondaryPos)));
}
if (entity.getElevation() > 0) {
tempBounds.y = tempBounds.y - altAdjust;
}
bounds = tempBounds;
image = null;
}
@Override
public Rectangle getBounds() {
// Start with the hex and add the label
bounds = new Rectangle(0,0,bv.hex_size.width, bv.hex_size.height);
updateLabel();
bounds.add(labelRect);
// Add space for 4 little status boxes
if (labelPos == Positioning.RIGHT) {
bounds.add(-4*(labelRect.height+2)+labelRect.x, labelRect.y);
} else {
bounds.add(4*(labelRect.height+2)+labelRect.x+labelRect.width, labelRect.y);
}
// Move to board position, save this origin for correct drawing
hexOrigin = bounds.getLocation();
Point ePos;
if (secondaryPos == -1) {
ePos = bv.getHexLocation(entity.getPosition());
} else {
ePos = bv.getHexLocation(entity.getSecondaryPositions().get(secondaryPos));
}
bounds.setLocation(hexOrigin.x + ePos.x, hexOrigin.y + ePos.y);
entityRect = new Rectangle(bounds.x + (int) (20 * bv.scale), bounds.y
+ (int) (14 * bv.scale), (int) (44 * bv.scale),
(int) (44 * bv.scale));
return bounds;
}
/**
* This method creates and returns bounds.
*
* @param width The width of the bounds.
* @param height The height of the bounds.
* @param area The area to center the bounds to.
*
* @return Bounds.
*/
public
static
Rectangle
createCenteredBounds(int width, int height, Rectangle area)
{
Rectangle rectangle = new Rectangle(0, 0, width, height);
int x = area.x + (area.width / 2);
int y = area.y + (area.height / 2);
rectangle.setLocation(x - (width / 2), y - (height / 2));
return rectangle;
}
public static Point place(Rectangle rectToPlace, int x, int y, Rectangle outerLimit) {
rectToPlace.setLocation(x - rectToPlace.width / 2, y - rectToPlace.height / 2);
return fitSmallerIntoBigger(rectToPlace.x, rectToPlace.y, rectToPlace.width, rectToPlace.height, outerLimit);
}
/**
* Calculate interpolated bounds based on the initial and final bounds,
* and the state of progress.
*
* @param startBounds initial bounds
* @param finalBounds final bounds
* @param progress state of progress
* @return interpolated bounds
*/
private Rectangle interpolate(Rectangle startBounds, Rectangle finalBounds, double progress) {
Rectangle bounds = new Rectangle();
bounds.setLocation(interpolate(startBounds.getLocation(), finalBounds.getLocation(), progress));
bounds.setSize(interpolate(startBounds.getSize(), finalBounds.getSize(), progress));
return bounds;
}