Example usage for java.awt Point Point

List of usage examples for java.awt Point Point

Introduction

In this page you can find the example usage for java.awt Point Point.

Prototype

public Point(int x, int y) 

Source Link

Document

Constructs and initializes a point at the specified (x,y) location in the coordinate space.

Usage

From source file:Main.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    int w = getSize().width;
    int h = getSize().height;

    Arc2D arc = new Arc2D.Double(0.0, 0.5, w, h, 0.0, 60.0, Arc2D.CHORD);

    g2.draw(arc);/*w  w w .java 2s .c om*/

    arc.setArc(new Point(2, 3), new Dimension(w, h), 80.0f, 110.0f, Arc2D.PIE);
    g2.fill(arc);

    arc = new Arc2D.Float(0.0f, 0.0f, w, h, 210.0f, 130.0f, Arc2D.OPEN);

    g2.draw(arc);
}

From source file:Main.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    int w = getSize().width;
    int h = getSize().height;

    Arc2D arc = new Arc2D.Double(0.0, 0.5, w, h, 0.0, 60.0, Arc2D.CHORD);
    arc.setArcType(Arc2D.PIE);// w w  w .j  av  a2 s  .  c  o  m
    g2.draw(arc);

    arc.setArcByTangent(new Point(2, 3), new Point(3, 4), new Point(3, 4), Arc2D.PIE);
    g2.fill(arc);

    arc = new Arc2D.Float(0.0f, 0.0f, w, h, 210.0f, 130.0f, Arc2D.OPEN);

    g2.draw(arc);
}

From source file:Util.java

/**
 * Returns closest point on segment to point
 * /*from  w ww .  ja v a2s  .  c  o  m*/
 * @param sx1
 *            segment x coord 1
 * @param sy1
 *            segment y coord 1
 * @param sx2
 *            segment x coord 2
 * @param sy2
 *            segment y coord 2
 * @param px
 *            point x coord
 * @param py
 *            point y coord
 * @return closets point on segment to point
 */
public static Point getClosestPointOnSegment(int sx1, int sy1, int sx2, int sy2, int px, int py) {
    double xDelta = sx2 - sx1;
    double yDelta = sy2 - sy1;

    if ((xDelta == 0) && (yDelta == 0)) {
        throw new IllegalArgumentException("Segment start equals segment end");
    }

    double u = ((px - sx1) * xDelta + (py - sy1) * yDelta) / (xDelta * xDelta + yDelta * yDelta);

    final Point closestPoint;
    if (u < 0) {
        closestPoint = new Point(sx1, sy1);
    } else if (u > 1) {
        closestPoint = new Point(sx2, sy2);
    } else {
        closestPoint = new Point((int) Math.round(sx1 + u * xDelta), (int) Math.round(sy1 + u * yDelta));
    }

    return closestPoint;
}

From source file:Main.java

public static Point positionToClickPoint(Container component, int caretPosition, Container invokedIn) {
    if (component == null) {
        return null;
    }//w ww  .j  ava2  s. co  m

    //System.err.println("Checking: " + component.getClass().getName());
    if (component.getClass().getName().indexOf("EditorPane") >= 0) {
        try {
            java.lang.reflect.Method pointGetter = component.getClass().getMethod("modelToView",
                    new Class[] { Integer.TYPE });
            Rectangle rec = (Rectangle) pointGetter.invoke(component,
                    new Object[] { new Integer(caretPosition) });
            //System.err.println("Before: " + (int)rec.getY());
            // FIXME: somehow it fails here to convert point from scrollable component
            Point point = SwingUtilities.convertPoint(component, (int) rec.getX(), (int) rec.getY() + 10,
                    invokedIn);
            // FIXME: ugly hack :(
            if (point.getY() > 1024) {
                point = new Point((int) point.getX(), 250);
            }
            //System.err.println("After: " + (int)point.getY());
            return point;
        } catch (Exception e) {
            System.err.println("Method invocation exception caught");
            e.printStackTrace();

            //FIXME: BUG
            return null;
            //throw new RuntimeException("Method invocation exception caught");
        }
    }

    for (int i = 0; i < component.getComponentCount(); i++) {
        java.awt.Component childComponent = component.getComponent(i);
        if (childComponent instanceof javax.swing.JComponent) {
            Point point = positionToClickPoint((javax.swing.JComponent) childComponent, caretPosition,
                    invokedIn);
            if (point != null) {
                return point;
            }
        }
    }

    return null;
}

From source file:Main.java

/**
 * Returns the center of the given rectangle.
 *
 * @param bounds/*from   w  w w.  j  a  va2 s.  co m*/
 *            The rectangle which center to get
 * @return The center of the rectangle
 */
private static Point getCenter(Rectangle bounds) {
    return new Point(bounds.x + bounds.width / 2, bounds.y + bounds.height / 2);
}

From source file:Main.java

public static void synchronizeView(final JViewport masterViewport, final JViewport slaveViewport,
        final int orientation) {
    final ChangeListener c1 = new ChangeListener() {
        public void stateChanged(ChangeEvent e) {
            if (masterViewport.getView() == null || slaveViewport.getView() == null) {
                return;
            }// w  w w.java  2 s . c o m
            if (orientation == SwingConstants.HORIZONTAL) {
                Point v1 = masterViewport.getViewPosition();
                Point v2 = slaveViewport.getViewPosition();
                if (v1.x != v2.x) {
                    slaveViewport.setViewPosition(new Point(v1.x, v2.y));
                }
            } else if (orientation == SwingConstants.VERTICAL) {
                Point v1 = masterViewport.getViewPosition();
                Point v2 = slaveViewport.getViewPosition();
                if (v1.y != v2.y) {
                    slaveViewport.setViewPosition(new Point(v2.x, v1.y));
                }
            }
        }
    };

    masterViewport.addChangeListener(c1);
}

From source file:Main.java

public static void centerFrameOnScreen(JFrame frame) {

    Toolkit toolkit = Toolkit.getDefaultToolkit();
    Dimension screenSize = toolkit.getScreenSize();
    Dimension frameSize = frame.getSize();
    if (frameSize.height > screenSize.height) {
        frameSize.height = screenSize.height;
    }// w w w. j a v  a2  s  .com
    if (frameSize.width > screenSize.width) {
        frameSize.width = screenSize.width;
    }
    Point p = new Point((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
    frame.setLocation(p);
}

From source file:com.offbynull.peernetic.debug.visualizer.VisualizerUtils.java

/**
 * Creates a random point within a rectangle.
 * @param width rectangle width/*  ww w . j a v  a  2  s. c o m*/
 * @param height rectangle height
 * @return new random point within rectangle specified by {@code width} and {@code height}
 * @throws IllegalArgumentException if any argument is negative or a special double value (e.g. NaN/infinite/etc..)
 */
public static Point randomPointInRectangle(double width, double height) {
    Validate.inclusiveBetween(0.0, Double.MAX_VALUE, width);
    Validate.inclusiveBetween(0.0, Double.MAX_VALUE, height);
    return new Point((int) (Math.random() * width), (int) (Math.random() * height));
}

From source file:Main.java

public static void setPreferredGeometry(JFrame frame) {

    Toolkit tk = Toolkit.getDefaultToolkit();
    Dimension frameSize = frame.getPreferredSize();
    Dimension screenSize = tk.getScreenSize();

    if (frameSize.height > screenSize.height) {
        frameSize.height = screenSize.height;
    }//from   w ww.j a v  a 2  s  . c om

    if (frameSize.width > screenSize.width) {
        frameSize.width = screenSize.width;
    }

    Point p = new Point((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
    Rectangle rect = new Rectangle(p, frameSize);
    frame.setBounds(rect);
}

From source file:Main.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    Ellipse2D e1 = new Ellipse2D.Double(20.0, 20.0, 80.0, 70.0);
    Ellipse2D e2 = new Ellipse2D.Double(20.0, 70.0, 40.0, 40.0);

    Area a1 = new Area(e1);
    Area a2 = new Area(e2);

    a1.subtract(a2);/*from  ww w  .j av a  2  s.  c  om*/

    g2.setColor(Color.orange);
    g2.fill(a1);

    g2.setColor(Color.black);
    g2.drawString("subtract", 20, 140);

    System.out.println(a1.contains(new Point(50, 50)));
}