下面列出了java.awt.Point#distance ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Draw a box on screen.
*
* @param component The {@code JComponent} to draw on.
* @param startPoint The starting {@code Point}.
* @param endPoint The end {@code Point}.
*/
private void drawBox(JComponent component,
Point startPoint, Point endPoint) {
if (startPoint == null || endPoint == null
|| startPoint.distance(endPoint) == 0
|| getFreeColClient().getMapEditorController() == null)
return;
Graphics2D graphics = (Graphics2D)component.getGraphics();
graphics.setColor(Color.WHITE);
int x = Math.min(startPoint.x, endPoint.x);
int y = Math.min(startPoint.y, endPoint.y);
int width = Math.abs(startPoint.x - endPoint.x);
int height = Math.abs(startPoint.y - endPoint.y);
graphics.drawRect(x, y, width, height);
}
protected void mouseReleased(MouseEvent e) {
Key key = new Key(e);
Point clickLoc = clickLocs.get(key);
if (clickLoc == null)
return;
Point releaseLoc = e.getPoint();
double distance = releaseLoc.distance(clickLoc);
if (distance > getClickPixelTolerance(e.getComponent())) {
clickLocs.remove(key);
return;
}
SwingUtilities.invokeLater(new TriggerMouseClick(key, e));
}
@Override
public void mouseDragged(MouseEvent e)
{
if (SwingUtilities.isLeftMouseButton(e) && dragStartPoint != null)
{
Point point = e.getPoint();
if (draggingComponent != null)
{
drag(point);
}
else if (point.distance(dragStartPoint) > DragSource.getDragThreshold())
{
startDragging(point);
}
}
}
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();
}
}
/** Gets the meter per pixel.
*
* @return the meter per pixel */
public double getMeterPerPixel() {
Point origin = new Point(5, 5);
Point center = new Point(getWidth() / 2, getHeight() / 2);
double pDistance = center.distance(origin);
ICoordinate originCoord = getPosition(origin);
ICoordinate centerCoord = getPosition(center);
double mDistance = tileSource.getDistance(originCoord.getLat(), originCoord.getLon(), centerCoord.getLat(), centerCoord.getLon());
return mDistance / pDistance;
}
/**
* We add the provided glyph to a time signature composed of single
* digits.
*
* @param glyph the provided (single-digit) glyph
* @param measure the containing measure
* @param staff the related staff
* @return true if successful
*/
private static boolean populatePartialTime (Glyph glyph,
Measure measure,
Staff staff)
{
logger.debug("populatePartialTime with {}", glyph);
TimeSignature ts = measure.getTimeSignature(staff);
if (ts != null) {
// Check we are not too far from this first time signature part
Point center = measure.computeGlyphCenter(glyph);
double dist = center.distance(ts.getCenter());
double max = measure.getScale().toPixelsDouble(
constants.maxTimeDistance);
if (dist > max) {
logger.debug("Time signature part ({}" + ")"
+ " too far from previous one", glyph.idString());
return false;
}
} else {
// Start a brand new time sig
ts = new TimeSignature(measure, staff);
}
ts.addGlyph(glyph);
glyph.setTranslation(ts);
return true;
}
protected void mouseDragged(MouseEvent e) {
Key key = new Key(e);
Point clickLoc = clickLocs.get(key);
if (clickLoc == null)
return;
Point releaseLoc = e.getPoint();
double distance = releaseLoc.distance(clickLoc);
if (distance > getClickPixelTolerance(e.getComponent())) {
clickLocs.remove(key);
return;
}
}
/**
* Gets the shape to be filled in the draw method.
*
* @param vidPanel the video panel
* @return the line shape
*/
protected Shape getShape(VideoPanel vidPanel) {
this.center(tip, tail);
Point p1 = tail.getScreenPosition(vidPanel); // tail
Point p2 = tip.getScreenPosition(vidPanel); // tip
// set up transform
double theta = Math.atan2(p2.y-p1.y, p2.x-p1.x);
rotation.setToRotation(theta, p1.x, p1.y);
rotation.translate(p1.x, p1.y);
// get line length d
float d = (float) p1.distance(p2);
// set up head hit shape usng full line length
path.reset();
path.moveTo(d-4, 0);
path.lineTo(d-6, -2);
path.lineTo(d, 0);
path.lineTo(d-6, 2);
path.closePath();
head = rotation.createTransformedShape(path);
// shorten line length to account for stroke width
// see Java 2D API Graphics, by VJ Hardy (Sun, 2000) page 147
float w = stroke.getLineWidth();
d = d-1.58f*w;
// set up shaft hit shape using shortened line length
line.setLine(0, 0, d-length, 0);
shaft = rotation.createTransformedShape(line);
// set up and return fill shape usng shortened line length
path.reset();
path.moveTo(0, 0);
path.lineTo(d-length+width, 0);
path.lineTo(d-length, -width);
path.lineTo(d, 0);
path.lineTo(d-length, width);
path.lineTo(d-length+width, 0);
Shape vector = rotation.createTransformedShape(path);
return stroke.createStrokedShape(vector);
}
/**
* Gets the meter per pixel.
*
* @return the meter per pixel
* @author Jason Huntley
*/
public double getMeterPerPixel() {
Point origin = new Point(5, 5);
Point center = new Point(getWidth() / 2, getHeight() / 2);
double pDistance = center.distance(origin);
Coordinate originCoord = getPosition(origin);
Coordinate centerCoord = getPosition(center);
double mDistance = tileSource.getDistance(originCoord.getLat(), originCoord.getLon(), centerCoord.getLat(),
centerCoord.getLon());
return mDistance / pDistance;
}
public double calcAverageDistanceToPoint(@Nonnull final Point point) {
final double d1 = point.distance(this.bounds.getX(), this.bounds.getY());
final double d2 = point.distance(this.bounds.getMaxX(), this.bounds.getY());
final double d3 = point.distance(this.bounds.getX(), this.bounds.getMaxY());
final double d4 = point.distance(this.bounds.getMaxX(), this.bounds.getMaxY());
return (d1 + d2 + d3 + d4) / (this.bounds.contains(point) ? 8.0d : 4.0d);
}
private void mouseCheck(final MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
float dx = mouseX - joyCenterX;
float dy = mouseY - joyCenterY;
if (leftMouseButton) {
isMouseTracking = true;
} else {
isMouseTracking = false;
}
if (isMouseTracking) {
curJoyAngle = (float) Math.atan2(dy, dx);
curJoySize = (float) Point.distance(mouseX, mouseY,
joyCenterX, joyCenterY);
} else {
curJoySize = 0;
}
if (curJoySize > joySize) {
curJoySize = joySize;
}
position.x = (int) (joyOutputRange * (Math.cos(curJoyAngle)
* curJoySize) / joySize);
position.y = (int) (joyOutputRange * (-(Math.sin(curJoyAngle)
* curJoySize) / joySize));
repaint();
callPositionListeners();
sendMessage();
}
public static void segmentMinDistance(List<MarvinSegment> segments, double minDistance){
MarvinSegment s1,s2;
for(int i=0; i<segments.size()-1; i++){
for(int j=i+1; j<segments.size(); j++){
s1 = segments.get(i);
s2 = segments.get(j);
if(Point.distance( (s1.x1+s1.x2)/2, (s1.y1+s1.y2)/2, (s2.x1+s2.x2)/2, (s2.y1+s2.y2)/2 ) < minDistance){
segments.remove(s2);
j--;
}
}
}
}
@Override
public void mousePressed(MouseEvent e)
{
Point mouse = e.getPoint();
int scrollFactor = scrollBar.getValue()*10;
final int OFFSET = 5;
final int DIAMETER = 50;
int colorNum = 0;
for (int i = 0; i < palette.size(); i++)
{
Color color = palette.get(i);
final int SEPARATION = 10 + DIAMETER;
int circX = colorNum*SEPARATION + OFFSET + DIAMETER/2 - scrollFactor;
int circY = OFFSET;
if (mouse.distance(circX, circY) < DIAMETER/2+OFFSET)
{
if (e.getButton() == 3)
{
palette.remove(color);
fireChangeListeners();
updateScrollBar();
repaint();
}
}
colorNum++;
}
// Add a new color to the palette if the user clicks the "+" button
int cX = colorNum*60 + OFFSET + DIAMETER/2 - scrollFactor;
int cY = OFFSET;
if (mouse.distance(cX, cY) < DIAMETER/2+OFFSET)
{
palette.add(getCurrentColor());
fireChangeListeners();
updateScrollBar();
repaint();
}
}
private double distanceToPanel(Point point, Panel panel)
{
int panelx = panel.getX();
int panely = panel.getY();
return point.distance(panelx, panely);
}
public void findHits(World world) {
selfHits=0;
strange=0;
for(int emitted=0;emitted<rays;emitted++){
//creating ray
Point start=firesimulator.util.Geometry.getRndPoint(a,b);
if(start==null){
strange++;
LOG.debug("strange -> "+a.x+","+a.y+"/"+b.x+","+b.y);
continue;
}
Point end=firesimulator.util.Geometry.getRndPoint(start,MAX_SAMPLE_DISTANCE);
//intersect
Wall closest=null;
double minDist=Double.MAX_VALUE;
for(Iterator it=world.allWalls.iterator();it.hasNext();){
Wall other=(Wall)it.next();
if(other==this) continue;
Point cross=firesimulator.util.Geometry.intersect(start,end,other.a,other.b);
if(cross!=null){
if(cross.distance(start)<minDist){
minDist=cross.distance(start);
closest=other;
}
}
}
if(closest == null){
//Nothing was hit
continue;
}
if(closest.owner==this.owner){
//The source building was hit
selfHits++;
}
if(closest!=this&&closest!=null&&closest.owner!=owner){
hits++;
Integer value=(Integer)owner.connectedBuildings.get(closest.owner);
int temp = 0;
if(value != null){
temp = value.intValue();
}
temp++;
owner.connectedBuildings.put(closest.owner,new Integer(temp));
}
}
}
public double getDistance(CoordinatesInWorld other) {
return Point.distance(xInWorld, yInWorld, other.xInWorld, other.yInWorld);
}
public double getDistance(CoordinatesInWorld other) {
return Point.distance(xInWorld, yInWorld, other.xInWorld, other.yInWorld);
}
public double getDistance(long xInWorld, long yInWorld) {
return Point.distance(this.xInWorld, this.yInWorld, xInWorld, yInWorld);
}
public boolean canDeployDoor(Point pos) {
Point toStep = calcPointBelow(pos);
return toStep != null && toStep.distance(pos) <= 42;
}
/**
* Checks whether two nodes are in wireless communication range.
*
* @param first position of the first node
* @param second position of the second node
* @param minPosition minimum position on the map
* @param maxPosition maximum position on the map
* @return {@code true} if the nodes are in range, {@code false} otherwise
*/
private boolean inRange(Point first, Point second, int minPosition, int maxPosition) {
return Point.distance(first.x, first.y, second.x, second.y)
<= (maxRange * traceLength / (maxPosition - minPosition));
}