Java Code Examples for java.awt.Point#distance()

The following examples show how to use java.awt.Point#distance() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CanvasMapEditorMouseListener.java    From freecol with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example 2
Source File: ClickSensitivityControl.java    From pumpernickel with MIT License 6 votes vote down vote up
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));
}
 
Example 3
Source File: DragAndDropReorderPane.java    From runelite with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@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);
		}
	}
}
 
Example 4
Source File: MapleMap.java    From mapleLemon with GNU General Public License v2.0 6 votes vote down vote up
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();
    }
}
 
Example 5
Source File: JMapViewer.java    From amodeus with GNU General Public License v2.0 5 votes vote down vote up
/** 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;
}
 
Example 6
Source File: TimeSignature.java    From libreveris with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 7
Source File: MarvinSegment.java    From marvinproject with GNU Lesser General Public License v3.0 5 votes vote down vote up
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--;
			}
		}
	}
}
 
Example 8
Source File: ClickSensitivityControl.java    From pumpernickel with MIT License 5 votes vote down vote up
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;
	}
}
 
Example 9
Source File: Joystick.java    From Ardulink-1 with Apache License 2.0 5 votes vote down vote up
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();
  }
 
Example 10
Source File: TVector.java    From osp with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 11
Source File: JMapViewer.java    From Course_Generator with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 12
Source File: AbstractElement.java    From netbeans-mmd-plugin with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 13
Source File: MapleMap.java    From HeavenMS with GNU Affero General Public License v3.0 4 votes vote down vote up
public boolean canDeployDoor(Point pos) {
    Point toStep = calcPointBelow(pos);
    return toStep != null && toStep.distance(pos) <= 42;
}
 
Example 14
Source File: Palette.java    From nanoleaf-desktop with MIT License 4 votes vote down vote up
@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();
	}
}
 
Example 15
Source File: CoordinatesInWorld.java    From amidst with GNU General Public License v3.0 4 votes vote down vote up
public double getDistance(long xInWorld, long yInWorld) {
	return Point.distance(this.xInWorld, this.yInWorld, xInWorld, yInWorld);
}
 
Example 16
Source File: CoordinatesInWorld.java    From amidst with GNU General Public License v3.0 4 votes vote down vote up
public double getDistance(CoordinatesInWorld other) {
	return Point.distance(xInWorld, yInWorld, other.xInWorld, other.yInWorld);
}
 
Example 17
Source File: CoordinatesInWorld.java    From amidst with GNU General Public License v3.0 4 votes vote down vote up
public double getDistance(CoordinatesInWorld other) {
	return Point.distance(xInWorld, yInWorld, other.xInWorld, other.yInWorld);
}
 
Example 18
Source File: Wall.java    From rcrs-server with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
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));
		}						
	}		
}
 
Example 19
Source File: PanelCanvas.java    From nanoleaf-desktop with MIT License 4 votes vote down vote up
private double distanceToPanel(Point point, Panel panel)
{
	int panelx = panel.getX();
	int panely = panel.getY();
	return point.distance(panelx, panely);
}
 
Example 20
Source File: NCCU.java    From mobemu with MIT License 2 votes vote down vote up
/**
 * 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));
}