下面列出了java.awt.Rectangle#getY ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Recalculate the Window position
*
* @param window the window
* @param position the position of the window
*/
private void recalculateWindowPosition(Window window, int position) {
Rectangle parentBounds = parent.getBounds();
parentBounds.setLocation(parent.getLocationOnScreen());
int rightX = (int) (parentBounds.getX() + parent.getWidth() - DEFAULT_RIGHT_MARGIN);
// this was going crazy sometimes
int topY = (int) parentBounds.getY();
int yOffset = windowYOffset.get(position);
// Recalculate the window positions
window.setLocation(rightX - window.getWidth(), topY + yOffset - window.getHeight());
// Check if the Window fits into the parents bounds
if (!parentBounds.contains(window.getBounds())) {
// back into the bubbleStack
window.setVisible(false);
bubbleStack.addFirst(window);
freeSpaces.add(windowPosition.remove(window));
window.removeWindowListener(closeListener);
}
}
/**
* remove redundant rectangles. Each of them may occur raster error, or has smaller length than minLength.
*
* @param rectangles list of rectangles
* @param xLimit limit of x+width of given rectangle
* @param yLimit limit of y+height of given rectangle
*/
public static void removeRedundantRectangles(List<Rectangle> rectangles, int xLimit, int yLimit) {
int minLength = 1;
List<Rectangle> removeList = new ArrayList<Rectangle>();
for (Rectangle rectangle : rectangles) {
reshapeRect(rectangle, xLimit, yLimit);
if (rectangle.getX() >= (xLimit - minLength) || rectangle.getY() >= (yLimit - minLength)
|| rectangle.getWidth() < minLength || rectangle.getHeight() < minLength) {
removeList.add(rectangle);
}
}
// remove recorded rectangles
for (Rectangle removeRect : removeList) {
rectangles.remove(removeRect);
}
}
/**
* if the given rectangle may occur raster error, reshape it
*
* @param rectangle Rectangle which will be reshaped
* @param xLimit limit of x+width of given rectangle
* @param yLimit limit of y+height of given rectangle
*/
public static void reshapeRect(Rectangle rectangle, int xLimit, int yLimit) {
double width = rectangle.getWidth(), height = rectangle.getHeight();
double x = rectangle.getX(), y = rectangle.getY();
if (x < 0) {
width += x;
x = 0;
}
if (y < 0) {
height += y;
y = 0;
}
if (x + width >= xLimit)
width = xLimit - x;
if (y + height >= yLimit)
height = yLimit - y;
rectangle.setRect(x, y, Math.max(width, 1), Math.max(height, 1));
}
public boolean contains(Rectangle rectangle, int row, int column) {
if (rectangle == null)
return true;
double px = column * (pageWidth + W_SPACE / zoom) * getZoom();
double py = row * (pageHeight + W_SPACE / zoom) * getZoom();
double r = (width + W_SPACE / zoom) * getZoom() + px;
double b = (height + W_SPACE / zoom) * getZoom() + py;
double rx = rectangle.getX();
double ry = rectangle.getY();
double rr = rectangle.getMaxX();
double rb = rectangle.getMaxY();
if (((px <= rr) && (px >= rx)) || ((r <= rr) && (r >= rx))
|| ((rr <= r) && (rr >= px)) || ((rx <= r) && (rx >= px))) {
return (((py <= rb) && (py >= ry)) || ((b <= rb) && (b >= ry))
|| ((rb <= b) && (rb >= py)) || ((ry <= b) && (ry >= py)));
}
return false;
}
/**
* <a href="http://stackoverflow.com/questions/40611736/rotate-pdf-around-its-center-using-pdfbox-in-java">
* Rotate PDF around its center using PDFBox in java
* </a>
* <p>
* This test shows how to rotate the page content and then set the crop
* box and media box to the bounding rectangle of the rotated page area.
* </p>
*/
@Test
public void testRotateExpandBox() throws IOException
{
try ( InputStream resource = getClass().getResourceAsStream("IRJET_Copy_Right_form.pdf") )
{
PDDocument document = Loader.loadPDF(resource);
PDPage page = document.getDocumentCatalog().getPages().get(0);
PDPageContentStream cs = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.PREPEND, false, false);
Matrix matrix = Matrix.getRotateInstance(Math.toRadians(45), 0, 0);
cs.transform(matrix);
cs.close();
PDRectangle cropBox = page.getCropBox();
Rectangle rectangle = cropBox.transform(matrix).getBounds();
PDRectangle newBox = new PDRectangle((float)rectangle.getX(), (float)rectangle.getY(), (float)rectangle.getWidth(), (float)rectangle.getHeight());
page.setCropBox(newBox);
page.setMediaBox(newBox);
document.save(new File(RESULT_FOLDER, "IRJET_Copy_Right_form-rotated-expand-box.pdf"));
}
}
@Override
protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds) {
int x = (int) thumbBounds.getX();
int y = (int) thumbBounds.getY();
int w = (int) thumbBounds.getWidth();
int h = (int) thumbBounds.getHeight();
if (c.isEnabled() && w > 0 && h > 0) {
if (this.scrollbar.getOrientation() == Adjustable.HORIZONTAL) {
h -= 1;
y++;
drawHorizThumb(g, x, y, w, h);
} else {
w -= 1;
x++;
drawVertThumb(g, x, y, w, h);
}
}
}
/**
* get tight differences area
*
* @param rectangle
* @param xLimit
* @param yLimit
* @return
*/
public static Rectangle getTightDiffArea(Rectangle rectangle, int xLimit, int yLimit) {
int minMargin = Math.min(ComparisonParameterDefaults.getDefaultGroupDistance() / 2,
ComparisonParameterDefaults.getSplitGroupDistance() / 2);
int x = (int) rectangle.getX(), y = (int) rectangle.getY(), width = (int) rectangle.getWidth(), height = (int) rectangle
.getHeight();
// check if the rectangle meets the boundary
if (x > 0) {
x += minMargin;
width -= minMargin;
}
if (y > 0) {
y += minMargin;
height -= minMargin;
}
if (x + width < xLimit) {
width -= minMargin;
}
if (y + height < yLimit) {
height -= minMargin;
}
return new Rectangle(x, y, width, height);
}
private void renderInventory(Graphics2D graphics)
{
Widget inventoryWidget = client.getWidget(WidgetInfo.INVENTORY);
if (inventoryWidget == null || inventoryWidget.isHidden())
{
return;
}
for (WidgetItem item : inventoryWidget.getWidgetItems())
{
Rectangle slotBounds = item.getCanvasBounds();
String idText = "" + item.getId();
FontMetrics fm = graphics.getFontMetrics();
Rectangle2D textBounds = fm.getStringBounds(idText, graphics);
int textX = (int) (slotBounds.getX() + (slotBounds.getWidth() / 2) - (textBounds.getWidth() / 2));
int textY = (int) (slotBounds.getY() + (slotBounds.getHeight() / 2) + (textBounds.getHeight() / 2));
graphics.setColor(new Color(255, 255, 255, 65));
graphics.fill(slotBounds);
graphics.setColor(Color.BLACK);
graphics.drawString(idText, textX + 1, textY + 1);
graphics.setColor(YELLOW);
graphics.drawString(idText, textX, textY);
}
}
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);
}
/**
* Expand the splitRectangle, if it borders on subRectangle, as much as border removed
*
* @param subRectangle Rectangle for checking expansion
* @param splitRectangle Rectangle which is expanded
* @param sub_margin how much border removed
*/
private void expand(Rectangle subRectangle, Rectangle splitRectangle, int sub_margin) {
int subX = (int) subRectangle.getX(), subY = (int) subRectangle.getY();
int subWidth = (int) subRectangle.getWidth(), subHeight = (int) subRectangle.getHeight();
int splitX = (int) splitRectangle.getX(), splitY = (int) splitRectangle.getY();
int splitWidth = (int) splitRectangle.getWidth(), splitHeight = (int) splitRectangle.getHeight();
// Left-directional expansion
if (splitX <= subX) {
splitX = subX - sub_margin;
splitWidth = splitWidth + sub_margin;
}
// Top-directional expansion
if (splitY <= subY) {
splitY = subY - sub_margin;
splitHeight = splitHeight + sub_margin;
}
// Right-directional expansion
if (splitX + splitWidth >= subX + subWidth) {
splitWidth = subX + subWidth + sub_margin - splitX;
}
// Down-directional expansion
if (splitY + splitHeight >= subY + subHeight) {
splitHeight = subY + subHeight + sub_margin - splitY;
}
splitRectangle.setBounds(splitX, splitY, splitWidth, splitHeight);
}
private static ImageIcon convertShapeToIcon(Shape seriesShape) {
Rectangle rectangle = seriesShape.getBounds();
if (rectangle.getWidth() > 0 && rectangle.getHeight() > 0) {
BufferedImage image = new BufferedImage((int) (rectangle.getWidth() - rectangle.getX()),
(int) (rectangle.getHeight() - rectangle.getY()), BufferedImage.TYPE_INT_ARGB);
final Graphics2D graphics = image.createGraphics();
graphics.translate(-rectangle.x, -rectangle.y);
graphics.setColor(Color.BLACK);
graphics.draw(seriesShape);
graphics.dispose();
return new ImageIcon(image);
}
return new ImageIcon();
}
private void renderWiw(Graphics2D g, Object wiw, Color color)
{
g.setColor(color);
if (wiw instanceof WidgetItem)
{
WidgetItem wi = (WidgetItem) wiw;
Rectangle bounds = wi.getCanvasBounds();
g.draw(bounds);
String text = wi.getId() + "";
FontMetrics fm = g.getFontMetrics();
Rectangle2D textBounds = fm.getStringBounds(text, g);
int textX = (int) (bounds.getX() + (bounds.getWidth() / 2) - (textBounds.getWidth() / 2));
int textY = (int) (bounds.getY() + (bounds.getHeight() / 2) + (textBounds.getHeight() / 2));
g.setColor(Color.BLACK);
g.drawString(text, textX + 1, textY + 1);
g.setColor(Color.ORANGE);
g.drawString(text, textX, textY);
}
else
{
Widget w = (Widget) wiw;
g.draw(w.getBounds());
}
}
/**
* create ScreenImage with given
*
* @param roi the rectangle it was taken from
* @param img the BufferedImage
*/
public ScreenImage(Rectangle roi, BufferedImage img) {
_img = img;
_roi = roi;
x = (int) roi.getX();
y = (int) roi.getY();
w = _img.getWidth();
h = _img.getHeight();
onScreen(false);
}
/**
* 2枚の画像を比較し、差分の一覧を取得します。
*
* @param img1 画像1
* @param img1Area 画像1で比較の対象とする範囲
* @param img2 画像2
* @param img2Area 画像2で比較の対象とする範囲
* @return 比較結果の差分データ
*/
public ImageComparedResult compare(BufferedImage img1, Rectangle img1Area, BufferedImage img2, Rectangle img2Area) {
if (img1 == null || img2 == null) {
throw new TestRuntimeException("Both img1 and img2 is required.");
}
LOG.trace("[Compare] image1[w: {}, h: {}; {}]; image2[w: {}, h: {}: {}]", img1.getWidth(), img1.getHeight(),
img1Area, img2.getWidth(), img2.getHeight(), img2Area);
int offsetX = 0;
int offsetY = 0;
BufferedImage image1 = null;
BufferedImage image2 = null;
if (img1Area != null) {
image1 = getSubImage(img1, img1Area);
offsetX = (int) img1Area.getX();
offsetY = (int) img1Area.getY();
} else {
image1 = img1;
}
if (img2Area != null) {
image2 = getSubImage(img2, img2Area);
} else {
image2 = img2;
}
List<Point> sizeDiffPoints = createSizeDiffPoints(image1, image2, offsetX, offsetY);
List<Point> diffPoints = compare(image1, image2, offsetX, offsetY);
return new DiffPoints(diffPoints, sizeDiffPoints);
}
private void drawItem(Graphics2D graphics, Rectangle bounds, WidgetItem item)
{
if (item.getId() == -1)
{
return;
}
final BufferedImage draggedItemImage = itemManager.getImage(item.getId(), item.getQuantity(), false);
final int x = (int) bounds.getX();
final int y = (int) bounds.getY();
graphics.setComposite(AlphaComposite.SrcOver.derive(0.3f));
graphics.drawImage(draggedItemImage, x, y, null);
graphics.setComposite(AlphaComposite.SrcOver);
}
/**
* The bounding box is only valid for a given meshCellSize, i.e. setting it
* independently of the meshCellSize is potentially harmful.
*
* @param box coordinates that define the bounding box for this tile.
*/
public void setBoundingBox(final Rectangle box, final double meshCellSize) {
this.minX = box.getX();
this.minY = box.getY();
this.maxX = box.getMaxX();
this.maxY = box.getMaxY();
this.meshCellSize = meshCellSize;
}
@Override
public void propertyChange(PropertyChangeEvent e) {
String prop = e.getPropertyName();
if (prop.equals(ZoomModel.ZOOM)) {
// mouse point
Point point = getMousePosition(true);
double oldZoom = ((Double) e.getOldValue()).doubleValue();
Rectangle r = getViewport().getViewRect();
double cx = (r.getX() + r.getWidth() / 2) / oldZoom;
double cy = (r.getY() + r.getHeight() / 2) / oldZoom;
double newZoom = ((Double) e.getNewValue()).doubleValue();
r = getViewport().getViewRect();
if (point != null) {// mouse is pointing something
int newX = (int) Math
.round(r.getX() / oldZoom * newZoom + point.getX() / oldZoom * newZoom - point.getX());
int newY = (int) Math
.round(r.getY() / oldZoom * newZoom + point.getY() / oldZoom * newZoom - point.getY());
getHorizontalScrollBar().setValue(newX);
getVerticalScrollBar().setValue(newY);
} else {// mouse is outside from canvas panel
int hv = (int) (cx * newZoom - r.getWidth() / 2);
int vv = (int) (cy * newZoom - r.getHeight() / 2);
getHorizontalScrollBar().setValue(hv);
getVerticalScrollBar().setValue(vv);
}
contents.recomputeSize();
}
}
private static void setTransform(Viewport vp1, Viewport vp2) {
vp2.setTransform(vp1);
final Rectangle rectangle1 = vp1.getViewBounds();
final Rectangle rectangle2 = vp2.getViewBounds();
final double w1 = rectangle1.getWidth();
final double w2 = rectangle2.getWidth();
final double h1 = rectangle1.getHeight();
final double h2 = rectangle2.getHeight();
final double x1 = rectangle1.getX();
final double y1 = rectangle1.getY();
final double cx = (x1 + w1) / 2.0;
final double cy = (y1 + h1) / 2.0;
final double magnification;
if (w1 > h1) {
magnification = w2 / w1;
} else {
magnification = h2 / h1;
}
final Point2D modelCenter = vp1.getViewToModelTransform().transform(new Point2D.Double(cx, cy), null);
final double zoomFactor = vp1.getZoomFactor() * magnification;
if (zoomFactor > 0.0) {
vp2.setZoomFactor(zoomFactor, modelCenter.getX(), modelCenter.getY());
}
}
private void drawRegionOverlay(Graphics2D graphics)
{
RenderOverview ro = client.getRenderOverview();
Widget map = client.getWidget(WidgetInfo.WORLD_MAP_VIEW);
float pixelsPerTile = ro.getWorldMapZoom();
if (map == null)
{
return;
}
Rectangle worldMapRect = map.getBounds();
graphics.setClip(worldMapRect);
int widthInTiles = (int) Math.ceil(worldMapRect.getWidth() / pixelsPerTile);
int heightInTiles = (int) Math.ceil(worldMapRect.getHeight() / pixelsPerTile);
Point worldMapPosition = ro.getWorldMapPosition();
// Offset in tiles from anchor sides
int yTileMin = worldMapPosition.getY() - heightInTiles / 2;
int xRegionMin = (worldMapPosition.getX() - widthInTiles / 2) & REGION_TRUNCATE;
int xRegionMax = ((worldMapPosition.getX() + widthInTiles / 2) & REGION_TRUNCATE) + REGION_SIZE;
int yRegionMin = (yTileMin & REGION_TRUNCATE);
int yRegionMax = ((worldMapPosition.getY() + heightInTiles / 2) & REGION_TRUNCATE) + REGION_SIZE;
int regionPixelSize = (int) Math.ceil(REGION_SIZE * pixelsPerTile);
for (int x = xRegionMin; x < xRegionMax; x += REGION_SIZE)
{
for (int y = yRegionMin; y < yRegionMax; y += REGION_SIZE)
{
graphics.setColor(WHITE_TRANSLUCENT);
int yTileOffset = -(yTileMin - y);
int xTileOffset = x + widthInTiles / 2 - worldMapPosition.getX();
int xPos = ((int) (xTileOffset * pixelsPerTile)) + (int) worldMapRect.getX();
int yPos = (worldMapRect.height - (int) (yTileOffset * pixelsPerTile)) + (int) worldMapRect.getY();
// Offset y-position by a single region to correct for drawRect starting from the top
yPos -= regionPixelSize;
graphics.drawRect(xPos, yPos, regionPixelSize, regionPixelSize);
int regionId = ((x >> 6) << 8) | (y >> 6);
String regionText = String.valueOf(regionId);
FontMetrics fm = graphics.getFontMetrics();
Rectangle2D textBounds = fm.getStringBounds(regionText, graphics);
int labelWidth = (int) textBounds.getWidth() + 2 * LABEL_PADDING;
int labelHeight = (int) textBounds.getHeight() + 2 * LABEL_PADDING;
graphics.fillRect(xPos, yPos, labelWidth, labelHeight);
graphics.setColor(Color.BLACK);
graphics.drawString(regionText, xPos + LABEL_PADDING, yPos + (int) textBounds.getHeight() + LABEL_PADDING);
}
}
}
/**
* Check the sub-image of actualImage in the given rectangle area is contained in expectedImage at the same or
* nearby location if then, create ComparedRectangle with shift information and insert it into ComparedRectangles
* list.
*
* @param expectedImage
* @param actualImage
* @param ComparedRectangles list of ComparedRectangle
* @param rectangle sub-image area of actual image
* @return true if this rectangle is shifted
*/
public static boolean CheckShift(BufferedImage expectedImage, BufferedImage actualImage,
List<ComparedRectangleArea> ComparedRectangles, Rectangle rectangle) {
int minWidth = Math.min(expectedImage.getWidth(), actualImage.getWidth()), minHeight = Math.min(
expectedImage.getHeight(), actualImage.getHeight());
// set range to be checked
int x = (int) rectangle.getX(), y = (int) rectangle.getY(), w = (int) rectangle.getWidth(), h = (int) rectangle
.getHeight();
int maxShift = ComparisonParameterDefaults.getMaxShift();
int leftMove = Math.min(maxShift, x - 1);
int rightMove = Math.min(maxShift, minWidth - (x + w));
int topMove = Math.min(maxShift, y - 1);
int downMove = Math.min(maxShift, minHeight - (y + h));
Rectangle entireFrame = new Rectangle(x - leftMove, y - topMove, w + leftMove + rightMove, h + topMove
+ downMove);
BufferedImage entireImage = ImageUtils.getSubImage(expectedImage, entireFrame);
BufferedImage templateImage = ImageUtils.getSubImage(actualImage, rectangle);
double[][] integralImage = ImageUtils.calcIntegralImage(entireImage);
double sumTemplate = 0;
Raster r = templateImage.getRaster();
int[] dArray = new int[r.getNumDataElements()];
for (int i = 0; i < r.getWidth(); i++) {
for (int j = 0; j < r.getHeight(); j++) {
sumTemplate += r.getPixel(i, j, dArray)[0];
}
}
int templateWidth = templateImage.getWidth();
int templateHeight = templateImage.getHeight();
double topLeft, topRight, bottomLeft, bottomRight;
double sumEntire;
for (int i = 0; i <= topMove + downMove; i++) {
for (int j = 0; j <= leftMove + rightMove; j++) {
bottomRight = integralImage[i + templateHeight - 1][j + templateWidth - 1];
bottomLeft = (j == 0) ? 0 : integralImage[i + templateHeight - 1][j - 1];
topRight = (i == 0) ? 0 : integralImage[i - 1][j + templateWidth - 1];
topLeft = (j == 0 || i == 0) ? 0 : integralImage[i - 1][j - 1];
sumEntire = bottomRight - bottomLeft - topRight + topLeft;
if (Double.compare(sumEntire, sumTemplate) == 0) {
BufferedImage cropEntire = entireImage.getSubimage(j, i, templateWidth, templateHeight);
// If the template matches at this position, create new ComparedRectangle and add it in the list
if (ImageUtils.imageEquals(cropEntire, templateImage)) {
ComparedRectangleArea newMatch = new ComparedRectangleArea(rectangle, leftMove - j, topMove - i);
ComparedRectangles.add(newMatch);
return true;
}
}
}
}
return false;
}