Example usage for java.awt.event MouseEvent getXOnScreen

List of usage examples for java.awt.event MouseEvent getXOnScreen

Introduction

In this page you can find the example usage for java.awt.event MouseEvent getXOnScreen.

Prototype

public int getXOnScreen() 

Source Link

Document

Returns the absolute horizontal x position of the event.

Usage

From source file:Main.java

public static void main(String[] args) {
    String[] items = { "Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat" };
    JList<String> myJList = new JList(items) {
        @Override//  w  w  w  .  j a  v a2  s.  c  om
        protected void processMouseEvent(MouseEvent e) {
            int modifiers = e.getModifiers() | InputEvent.CTRL_MASK;
            int modifiersEx = e.getModifiersEx() | InputEvent.CTRL_MASK;
            MouseEvent myME = new MouseEvent((Component) e.getSource(), e.getID(), e.getWhen(), modifiers,
                    e.getX(), e.getY(), e.getXOnScreen(), e.getYOnScreen(), e.getClickCount(),
                    e.isPopupTrigger(), e.getButton());
            super.processMouseEvent(myME);
        }
    };
    JFrame f = new JFrame();
    f.add(new JScrollPane(myJList));
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.pack();
    f.setLocationRelativeTo(null);
    f.setVisible(true);
}

From source file:Main.java

/**
 * Returns an appropriate location for a component's tool tip that <i>always</i>
 * lies within the specified frame.//w  ww . j av a2s. c  o  m
 * <p>
 * Intended be used in custom implementations of {@link JComponent#getToolTipLocation(MouseEvent)}.
 *
 * @param e
 *          the event that caused the display of the tool tip
 * @param c
 *          the parent component of the tool tip
 * @param frame
 *          a component in which the tool tip has to fit (usually the surrounding window of "c")
 * @return
 */
public static Point getAdjustedToolTipLocation(MouseEvent e, JComponent c, Component frame) {
    JToolTip tip = new JToolTip();
    tip.setTipText(c.getToolTipText(e));
    Dimension tipSize = tip.getPreferredSize();
    // Tool tip will be positioned within the bounds of the specified component (+ 5px inset)
    Rectangle frameR = frame.getBounds();
    if (frame instanceof Container) {
        Container container = (Container) frame;
        Insets insets = container.getInsets();
        frameR.x += insets.left;
        frameR.y += insets.top;
        frameR.width -= (insets.left + insets.right);
        frameR.height -= (insets.top + insets.bottom);
    }
    frameR.x += 5;
    frameR.y += 5;
    frameR.width -= 10;
    frameR.height -= 10;
    // Initial try for the tool tip's position
    Rectangle r = new Rectangle(e.getXOnScreen(), c.getLocationOnScreen().y + c.getSize().height + 1,
            tipSize.width, tipSize.height);
    // Check if it fits within the frame
    Rectangle intersection = frameR.intersection(r);
    if (r.equals(intersection)) {
        // Tool tip is fully visible within the frame --> use default behaviour
        //
        // Note: The implementation of ToolTipManager.showTipWindow() is not always
        // correct in dual screen mode. The tool tip is _always_ put on that screen,
        // where the most part of the frame lies upon, even if we return coordinates
        // that clearly belong to the other screen. Unfortunately we cannot change
        // that behavior... (bsh 2010-11-24)
        return null;
    }
    // Otherwise, move the tool tip
    int correction = 0;
    if (r.height == intersection.height) {
        // Height is okay, just move left. To make it look better, position the
        // tip 5px below the component.
        r = new Rectangle(r.x, c.getLocationOnScreen().y + c.getSize().height + 5, tipSize.width,
                tipSize.height);
        correction = -5; // needed to make the ToolTipManager use a lightweight pop-up
    } else {
        // The height does not fit. Position the tool tip above the component.
        r = new Rectangle(c.getLocationOnScreen().x + 10, c.getLocationOnScreen().y - tipSize.height - 1,
                tipSize.width, tipSize.height);
    }
    // Adjust to frame bounds
    intersection = frameR.intersection(r);
    intersection.x -= (r.width - intersection.width);
    intersection.y -= (r.height - intersection.height);
    // Return value is expected to be relative to the component's position
    return new Point((-c.getLocationOnScreen().x) + intersection.x + correction,
            (-c.getLocationOnScreen().y) + intersection.y);
}

From source file:Main.java

public Main() {
    setLayout(null);// w  ww.j a v a2  s .c  o  m
    add(button);
    button.setSize(button.getPreferredSize());
    button.setLocation(20, 20);
    addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent event) {
            System.out.println(event.getXOnScreen());
        }
    });
}

From source file:hwcdhackwaterloo.GUIMain.java

/**
 * Creates new form NewJFrame/*from ww  w.j a  v a  2  s.c o m*/
 */
public GUIMain() {
    initComponents();
    menuPanel.addMouseListener(new MouseAdapter() {

        public void mousePressed(MouseEvent e) {
            posX = e.getX();
            posY = e.getY();
        }
    });
    menuPanel.addMouseMotionListener(new MouseAdapter() {
        public void mouseDragged(MouseEvent evt) {
            //sets frame position when mouse dragged         
            setLocation(evt.getXOnScreen() - posX, evt.getYOnScreen() - posY);

        }
    });
}

From source file:project16.Home16.java

private void pnl_sidebarMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_pnl_sidebarMouseDragged
    // TODO add your handling code here:
    int x = evt.getXOnScreen();
    int y = evt.getYOnScreen();
    this.setLocation(x - xx, y - xy);
}

From source file:org.exist.launcher.Launcher.java

private boolean initSystemTray() {
    final Dimension iconDim = tray.getTrayIconSize();
    BufferedImage image = null;//from  w ww  . ja  va  2  s. c om
    try {
        image = ImageIO.read(getClass().getResource("icon32.png"));
    } catch (final IOException e) {
        showMessageAndExit("Launcher failed", "Failed to read system tray icon.", false);
    }
    trayIcon = new TrayIcon(image.getScaledInstance(iconDim.width, iconDim.height, Image.SCALE_SMOOTH),
            "eXist-db Launcher");

    final JDialog hiddenFrame = new JDialog();
    hiddenFrame.setUndecorated(true);
    hiddenFrame.setIconImage(image);

    final PopupMenu popup = createMenu();
    trayIcon.setPopupMenu(popup);
    trayIcon.addActionListener(actionEvent -> {
        trayIcon.displayMessage(null, "Right click for menu", TrayIcon.MessageType.INFO);
        setServiceState();
    });

    // add listener for left click on system tray icon. doesn't work well on linux though.
    if (!SystemUtils.IS_OS_LINUX) {
        trayIcon.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent mouseEvent) {
                if (mouseEvent.getButton() == MouseEvent.BUTTON1) {
                    setServiceState();
                    hiddenFrame.add(popup);
                    popup.show(hiddenFrame, mouseEvent.getXOnScreen(), mouseEvent.getYOnScreen());
                }
            }
        });
    }

    try {
        hiddenFrame.setResizable(false);
        hiddenFrame.pack();
        hiddenFrame.setVisible(true);
        tray.add(trayIcon);
    } catch (final AWTException e) {
        return false;
    }

    return true;
}

From source file:org.wandora.application.gui.topicpanels.RTopicPanel.java

public void showMenu(String[] struct, MouseEvent evt) {
    menu = UIBox.makePopupMenu(struct, this);
    menu.setLocation(evt.getXOnScreen() - 2, evt.getYOnScreen() - 2);
    menu.show(evt.getComponent(), evt.getX() - 2, evt.getY() - 2);
}

From source file:org.wandora.application.gui.topicpanels.ProcessingTopicPanel.java

private void showMenu(String[] struct, MouseEvent evt) {
    menu = UIBox.makePopupMenu(struct, this);
    menu.setLocation(evt.getXOnScreen() - 2, evt.getYOnScreen() - 2);
    menu.show(evt.getComponent(), evt.getX() - 2, evt.getY() - 2);
}

From source file:pl.edu.icm.visnow.geometries.viewer3d.Display3DPanel.java

/**
 * Creates new form Display3DPanel/*from w  ww  .ja  v a 2s .  c o  m*/
 */
public Display3DPanel() {
    initComponents();
    effectiveHeight = getHeight();
    effectiveWidth = getWidth();
    logger.debug("creating Display3DPanel");
    this.setMinimumSize(new Dimension(200, 200));
    this.setPreferredSize(new Dimension(800, 600));
    GraphicsConfigTemplate3D template = new GraphicsConfigTemplate3D();
    template.setStereo(GraphicsConfigTemplate3D.PREFERRED);

    // Get the GraphicsConfiguration that best fits our needs.
    logger.debug("getting config");
    GraphicsConfiguration gcfg = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()
            .getBestConfiguration(template);
    logger.debug("creating canvas");
    canvas = new Canvas3D(gcfg) {
        @Override
        public void postRender() {
            vGraphics = super.getGraphics2D();
            vGraphics.setFont(new Font("sans-serif", Font.PLAIN, 10));
            vGraphics.setColor(Color.YELLOW);
            locToWin.update();
            fireProjectionChanged(new ProjectionEvent(this, locToWin));
            draw2D(vGraphics, locToWin, effectiveWidth, effectiveHeight);
            vGraphics.flush(false);
        }

        @Override
        public void postSwap() {
            if (postRenderSilent || waitForExternalTrigger) {
                return;
            }
            if (!(storingJPEG || storingPNG || storingFrames)) {
                fireFrameRendered();
            }
            if (storingFrames) {
                if (storingJPEG) {
                    writeImage(new File(controlsPanel.getMovieCreationPanel().getCurrentFrameFileName()));
                } else {
                    writeYUV(controlsPanel.getMovieCreationPanel().getGenericFrameFileName());
                }
            }
        }
    };
    canvas.setStereoEnable(false);
    add("Center", canvas);

    pickObject = new PickObject(canvas, objScene, rootObject.getGeometryObj(), objRotate);

    canvas.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent evt) {
            if (evt.isShiftDown()) {
                //              if (e.isShiftDown())
                //                 System.out.println(""+e.getX() + " " + e.getY());
            }
            if (evt.getButton() == MouseEvent.BUTTON1) {
                if (evt.getClickCount() > 1) {
                    reset();
                } else if (pickObject.consumeEmulated3DPick(evt.getX(), evt.getY())) {
                    /* Nothing must be done - everything was done in consumeEmulated3DPick() */
                } else {
                    pickObject.consume2DSelectModule(evt.getX(), evt.getY());
                }
                rootObject.firePickChanged(evt, locToWin);
            }
            if (evt.getButton() == MouseEvent.BUTTON3) {
                if (getControlsFrame() != null) {
                    getControlsFrame().setBounds(evt.getXOnScreen() - 130, evt.getYOnScreen(), 240, 500);
                    getControlsFrame().setVisible(true);
                } else if (getTransientControlsFrame() != null) {
                    getTransientControlsFrame().setBounds(evt.getXOnScreen() - 130, evt.getYOnScreen(), 240,
                            500);
                    getTransientControlsFrame().setVisible(true);
                } else if (application != null) {
                    application.getFrames().getGuiPanel().selectModule(name);
                }
            }
            if (evt.getButton() == MouseEvent.BUTTON2) {
                reset();
            }
        }

        @Override
        public void mouseEntered(java.awt.event.MouseEvent evt) {
            mouseOn = true;
            mouseObserverThread = new Thread(new MouseObserver());
            mouseObserverThread.start();
        }

        @Override
        public void mouseExited(java.awt.event.MouseEvent evt) {
            mouseObserverThread = null;
            mouseOn = false;
        }
    });

    canvas.addMouseMotionListener(new java.awt.event.MouseMotionListener() {
        @Override
        public void mouseDragged(MouseEvent e) {
            //              if (e.isShiftDown())
            //                 System.out.println(""+e.getX() + " " + e.getY());
        }

        @Override
        public void mouseMoved(MouseEvent e) {
        }
    });

    canvas.addMouseWheelListener(new java.awt.event.MouseWheelListener() {
        @Override
        public void mouseWheelMoved(java.awt.event.MouseWheelEvent evt) {
            rescaleFromMouseWheel(evt);
        }
    });

    canvas.addKeyListener(new java.awt.event.KeyAdapter() {
        @Override
        public void keyTyped(KeyEvent evt) {
            formKeyTyped(evt);
        }

        @Override
        public void keyPressed(KeyEvent evt) {
            formKeyPressed(evt);
        }

        @Override
        public void keyReleased(KeyEvent evt) {
            formKeyReleased(evt);
        }
    });

    ToolTipManager.sharedInstance().setLightWeightPopupEnabled(true);
    universe = new SimpleUniverse(canvas);
    view = canvas.getView();
    view.setProjectionPolicy(View.PERSPECTIVE_PROJECTION);
    view.setTransparencySortingPolicy(View.TRANSPARENCY_SORT_GEOMETRY);

    objScene.addChild(rootObject.getGeometryObj());
    rootObject.setRenderingWindow(this);
    rootObject.getGeometryObj().setUserData(null);

    bg.setCapability(Background.ALLOW_COLOR_WRITE);
    bg.setCapability(Background.ALLOW_COLOR_READ);
    bg.setCapability(Background.ALLOW_IMAGE_WRITE);
    bg.setCapability(Background.ALLOW_IMAGE_READ);
    bg.setCapability(Background.ALLOW_IMAGE_SCALE_MODE_READ);
    bg.setCapability(Background.ALLOW_IMAGE_SCALE_MODE_WRITE);
    bg.setImageScaleMode(Background.SCALE_FIT_ALL);
    bg.setApplicationBounds(bounds);

    myFog.setCapability(LinearFog.ALLOW_DISTANCE_WRITE);
    myFog.setCapability(LinearFog.ALLOW_COLOR_WRITE);
    myFog.setInfluencingBounds(bounds);
    myFog.setFrontDistance(1.);
    myFog.setBackDistance(4.);
    myFogGroup.addChild(myFog);

    mouseRotate.setTransformGroup(objRotate);
    mouseRotate.setSchedulingBounds(bounds);
    mouseRotate.setFactor(mouseRotateSensitivity);

    mouseTranslate.setTransformGroup(objRotate);
    mouseTranslate.setSchedulingBounds(bounds);
    mouseTranslate.setFactor(mouseTranslateSensitivity);

    mouseZoom.setTransformGroup(objRotate);
    mouseZoom.setSchedulingBounds(bounds);

    windowRootObject.addChild(mouseRotate);
    windowRootObject.addChild(mouseTranslate);
    windowRootObject.addChild(mouseZoom);

    objTranslate.addChild(objScene);
    objScale.addChild(objTranslate);
    objRotate.addChild(bg);
    objRotate.addChild(objScale);
    windowRootObject.addChild(objRotate);

    ambientLight = new EditableAmbientLight(new Color3f(.25f, .25f, .25f), bounds, "ambient light", true);
    windowRootObject.addChild(ambientLight.getLight());

    directionalLights.add(new EditableDirectionalLight(new Color3f(0.4f, 0.4f, 0.4f),
            new Vector3f(.1f, .08f, -.5f), bounds, "light 1", true, true));
    directionalLights.add(new EditableDirectionalLight(new Color3f(0.2f, 0.15f, 0.1f),
            new Vector3f(-1.f, -0.4f, -.5f), bounds, "light 2", true, false));
    directionalLights.add(new EditableDirectionalLight(new Color3f(0.1f, 0.1f, 0.15f),
            new Vector3f(0.f, 0.6f, -1.f), bounds, "light 3", true, false));
    directionalLights.add(new EditableDirectionalLight(new Color3f(0.1f, 0.1f, 0.15f),
            new Vector3f(0.f, -0.6f, -1.f), bounds, "light 3", false, false));
    for (int i = 0; i < directionalLights.size(); i++) {
        TransformGroup lightTransform = new TransformGroup();
        lightTransform.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        lightTransform.setName("light" + i + "Transform");
        directionalLightTransforms.add(lightTransform);
        OpenBranchGroup lightGroup = new OpenBranchGroup();
        EditableDirectionalLight light = directionalLights.get(i);
        lightGroup.addChild(light.getLight());
        lightGroup.addChild(light.getBackLight());
        lightTransform.addChild(lightGroup);
        windowRootObject.addChild(lightTransform);
    }
    modelClip.setCapability(ModelClip.ALLOW_ENABLE_READ);
    modelClip.setCapability(ModelClip.ALLOW_ENABLE_WRITE);
    modelClip.setCapability(ModelClip.ALLOW_PLANE_READ);
    modelClip.setCapability(ModelClip.ALLOW_PLANE_WRITE);
    modelClip.setInfluencingBounds(bounds);
    objScene.addChild(modelClip);

    pointLights
            .add(new EditablePointLight(new Color3f(0.4f, 0.4f, 0.4f), bounds, null, null, "light 1", false));
    pointLights
            .add(new EditablePointLight(new Color3f(0.4f, 0.4f, 0.4f), bounds, null, null, "light 1", false));
    pointLights
            .add(new EditablePointLight(new Color3f(0.4f, 0.4f, 0.4f), bounds, null, null, "light 1", false));
    for (int i = 0; i < pointLights.size(); i++) {
        TransformGroup lightTransform = new TransformGroup();
        lightTransform.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        lightTransform.setName("light" + i + "Transform");
        pointLightTransforms.add(lightTransform);
        OpenBranchGroup lightGroup = new OpenBranchGroup();
        EditablePointLight light = pointLights.get(i);
        lightGroup.addChild(light.getLight());
        lightTransform.addChild(lightGroup);
        windowRootObject.addChild(lightTransform);
    }
    alternateTransformObj.addChild(alternateRootObj);
    alternateTransormBranch.addChild(alternateTransformObj);
    windowRootObject.addChild(alternateTransormBranch);

    universe.getViewingPlatform().setNominalViewingTransform();
    universe.addBranchGraph(windowRootObject);

    view.getPhysicalBody().getLeftEyePosition(defaultLeftEye);
    view.getPhysicalBody().getRightEyePosition(defaultRightEye);
    controlsFrame = new Display3DControlsFrame(this);
    controlsPanel = controlsFrame.getControlPanel();
    universe.getViewingPlatform().getViewPlatformTransform().getTransform(initialCameraTransform);

    objReper.addChild(reper.getGeometryObj());
    positionedReper.addChild(objReper);
    positionedReper.setTransform(
            new Transform3D(new float[] { .15f, 0, 0, -.8f, 0, .15f, 0, .76f, 0, 0, .15f, 0, 0, 0, 0, 1 }));
    reperGroup.addChild(positionedReper);
    OpenBranchGroup reperLightGroup = new OpenBranchGroup();
    reperLightGroup.addChild(new AmbientLight(new Color3f(.6f, .6f, .6f)));
    reperLightGroup.addChild(new EditableDirectionalLight(new Color3f(0.4f, 0.4f, 0.4f),
            new Vector3f(.1f, .08f, -1f), bounds, "light 1", true, true).getLight());
    reperGroup.addChild(reperLightGroup);
    universe.addBranchGraph(reperGroup);
    locToWin = new LocalToWindow(rootObject.getGeometryObj(), canvas);

    /**
     * The line below binds reper with scene rotation when using any method that calls
     * objRotate.setTransform(), so: keyboard (explicitly calling), mouse (Java3D is calling
     * setTransform) and some custom ways (e.g. in haptic pointer)
     */
    objRotate.addTransformListener(objReper);
    //        mouseRotate.setupCallback(objReper); // it is not needed, because Java3D calls objRotate.setTranform which fires callback to the reper
}