Search in sources :

Example 1 with Point

use of java.awt.Point in project jmonkeyengine by jMonkeyEngine.

the class MapModel2D method toPosition.

/**
     * Converts a pixel position into a mercator position
     * @param p {@link Point} object that you wish to convert into
     *        longitude / latiude
     * @return the converted {@code Position} object
     * @since 1.0
     */
public Position toPosition(Point p) {
    double lat, lon;
    Position pos = null;
    try {
        Point pixelCentre = toPixel(new Position(0, 0));
        // Get the distance between position and the centre
        double xDistance = distance(xCentre, p.getX());
        double yDistance = distance(pixelCentre.getY(), p.getY());
        double lonDistanceInDegrees = (xDistance * minutesPerPixel) / 60;
        double mp = (yDistance * minutesPerPixel);
        // Otherwise use binary search
        if (getMinutesPerPixel() < 0.05) {
            lat = findLat(mp, getCentre().getLatitude());
            if (lat == -1000) {
                System.out.println("lat: " + lat);
            }
        } else {
            lat = findLat(mp, 0.0, 85.0);
        }
        lon = (p.getX() < xCentre ? centre.getLongitude() - lonDistanceInDegrees : centre.getLongitude() + lonDistanceInDegrees);
        if (p.getY() > pixelCentre.getY()) {
            lat = -1 * lat;
        }
        if (lat == -1000 || lon == -1000) {
            return pos;
        }
        pos = new Position(lat, lon);
    } catch (InvalidPositionException ipe) {
        ipe.printStackTrace();
    }
    return pos;
}
Also used : Point(java.awt.Point)

Example 2 with Point

use of java.awt.Point in project jmonkeyengine by jMonkeyEngine.

the class MapModel2D method toPixel.

/**
     * Converts a latitude/longitude position into a pixel co-ordinate
     * @param position the position to convert
     * @return {@code Point} a pixel co-ordinate
     * @since 1.0
     */
public Point toPixel(Position position) {
    // Get the distance between position and the centre for calculating
    // the position's longitude translation
    double distance = NavCalculator.computeLongDiff(centre.getLongitude(), position.getLongitude());
    // Use the distance from the centre to calculate the pixel x co-ordinate
    double distanceInPixels = (distance / minutesPerPixel);
    // Use the difference in meridional parts to calculate the pixel y co-ordinate
    double dmp = NavCalculator.computeDMPClarkeSpheroid(centre.getLatitude(), position.getLatitude());
    int x = 0;
    int y = 0;
    if (centre.getLatitude() == position.getLatitude()) {
        y = yCentre;
    }
    if (centre.getLongitude() == position.getLongitude()) {
        x = xCentre;
    }
    // Distinguish between northern and southern hemisphere for latitude calculations
    if (centre.getLatitude() > 0 && position.getLatitude() > centre.getLatitude()) {
        // Centre is north. Position is north of centre
        y = yCentre + (int) ((dmp) / minutesPerPixel);
    } else if (centre.getLatitude() > 0 && position.getLatitude() < centre.getLatitude()) {
        // Centre is north. Position is south of centre
        y = yCentre - (int) ((dmp) / minutesPerPixel);
    } else if (centre.getLatitude() < 0 && position.getLatitude() > centre.getLatitude()) {
        // Centre is south. Position is north of centre
        y = yCentre + (int) ((dmp) / minutesPerPixel);
    } else if (centre.getLatitude() < 0 && position.getLatitude() < centre.getLatitude()) {
        // Centre is south. Position is south of centre
        y = yCentre - (int) ((dmp) / minutesPerPixel);
    } else if (centre.getLatitude() == 0 && position.getLatitude() > centre.getLatitude()) {
        // Centre is at the equator. Position is north of the equator
        y = yCentre + (int) ((dmp) / minutesPerPixel);
    } else if (centre.getLatitude() == 0 && position.getLatitude() < centre.getLatitude()) {
        // Centre is at the equator. Position is south of the equator
        y = yCentre - (int) ((dmp) / minutesPerPixel);
    }
    // Distinguish between western and eastern hemisphere for longitude calculations
    if (centre.getLongitude() < 0 && position.getLongitude() < centre.getLongitude()) {
        // Centre is west. Position is west of centre
        x = xCentre - (int) distanceInPixels;
    } else if (centre.getLongitude() < 0 && position.getLongitude() > centre.getLongitude()) {
        // Centre is west. Position is south of centre
        x = xCentre + (int) distanceInPixels;
    } else if (centre.getLongitude() > 0 && position.getLongitude() < centre.getLongitude()) {
        // Centre is east. Position is west of centre
        x = xCentre - (int) distanceInPixels;
    } else if (centre.getLongitude() > 0 && position.getLongitude() > centre.getLongitude()) {
        // Centre is east. Position is east of centre
        x = xCentre + (int) distanceInPixels;
    } else if (centre.getLongitude() == 0 && position.getLongitude() > centre.getLongitude()) {
        // Centre is at the equator. Position is east of centre
        x = xCentre + (int) distanceInPixels;
    } else if (centre.getLongitude() == 0 && position.getLongitude() < centre.getLongitude()) {
        // Centre is at the equator. Position is west of centre
        x = xCentre - (int) distanceInPixels;
    }
    // Distinguish between northern and souterhn hemisphere for longitude calculations
    return new Point(x, y);
}
Also used : Point(java.awt.Point) Point(java.awt.Point)

Example 3 with Point

use of java.awt.Point in project jna by java-native-access.

the class ShapedWindowDemo method main.

public static void main(String[] args) {
    try {
        System.setProperty("sun.java2d.noddraw", "true");
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e) {
    }
    final JFrame frame = new JFrame("Shaped Window Demo");
    MouseInputAdapter handler = new MouseInputAdapter() {

        private Point offset;

        private void showPopup(MouseEvent e) {
            final JPopupMenu m = new JPopupMenu();
            m.add(new AbstractAction("Hide") {

                public void actionPerformed(ActionEvent e) {
                    frame.setState(JFrame.ICONIFIED);
                }

                private static final long serialVersionUID = 1L;
            });
            m.add(new AbstractAction("Close") {

                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }

                private static final long serialVersionUID = 1L;
            });
            m.pack();
            m.show(e.getComponent(), e.getX(), e.getY());
        }

        public void mousePressed(MouseEvent e) {
            offset = e.getPoint();
            if (e.isPopupTrigger()) {
                showPopup(e);
            }
        }

        public void mouseDragged(MouseEvent e) {
            if (!SwingUtilities.isLeftMouseButton(e))
                return;
            Point where = e.getPoint();
            where.translate(-offset.x, -offset.y);
            Point loc = frame.getLocationOnScreen();
            loc.translate(where.x, where.y);
            frame.setLocation(loc.x, loc.y);
        }

        public void mouseReleased(MouseEvent e) {
            if (e.isPopupTrigger()) {
                showPopup(e);
            }
        }
    };
    frame.addMouseListener(handler);
    frame.addMouseMotionListener(handler);
    ClockFace face = new ClockFace(new Dimension(150, 150));
    frame.getContentPane().setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
    frame.getContentPane().add(face);
    frame.setUndecorated(true);
    try {
        Shape mask = new Area(new Ellipse2D.Float(0, 0, 150, 150));
        WindowUtils.setWindowMask(frame, mask);
        if (WindowUtils.isWindowAlphaSupported()) {
            WindowUtils.setWindowAlpha(frame, .7f);
        }
        frame.setIconImage(face.getIconImage());
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocation(100, 100);
        frame.setVisible(true);
    } catch (UnsatisfiedLinkError e) {
        e.printStackTrace();
        String msg = e.getMessage() + "\nError loading the JNA library";
        JTextArea area = new JTextArea(msg);
        area.setOpaque(false);
        area.setFont(UIManager.getFont("Label.font"));
        area.setEditable(false);
        area.setColumns(80);
        area.setRows(8);
        area.setWrapStyleWord(true);
        area.setLineWrap(true);
        JOptionPane.showMessageDialog(frame, new JScrollPane(area), "Library Load Error: " + System.getProperty("os.name") + "/" + System.getProperty("os.arch"), JOptionPane.ERROR_MESSAGE);
        System.exit(1);
    }
}
Also used : JScrollPane(javax.swing.JScrollPane) MouseEvent(java.awt.event.MouseEvent) Shape(java.awt.Shape) JTextArea(javax.swing.JTextArea) ActionEvent(java.awt.event.ActionEvent) Point(java.awt.Point) Dimension(java.awt.Dimension) JPopupMenu(javax.swing.JPopupMenu) Ellipse2D(java.awt.geom.Ellipse2D) Area(java.awt.geom.Area) JTextArea(javax.swing.JTextArea) JFrame(javax.swing.JFrame) AbstractAction(javax.swing.AbstractAction) MouseInputAdapter(javax.swing.event.MouseInputAdapter)

Example 4 with Point

use of java.awt.Point in project jna by java-native-access.

the class AlphaMaskDemo method updateX11.

private void updateX11(boolean a, boolean i) {
    X11 x11 = X11.INSTANCE;
    X11.Window win = X11.Window.None;
    Display dpy = x11.XOpenDisplay(null);
    try {
        if (!alphaWindow.isDisplayable()) {
            alphaWindow.pack();
            if (System.getProperty("java.version").matches("^1\\.4\\..*"))
                alphaWindow.setVisible(true);
            win = new X11.Window((int) Native.getWindowID(alphaWindow));
            Window parent = alphaWindow.getOwner();
            Point where = parent.getLocationOnScreen();
            where.translate(parent.getWidth(), 0);
            alphaWindow.removeAll();
            alphaWindow.setLocation(where);
            alphaWindow.setBackground(new Color(0, 0, 0, 0));
        } else {
            win = new X11.Window((int) Native.getWindowID(alphaWindow));
        }
        if (i) {
            int w = image.getWidth(null);
            int h = image.getHeight(null);
            alphaWindow.setSize(w, h);
            if (buffer == null || buffer.size() != w * h * 4) {
                buffer = new com.sun.jna.Memory(w * h * 4);
                pixels = new int[w * h];
            }
            BufferedImage buf = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB_PRE);
            Graphics g = buf.getGraphics();
            g.drawImage(image, 0, 0, w, h, null);
            long start = System.currentTimeMillis();
            long blitTime, putImageTime, write;
            GC gc = x11.XCreateGC(dpy, win, new NativeLong(0), null);
            long gcTime = System.currentTimeMillis();
            try {
                Raster raster = buf.getData();
                int[] pixel = new int[4];
                for (int y = 0; y < h; y++) {
                    for (int x = 0; x < w; x++) {
                        raster.getPixel(x, y, pixel);
                        int alpha = (pixel[3] & 0xFF) << 24;
                        int red = (pixel[2] & 0xFF);
                        int green = (pixel[1] & 0xFF) << 8;
                        int blue = (pixel[0] & 0xFF) << 16;
                        pixels[y * w + x] = alpha | red | green | blue;
                    }
                }
                blitTime = System.currentTimeMillis();
                X11.XWindowAttributes xwa = new X11.XWindowAttributes();
                x11.XGetWindowAttributes(dpy, win, xwa);
                X11.XImage image = x11.XCreateImage(dpy, xwa.visual, 32, X11.ZPixmap, 0, buffer, w, h, 32, w * 4);
                buffer.write(0, pixels, 0, pixels.length);
                write = System.currentTimeMillis();
                x11.XPutImage(dpy, win, gc, image, 0, 0, 0, 0, w, h);
                x11.XFree(image.getPointer());
                putImageTime = System.currentTimeMillis();
            } finally {
                if (gc != null)
                    x11.XFreeGC(dpy, gc);
            }
            long end = System.currentTimeMillis();
        //System.out.println("gc: " + (gcTime-start) + "ms");
        //System.out.println("blit: " + (blitTime-gcTime) + "ms");
        //System.out.println("write: " + (write-blitTime) + "ms");
        //System.out.println("put image: " + (putImageTime-write) + "ms");
        //System.out.println("total: " + (end-start) + "ms");
        }
    } finally {
        if (dpy != null)
            x11.XCloseDisplay(dpy);
    }
    if (a)
        WindowUtils.setWindowAlpha(alphaWindow, alpha);
    if (!alphaWindow.isVisible()) {
        alphaWindow.setVisible(true);
        // hack for initial refresh (X11)
        update(true, true);
    }
}
Also used : Window(java.awt.Window) JWindow(javax.swing.JWindow) Color(java.awt.Color) NativeLong(com.sun.jna.NativeLong) Raster(java.awt.image.Raster) Point(java.awt.Point) Point(java.awt.Point) BufferedImage(java.awt.image.BufferedImage) Graphics(java.awt.Graphics) X11(com.sun.jna.platform.unix.X11) GC(com.sun.jna.platform.unix.X11.GC) Display(com.sun.jna.platform.unix.X11.Display)

Example 5 with Point

use of java.awt.Point in project jna by java-native-access.

the class AlphaMaskDemo method centerOnScreen.

/** Center the given {@link Window} on the default screen. */
private static void centerOnScreen(Window window) {
    Point center = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
    Rectangle max = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
    int x = Math.max(center.x - Math.round(window.getWidth() / 2f), max.x);
    int y = Math.max(center.y - Math.round(window.getHeight() / 2f), max.y);
    window.setLocation(new Point(x, y));
}
Also used : Rectangle(java.awt.Rectangle) Point(java.awt.Point) Point(java.awt.Point)

Aggregations

Point (java.awt.Point)2146 Dimension (java.awt.Dimension)522 Rectangle (java.awt.Rectangle)282 ActionEvent (java.awt.event.ActionEvent)174 ActionListener (java.awt.event.ActionListener)165 ChangeEvent (javax.swing.event.ChangeEvent)138 ChangeListener (javax.swing.event.ChangeListener)137 ArrayList (java.util.ArrayList)125 MouseEvent (java.awt.event.MouseEvent)119 JLabel (javax.swing.JLabel)112 JButton (javax.swing.JButton)93 JPanel (javax.swing.JPanel)82 MouseAdapter (java.awt.event.MouseAdapter)81 BufferedImage (java.awt.image.BufferedImage)75 Color (java.awt.Color)73 Graphics2D (java.awt.Graphics2D)73 ImageIcon (javax.swing.ImageIcon)73 Component (java.awt.Component)63 JSlider (javax.swing.JSlider)62 Font (java.awt.Font)60