下面列出了怎么用java.awt.Point的API类实例代码及写法,或者点击链接到github查看源代码。
public void paint(Graphics g, Point position, int radio) {
// int size_h = radio;
// int size = img. size_h * 2;
if (g instanceof Graphics2D && getBackColor() != null) {
Graphics2D g2 = (Graphics2D) g;
Composite oldComposite = g2.getComposite();
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
g2.setPaint(getBackColor());
g.drawImage(img, position.x - img.getWidth(null) / 2, position.y - img.getHeight(null) / 2, null);
// g.fillOval(position.x - size_h, position.y - size_h, size, size);
g2.setComposite(oldComposite);
}
g.setColor(getColor());
g.drawImage(img, position.x - img.getWidth(null) / 2, position.y - img.getHeight(null) / 2, null);
// g.drawOval(position.x - size_h, position.y - size_h, size, size);
if (getLayer() == null || getLayer().isVisibleTexts())
paintText(g, position);
}
public void mousePressed(MouseEvent e) {
if (model == null) {
return;
}
Point p = e.getPoint();
if (isOverFirstPosition(p)) {
state = State.DragFirstPosition;
} else if (isOverSecondPosition(p)) {
state = State.DragSecondPosition;
} else if (isOverSelection(p)) {
state = State.DragBar;
} else {
return;
}
startPoint = e.getPoint();
tempModel = model.copy();
}
public UnitsDrawer(
final int count,
final String unitType,
final String playerName,
final Point placementPoint,
final int damaged,
final int bombingUnitDamage,
final boolean disabled,
final boolean overflow,
final String territoryName,
final UiContext uiContext) {
this.count = count;
this.unitType = unitType;
this.playerName = playerName;
this.placementPoint = placementPoint;
this.damaged = damaged;
this.bombingUnitDamage = bombingUnitDamage;
this.disabled = disabled;
this.overflow = overflow;
this.territoryName = territoryName;
this.uiContext = uiContext;
}
public static void main(final String[] args) throws AWTException {
final Window window = new BackgroundIsNotUpdated(null);
window.setSize(300, 300);
window.setLocationRelativeTo(null);
window.setVisible(true);
sleep();
window.setBackground(Color.GREEN);
sleep();
final Robot robot = new Robot();
robot.setAutoDelay(200);
Point point = window.getLocationOnScreen();
Color color = robot.getPixelColor(point.x + window.getWidth() / 2,
point.y + window.getHeight() / 2);
window.dispose();
if (!color.equals(Color.GREEN)) {
throw new RuntimeException(
"Expected: " + Color.GREEN + " , Actual: " + color);
}
}
public void mousePressed(MouseEvent e) {
super.mousePressed(e);
if (!toolBar.isEnabled()) {
return;
}
pressedInBumps = false;
Rectangle bumpRect = new Rectangle();
if (toolBar.getOrientation() == JToolBar.HORIZONTAL) {
int x = MetalUtils.isLeftToRight(toolBar) ? 0 : toolBar.getSize().width-14;
bumpRect.setBounds(x, 0, 14, toolBar.getSize().height);
} else { // vertical
bumpRect.setBounds(0, 0, toolBar.getSize().width, 14);
}
if (bumpRect.contains(e.getPoint())) {
pressedInBumps = true;
Point dragOffset = e.getPoint();
if (!MetalUtils.isLeftToRight(toolBar)) {
dragOffset.x -= (toolBar.getSize().width
- toolBar.getPreferredSize().width);
}
setDragOffset(dragOffset);
}
}
public static void packAndCenterWindowOn(Window window, Component centeredOn) {
window.pack();
Dimension prefSize = window.getPreferredSize();
Dimension minSize = window.getMinimumSize();
int width = Math.max(prefSize.width, minSize.width);
int height = Math.max(prefSize.height, minSize.height);
int x;
int y;
if (centeredOn != null) {
Point where = centeredOn.getLocationOnScreen();
Dimension size = centeredOn.getSize();
x = where.x + (size.width - width) / 2;
y = where.y + (size.height - height) / 2;
} else {
Rectangle bounds = getMaximumWindowBounds(window);
x = bounds.x + (bounds.width - width) / 2;
y = bounds.y + (bounds.height - height) / 2;
}
window.setLocation(x, y);
forceOnScreen(window);
}
/**
* Returns the data area (the area inside the axes) for the plot or subplot,
* with the current scaling applied.
*
* @param x the x-coordinate (for subplot selection).
* @param y the y-coordinate (for subplot selection).
*
* @return The scaled data area.
*/
public Rectangle2D getScreenDataArea(int x, int y) {
PlotRenderingInfo plotInfo = this.info.getPlotInfo();
Rectangle2D result;
if (plotInfo.getSubplotCount() == 0) {
result = getScreenDataArea();
}
else {
// get the origin of the zoom selection in the Java2D space used for
// drawing the chart (that is, before any scaling to fit the panel)
Point2D selectOrigin = translateScreenToJava2D(new Point(x, y));
int subplotIndex = plotInfo.getSubplotIndex(selectOrigin);
if (subplotIndex == -1) {
return null;
}
result = scale(plotInfo.getSubplotInfo(subplotIndex).getDataArea());
}
return result;
}
public void broadcastMessage(MapleCharacter source, byte[] packet, double rangeSq, Point rangedFrom) {
this.charactersLock.readLock().lock();
try {
for (MapleCharacter chr : characters) {
if (chr != source) {
if (rangeSq < Double.POSITIVE_INFINITY) {
if (rangedFrom.distance(chr.getTruePosition()) <= rangeSq) {
chr.getClient().getSession().write(packet);
}
} else {
chr.getClient().getSession().write(packet);
}
}
}
} finally {
this.charactersLock.readLock().unlock();
}
}
private Table.UnsignedByte computeRelevantPoints (Area area)
{
Table.UnsignedByte table = new Table.UnsignedByte(rect.width, rect.height);
Point loc = rect.getLocation();
table.fill(PixelFilter.BACKGROUND);
for (int y = 0; y < rect.height; y++) {
int ay = y + loc.y; // Absolute ordinate
for (int x = 0; x < rect.width; x++) {
int ax = x + loc.x; // Absolute abscissa
if (area.contains(ax, ay)) {
table.setValue(x, y, 0);
pointCount++;
}
}
}
return table;
}
/**
* Zooms out.
*/
private void zoomOutToolUsed() {
if (this.setGraphScale(graph.getScale() / this.ZOOM_FACTOR)) {
JViewport view = this.getScroller().getViewport();
Point viewPos = view.getViewPosition();
center = new Point((int) (viewPos.getX() + view.getWidth() / 2), (int) (viewPos.getY() + view.getHeight() / 2));
center.x = (int) (center.getX() / this.ZOOM_FACTOR);
center.y = (int) (center.getY() / this.ZOOM_FACTOR);
updateGridSize();
this.pointToCenter(center);
}
this.repaint();
this.getScroller().repaint();
graph.repaint();
}
private GradientPaint translateGradientPaint(GradientPaint gp) {
Point p1 = translate(gp.getPoint1());
Point p2 = translate(gp.getPoint2());
Color c1 = gp.getColor1();
Color c2 = gp.getColor2();
return new GradientPaint(p1, c1, p2, c2);
}
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;
}
/**
* Ensures that the popup menue is allways inside the application frame
*/
public static void showPopup(JPopupMenu popup, MouseEvent event) {
if (popup == null) {
return;
}
final Component component = event.getComponent();
final Point point = event.getPoint();
popup.show(component, point.x, point.y);
}
/**
* Override for Xinerama case: call new Solaris API for getting the correct
* centering point from the windowing system.
*/
public Point getCenterPoint() {
if (runningXinerama()) {
Point p = getXineramaCenterPoint();
if (p != null) {
return p;
}
}
return super.getCenterPoint();
}
/**
* Use the long source sections to stick to the provided skeleton and return
* the resulting filament.
* <p>
* Strategy: We use only the long sections that intersect core length (height) and are close
* enough to the core mid line.
*
* @param source the input sections
* @param core the oriented core rectangle
*/
private Filament populateCore (Collection<Section> source,
Rectangle core)
{
final Filament fil = new CurvedFilament(scale.getInterline(), params.segmentLength);
for (Section section : source) {
Rectangle sectRect = VERTICAL.oriented(section.getBounds());
// Section with significant length, intersecting core and centroid in core alignment?
if (sectRect.width >= params.minCoreSectionLength) {
if (sectRect.intersects(core)) {
Point oCentroid = VERTICAL.oriented(section.getCentroid());
if (GeoUtil.yEmbraces(core, oCentroid.y)) {
fil.addSection(section);
}
}
}
}
if (!fil.getMembers().isEmpty()) {
return fil;
} else {
return null;
}
}
public final static int getBoundingShape(int xPoints[], int yPoints[], int startAngle, int arcAngle, int centerX,
int centerY, int boundingX, int boundingY, int boundingWidth, int boundingHeight) {
xPoints[0] = centerX;
yPoints[0] = centerY;
Point startPoint = getBoundingPointAtAngle(boundingX, boundingY, boundingWidth, boundingHeight, startAngle);
xPoints[1] = startPoint.x;
yPoints[1] = startPoint.y;
int i = 2;
for (int angle = 0; angle < arcAngle; i++, angle += 90) {
if (angle + 90 > arcAngle
&& ((startAngle + angle - 45) % 360) / 90 == ((startAngle + arcAngle + 45) % 360) / 90) {
break;
}
int modAngle = (startAngle + angle) % 360;
if (modAngle > 315 || modAngle <= 45) {
xPoints[i] = boundingX + boundingWidth;
yPoints[i] = boundingY;
} else if (modAngle > 135 && modAngle <= 225) {
xPoints[i] = boundingX;
yPoints[i] = boundingY + boundingHeight;
} else if (modAngle > 45 && modAngle <= 135) {
xPoints[i] = boundingX;
yPoints[i] = boundingY;
} else {
xPoints[i] = boundingX + boundingWidth;
yPoints[i] = boundingY + boundingHeight;
}
}
Point endPoint = getBoundingPointAtAngle(boundingX, boundingY, boundingWidth, boundingHeight,
(startAngle + arcAngle) % 360);
if (xPoints[i - 1] != endPoint.x || yPoints[i - 1] != endPoint.y) {
xPoints[i] = endPoint.x;
yPoints[i++] = endPoint.y;
}
return i;
}
private void fireTitleDraggedEvent(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_fireTitleDraggedEvent
Point pos = evt.getLocationOnScreen();
if (pos == null) {
return;
}
pos.translate(-dndDelta.x, -dndDelta.y);
setLocation(pos);
}
public boolean handleAttributionCursor(Point p) {
if (attrTextBounds != null && attrTextBounds.contains(p)) {
return true;
} else if (attrImageBounds != null && attrImageBounds.contains(p)) {
return true;
} else if (attrToUBounds != null && attrToUBounds.contains(p)) {
return true;
}
return false;
}
@Override
public Vector3f convertTranslationToDrag(final Camera camera, final Vector3f nodeLocation, final Point from, final Point to) {
// Calculate the vector representing the drag (in window coordinates)
final int dx = to.x - from.x;
final int dy = to.y - from.y;
final Vector3f movement = new Vector3f(dx, dy, 0);
// Calculate and return the vector representing the drag in graph coordinates.
final Vector3f newposition = new Vector3f();
Graphics3DUtilities.moveByProjection(nodeLocation, getCameraModelViewProjectionMatrix(camera), getViewport(), movement, newposition);
return newposition;
}
/**
* Create a compatible WritableRaster with the specified size, a new
* SampleModel, and a new initialized DataBuffer.
* @param w the specified width of the new <code>WritableRaster</code>
* @param h the specified height of the new <code>WritableRaster</code>
* @return a compatible <code>WritableRaster</code> with the specified
* size and a new sample model and data buffer.
* @exception RasterFormatException if the width or height is less than
* or equal to zero.
*/
public WritableRaster createCompatibleWritableRaster(int w, int h) {
if (w <= 0 || h <=0) {
throw new RasterFormatException("negative " +
((w <= 0) ? "width" : "height"));
}
SampleModel sm = sampleModel.createCompatibleSampleModel(w,h);
return new SunWritableRaster(sm, new Point(0,0));
}
/**
* Restores the auto-range calculation on the range axis.
*/
public void restoreAutoRangeBounds() {
Plot p = this.chart.getPlot();
if (p instanceof ValueAxisPlot) {
Zoomable z = (Zoomable) p;
// we need to guard against this.zoomPoint being null
org.eclipse.swt.graphics.Point zp =
(this.zoomPoint != null ? this.zoomPoint
: new org.eclipse.swt.graphics.Point(0, 0));
z.zoomRangeAxes(0.0, this.info.getPlotInfo(),
SWTUtils.toAwtPoint(zp));
}
}
public void mouseClicked(MouseEvent e) {
// todo (#pf): we need to solve problem between click and double
// click - click should be possible only on the check box area
// and double click should be bordered by title text.
// we need a test how to detect where the mouse pointer is
JTree tree = (JTree) e.getSource();
Point p = e.getPoint();
int x = e.getX();
int y = e.getY();
int row = tree.getRowForLocation(x, y);
TreePath path = tree.getPathForRow(row);
// if path exists and mouse is clicked exactly once
if( null == path )
return;
Node node = Visualizer.findNode( path.getLastPathComponent() );
if( null == node )
return;
Rectangle chRect = CheckRenderer.getCheckBoxRectangle();
Rectangle rowRect = tree.getPathBounds(path);
chRect.setLocation(chRect.x + rowRect.x, chRect.y + rowRect.y);
if (e.getClickCount() == 1 && chRect.contains(p)) {
boolean isSelected = settings.isNodeVisible( node );
settings.setNodeVisible( node, !isSelected );
tree.repaint();
}
}
/**
* Returns the x,y position of the event relative to the source component.
*
* @return a <code>Point</code> object containing the x and y coordinates
* relative to the source component
*
*/
public Point getPoint() {
int x;
int y;
synchronized (this) {
x = this.x;
y = this.y;
}
return new Point(x, y);
}
@Override
protected Shape getDropIndication( TopComponent draggedTC, Point location ) {
location = SwingUtilities.convertPoint( getComponent(), location, getTabDisplayer() );
Path2D res = new Path2D.Double();
Rectangle tabRect = getTabDisplayer().dropIndication( draggedTC, location );
if( null != tabRect ) {
tabRect = SwingUtilities.convertRectangle( getTabDisplayer(), tabRect, container );
res.append( tabRect, false );
}
res.append( container.getContentArea(), false );
return res;
}
public static Point getPoint(String widget, int part, int state, int property) {
readLock.lock();
try {
return getPoint(getTheme(widget), part, state, property);
} finally {
readLock.unlock();
}
}
public void runTest() throws Exception {
try {
SwingUtilities.invokeAndWait(this::setupUI);
robot.waitForIdle();
SwingUtilities.invokeAndWait(() ->
scrollPane.getViewport().scrollRectToVisible(comboBox.getBounds())
);
robot.waitForIdle();
// Move mouse pointer to the center of the combo box
Point p = comboBox.getLocationOnScreen();
Dimension d = comboBox.getSize();
robot.mouseMove(p.x + d.width / 2, p.y + d.height / 2);
// The currently visible rectangle in scrollPane
Rectangle viewRect0 = Util.invokeOnEDT(scrollPane.getViewport()::getViewRect);
// Scroll the scrollPane with mouse wheel
robot.mouseWheel(1);
robot.waitForIdle();
// The updated rectangle
Rectangle viewRect1 = Util.invokeOnEDT(scrollPane.getViewport()::getViewRect);
if (viewRect0.y == viewRect1.y) {
throw new RuntimeException("Mouse wheel should have scrolled the JScrollPane");
}
} finally {
if (frame != null) {
frame.dispose();
}
}
System.out.println("Test passed");
}
/**
* Retrieve the glyphs out of buffer runs.
*
* @return the map of spot glyphs per system
*/
public Map<SystemInfo, List<Glyph>> getSpots ()
{
RunTable headRuns = sheet.getPicture().getTable(Picture.TableKey.HEAD_SPOTS);
List<Glyph> spots = GlyphFactory.buildGlyphs(
headRuns,
new Point(0, 0),
GlyphGroup.HEAD_SPOT);
// Dispose the runTable
sheet.getPicture().removeTable(Picture.TableKey.HEAD_SPOTS);
// Dispatch spots per system(s)
return dispatchSheetSpots(spots);
}
private void scrollHome(JScrollPane scrollpane) {
JViewport vp = scrollpane.getViewport();
Component view;
if (vp != null && (view = vp.getView()) != null) {
if (scrollpane.getComponentOrientation().isLeftToRight()) {
vp.setViewPosition(new Point(0, 0));
} else {
Rectangle visRect = vp.getViewRect();
Rectangle bounds = view.getBounds();
vp.setViewPosition(new Point(bounds.width - visRect.width, 0));
}
}
}
/**
* Creates a Raster with the same layout but using a different
* width and height, and with new zeroed data arrays.
*/
public WritableRaster createCompatibleWritableRaster(int w, int h) {
if (w <= 0 || h <=0) {
throw new RasterFormatException("negative "+
((w <= 0) ? "width" : "height"));
}
SampleModel sm = sampleModel.createCompatibleSampleModel(w, h);
return new ByteComponentRaster(sm , new Point(0,0));
}
Point getDropLocation( TopComponentDroppable droppable, int dropMargin ) {
Dimension size = droppable.getDropComponent().getSize();
Point res = new Point();
switch( side ) {
case center:
res.x = size.width/2;
res.y = size.height/2;
break;
case top:
res.x = size.width/2;
res.y = dropMargin;
break;
case bottom:
res.x = size.width/2;
res.y = size.height-dropMargin;
break;
case left:
res.x = dropMargin;
res.y = size.height/2;
break;
case right:
res.x = size.width-dropMargin;
res.y = size.height/2;
break;
}
return res;
}