下面列出了java.awt.Rectangle#getWidth ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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);
}
}
/**
* calculate similarity of given rectangle area and offset, but similarityFeatureMatrix is given. And then, build
* similarRectangle using similarity values.
*
* @param expectedImage
* @param actualImage
* @param rectangle
* @param similarRectangle
* @param offset
* @param similarityFeatureMatrix in the case of "SCALING", we use similarity already calculated. in the other
* cases, it has -1.
*/
public static SimilarityUnit calcSimilarity(BufferedImage expectedImage, BufferedImage actualImage,
Rectangle rectangle, ComparedRectangleArea similarRectangle, Offset offset, double similarityFeatureMatrix) {
Offset featureOffset = offset;
SimilarityUnit similarityUnit = calcSimilarityPixelByPixel(expectedImage, actualImage, rectangle, offset);
/* calculate similarity using feature matrix. */
int comparedRectangleWidth = (int) rectangle.getWidth(), comparedRectangleHeight = (int) rectangle.getHeight();
// execute this only when comparedRectangleWidth >= FeatureCol && comparedRectangleHeight >= FeatureRow
if (similarityFeatureMatrix == -1
&& SimilarityUtils.checkFeatureSize(comparedRectangleWidth, comparedRectangleHeight)) {
similarityFeatureMatrix = calcSimilarityByFeatureMatrix(expectedImage, actualImage, rectangle,
featureOffset);
}
similarityUnit.setSimilarityFeatureMatrix(similarityFeatureMatrix);
if (similarRectangle.getCategory() == null) {
similarRectangle.setCategory(DiffCategory.SIMILAR);
}
return similarityUnit;
}
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());
}
}
private static ImageIcon convertStrokeToIcon(Stroke stroke) {
Shape strokeShape = new Line2D.Double(-40, 0, 40, 0);
final Rectangle rectangle = strokeShape.getBounds();
BufferedImage image = new BufferedImage((int) (rectangle.getWidth() - rectangle.getX()),
1,
BufferedImage.TYPE_INT_ARGB);
final Graphics2D graphics = image.createGraphics();
graphics.translate(-rectangle.x, -rectangle.y);
graphics.setColor(Color.BLACK);
graphics.setStroke(stroke);
graphics.draw(strokeShape);
graphics.dispose();
return new ImageIcon(image);
}
/**
* I've got a big, hairy expression and it doesn’t do what I expect.
*/
public static void main(String[] args) {
Rectangle rect = new Rectangle(0, 0, 10, 10);
Rectangle ngle = new Rectangle(-5, -5, -5, -5);
System.out.println(intersection(rect, ngle));
double halfWidth = 0.5 * rect.getWidth();
double halfHeight = 0.5 * rect.getHeight();
int dx = (int) Math.round(halfWidth);
int dy = (int) Math.round(halfHeight);
rect.translate(dx, dy);
}
public void generateActual(OutputStream out) throws Exception {
Rectangle rect = mapContext.getViewport().getScreenArea();
BufferedImage image = new BufferedImage((int) rect.getWidth(), (int) rect.getHeight(),
BufferedImage.TYPE_4BYTE_ABGR);
layer.draw(image.createGraphics(), mapContext, mapContext.getViewport());
ImageIO.write(image, "PNG", out);
}
/**
* Changes the displayed bounds of the object relative to its x,y width and height
*
* Implemented because the canvas has no layout manager
*
*/
protected final void updateBounds() {
Rectangle bounds = shape.getBounds();
Rectangle newBounds = new Rectangle((int)(model.getCentre().getX() + bounds.getX()), (int)(model.getCentre().getY() + bounds.getY()), (int) bounds.getWidth() + getComponentDrawOffset(), (int)bounds.getHeight() + getComponentDrawOffset()) ;
setBounds(newBounds);
// setBounds(model.getX(), model.getY(), model.getWidth() + getComponentDrawOffset(), model.getHeight() + getComponentDrawOffset());
//TODO: THIS IS A DIRTY HACK IN ORDER TO GET DRAGGIGN WHEN ZOOMED WORKING
Component root = SwingUtilities.getRoot(this);
if (root != null) {
root.repaint();
}
}
@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();
}
}
/**
* Center a frame on the main display
*
* @param frame
* The frame to center
*/
public static void centerFrameOnMainDisplay(final JFrame frame) {
final GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
final GraphicsDevice[] screens = ge.getScreenDevices();
if (screens.length < 1) {
return; // Silently fail.
}
final Rectangle screenBounds = screens[0].getDefaultConfiguration()
.getBounds();
final int x = (int) ((screenBounds.getWidth() - frame.getWidth()) / 2);
final int y = (int) ((screenBounds.getHeight() - frame.getHeight()) / 2);
frame.setLocation(x, y);
}
@Override
public Component getTableCellRendererComponent (
JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column
) {
JLabel c = (JLabel)defaultRenderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (value instanceof String) {
Rectangle cellRect = table.getCellRect(row, column, false);
String scCell = (String) value;
Dimension d = new Dimension((int) cellRect.getWidth(), (int) cellRect.getHeight());
if (panel == null)
panel = new ShortcutCellPanel(scCell);
panel.setText(scCell);
panel.setSize(d);
if (isSelected) {
panel.setBgColor(table.getSelectionBackground());
if (UIManager.getLookAndFeel ().getID ().equals ("GTK"))
panel.setFgCOlor(table.getForeground(), true);
else
panel.setFgCOlor(table.getSelectionForeground(), true);
} else {
panel.setBgColor(c.getBackground());
panel.setFgCOlor(c.getForeground(), false);
}
if (hasFocus) {
panel.setBorder(c.getBorder());
} else {
panel.setBorder(null);
}
return panel;
}
else {
return c;
}
}
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 Point clipToRectangle(Point drawPoint, Rectangle mapDisplayRectangle)
{
int clippedX = drawPoint.getX();
if (drawPoint.getX() < mapDisplayRectangle.getX())
{
clippedX = (int) mapDisplayRectangle.getX();
}
if (drawPoint.getX() > mapDisplayRectangle.getX() + mapDisplayRectangle.getWidth())
{
clippedX = (int) (mapDisplayRectangle.getX() + mapDisplayRectangle.getWidth());
}
int clippedY = drawPoint.getY();
if (drawPoint.getY() < mapDisplayRectangle.getY())
{
clippedY = (int) mapDisplayRectangle.getY();
}
if (drawPoint.getY() > mapDisplayRectangle.getY() + mapDisplayRectangle.getHeight())
{
clippedY = (int) (mapDisplayRectangle.getY() + mapDisplayRectangle.getHeight());
}
return new Point(clippedX, clippedY);
}
@Override
protected void paintComponent(Graphics graphics) {
super.paintComponent(graphics);
double scaleX = (double) getWidth() / (double) processRenderer.getWidth();
double scaleY = (double) getHeight() / (double) processRenderer.getHeight();
scale = Math.min(scaleX, scaleY);
double scaledW = processRenderer.getWidth() * scale;
double scaledH = processRenderer.getHeight() * scale;
Graphics2D g = (Graphics2D) graphics.create();
g.translate((getWidth() - scaledW) / 2d, (getHeight() - scaledH) / 2d);
g.scale(scale, scale);
g.setRenderingHints(ProcessDrawer.LOW_QUALITY_HINTS);
processRenderer.getOverviewPanelDrawer().draw(g, true);
g.setStroke(new BasicStroke((int) (1d / scale)));
Rectangle visibleRect = processRenderer.getVisibleRect();
Rectangle drawRect = new Rectangle((int) visibleRect.getX(), (int) visibleRect.getY(),
(int) visibleRect.getWidth() - 1, (int) visibleRect.getHeight() - 1);
g.setColor(FILL_COLOR);
g.fill(drawRect);
g.setColor(DRAW_COLOR);
g.draw(drawRect);
g.dispose();
}
private static boolean pressOK(Component comp) {
JInternalFrame internalFrame
= findModalInternalFrame(comp, QUESTION);
if (internalFrame == null) {
return false;
}
JButton button = (JButton) findButton(internalFrame);
if (button == null) {
return false;
}
try {
Robot robot = new Robot();
Point location = button.getLocationOnScreen();
Rectangle bounds = button.getBounds();
int centerX = (int) (location.getX() + bounds.getWidth() / 2);
int centerY = (int) (location.getY() + bounds.getHeight() / 2);
robot.mouseMove(centerX, centerY);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
} catch (IllegalComponentStateException ignore) {
return false;
} catch (AWTException e) {
throw new RuntimeException(e);
}
return true;
}
/**
* Retuns points which can be used to click on path.
*
* @param row a row index to click on.
* @return a Point in component's coordinate system.
*/
public Point getPointToClick(int row) {
Rectangle rect = getRowBounds(row);
if (rect != null) {
return (new Point((int) (rect.getX() + rect.getWidth() / 2),
(int) (rect.getY() + rect.getHeight() / 2)));
} else {
throw (new NoSuchPathException(row));
}
}
/**
* Initializes a number of things about this frame.
*/
private void initializeFrame() {
frame = new JFrame(Messages.getString("ClientGUI.title")); //$NON-NLS-1$
frame.setJMenuBar(menuBar);
Rectangle virtualBounds = getVirtualBounds();
int x, y, w, h;
if (GUIPreferences.getInstance().getWindowSizeHeight() != 0) {
x = GUIPreferences.getInstance().getWindowPosX();
y = GUIPreferences.getInstance().getWindowPosY();
w = GUIPreferences.getInstance().getWindowSizeWidth();
h = GUIPreferences.getInstance().getWindowSizeHeight();
if ((x < virtualBounds.getMinX())
|| ((x + w) > virtualBounds.getMaxX())) {
x = 0;
}
if ((y < virtualBounds.getMinY())
|| ((y + h) > virtualBounds.getMaxY())) {
y = 0;
}
if (w > virtualBounds.getWidth()) {
w = (int) virtualBounds.getWidth();
}
if (h > virtualBounds.getHeight()) {
h = (int) virtualBounds.getHeight();
}
frame.setLocation(x, y);
frame.setSize(w, h);
} else {
frame.setSize(800, 600);
}
frame.setMinimumSize(new Dimension(640, 480));
frame.setBackground(SystemColor.menu);
frame.setForeground(SystemColor.menuText);
List<Image> iconList = new ArrayList<Image>();
iconList.add(frame.getToolkit().getImage(
new MegaMekFile(Configuration.miscImagesDir(), FILENAME_ICON_16X16)
.toString()));
iconList.add(frame.getToolkit().getImage(
new MegaMekFile(Configuration.miscImagesDir(), FILENAME_ICON_32X32)
.toString()));
iconList.add(frame.getToolkit().getImage(
new MegaMekFile(Configuration.miscImagesDir(), FILENAME_ICON_48X48)
.toString()));
iconList.add(frame.getToolkit().getImage(
new MegaMekFile(Configuration.miscImagesDir(), FILENAME_ICON_256X256)
.toString()));
frame.setIconImages(iconList);
mechW = new JDialog(frame, Messages.getString("ClientGUI.MechDisplay"), false);
x = GUIPreferences.getInstance().getDisplayPosX();
y = GUIPreferences.getInstance().getDisplayPosY();
h = GUIPreferences.getInstance().getDisplaySizeHeight();
w = GUIPreferences.getInstance().getDisplaySizeWidth();
if ((x + w) > virtualBounds.getWidth()) {
x = 0;
w = Math.min(w, (int)virtualBounds.getWidth());
}
if ((y + h) > virtualBounds.getHeight()) {
y = 0;
h = Math.min(h, (int)virtualBounds.getHeight());
}
mechW.setLocation(x, y);
mechW.setSize(w, h);
mechW.setResizable(true);
unitDisplay = new UnitDisplay(null);
mechW.add(unitDisplay);
mechW.setVisible(true);
unitDisplay.displayEntity(testEntity);
}
/**
* sets the rectangles/bounds for each node
* also sets the bounds for the entire grid
*/
private void updateGridRectangles() {
//create/set rectangles for each location in grid
for (int row = 0; row < gridTable.getNumRows(); ++row) {
for (int col = 0; col < gridTable.getNumCols(); ++col) {
//find the bounds from the cell at this row/col
//use that rectangle as the node's rectangle
String cellID = "ROW" + row + "_COL" + col;
if (graph != null && graph.getGridRectangleCellFromID(cellID) != null) {
//set the component sizes (which may have changed due to zooming)
String compId = grid.get(row).get(col).getComponent();
if (compId != null) {
mxCell compCell = graph.getComponentCell(compId);
if (compCell != null) {
Rectangle compCellRect = graph.getCellGeometry(compCell).getRectangle();
componentGeomHeight = compCellRect.getHeight();
componentGeomWidth = compCellRect.getWidth();
}
}
//set the zoom scale
zoomAmount = graph.getView().getScale();
//get the bounds information from the graph and set the grid
//rectangle's data to fit with the graph's data
mxCell cell = graph.getGridRectangleCellFromID(cellID);
Rectangle cellGeom = graph.getCellGeometry(cell).getRectangle();
gridGeomHeight = cellGeom.getHeight();
gridGeomWidth = cellGeom.getWidth();
gridHeight = gridGeomHeight * zoomAmount;
gridWidth = gridGeomWidth * zoomAmount;
//set the current padding value (which may have changed due to zooming)
//this is usually going to be 30
padding = gridGeomWidth - componentGeomWidth;
grid.get(row).get(col).setRectangle(cellGeom);
}
}
}
//set the total grid bounds
int outerX = (int)gridWidth * gridTable.getNumCols();
int outerY = (int)gridHeight * gridTable.getNumRows() + verticalOffset;
gridBounds.setBounds(15, 15, outerX, outerY);
}
public RotatedRectangle(Rectangle r, double theta) {
this(r.getX(), r.getY(), r.getWidth(), r.getHeight(), theta);
}
@Override
public Transition2DInstruction[] getInstructions(float progress,
Dimension size) {
Rectangle r1 = new Rectangle(0, 0, size.width, size.height);
float k1 = 0.6f;
float k2 = 0.95f - k1;
float k3 = (1 - k1) / 2;
float cutOff = 0.4f;
Rectangle2D r2;
if (type == RIGHT) {
r2 = new Rectangle2D.Float(k2 * r1.width,
k3 * r1.height,
k1 * r1.width,
k1 * r1.height);
} else if (type == LEFT) {
r2 = new Rectangle2D.Float(0.05f * r1.width,
k3 * r1.height,
k1 * r1.width,
k1 * r1.height);
} else if (type == DOWN) {
r2 = new Rectangle2D.Float(k3 * r1.width,
k2 * r1.height,
k1 * r1.width,
k1 * r1.height);
} else { //up
r2 = new Rectangle2D.Float(k3 * r1.width,
0.05f * r1.height,
k1 * r1.width,
k1 * r1.height);
}
float zoomProgress;
float panProgress = (float) (0.5 + 0.5 * sin(PI * (progress * progress - 0.5)));
if (progress < cutOff) {
//we're zooming in
zoomProgress = progress / cutOff;
} else {
zoomProgress = 1;
}
Rectangle2D r3 = new Rectangle2D.Float(
(float) (r2.getX() * (1 - panProgress) + r1.getX() * panProgress),
(float) (r2.getY() * (1 - panProgress) + r1.getY() * panProgress),
(float) (r2.getWidth() * (1 - panProgress) + r1.getWidth() * panProgress),
(float) (r2.getHeight() * (1 - panProgress) + r1.getHeight() * panProgress)
);
List<Transition2DInstruction> v = new ArrayList<>();
AffineTransform t = RectangularTransform.create(r3, r1);
v.add(new ImageInstruction(false, t, r1));
if (zoomProgress != 1) {
v.add(new ImageInstruction(true, 1 - zoomProgress));
}
return v.toArray(new Transition2DInstruction[v.size()]);
}
/**
* Converts an AWT rectangle to a JavaFX bounds object.
* <p/>
* @param rect the rectangle to convert.
* @return the equivalent bounds.
*/
public static Bounds getBoundsFromRect(Rectangle rect) {
return new BoundingBox(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());
}