Example usage for java.awt.event MouseEvent getX

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

Introduction

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

Prototype

public int getX() 

Source Link

Document

Returns the horizontal x position of the event relative to the source component.

Usage

From source file:ClippedDragImage.java

public void mouseDragged(MouseEvent e) {
    imageX = e.getX();
    imageY = e.getY();/* w w w .j  a v a 2s .c o  m*/
    Rectangle r = getAffectedArea(oldX, oldY, imageX, imageY, imageWidth, imageHeight);
    repaint(r); // repaint just the affected part of the component
    oldX = imageX;
    oldY = imageY;
}

From source file:Main.java

public PopupMenu() {
    super(BoxLayout.Y_AXIS);
    final JPopupMenu menu = new JPopupMenu("Options");
    for (int i = 1; i < 20; i++)
        menu.add(new JMenuItem("Option" + i));

    JLabel clickMe = new JLabel("ClickMe");
    clickMe.setAlignmentX(RIGHT_ALIGNMENT);
    clickMe.addMouseListener(new MouseAdapter() {
        @Override/*  w  ww  .java 2 s .c o m*/
        public void mouseClicked(MouseEvent e) {
            menu.show(e.getComponent(), e.getX(), e.getY());
        }
    });
    add(clickMe);
}

From source file:TextHitInfoDemo.java

public TextHitInfoDemo() {
    addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent me) {
            TextHitInfo hit = mTextLayout.hitTestChar(me.getX() - mX, me.getY() - mY);
            System.out.println(hit);
        }//from ww  w.j av  a  2s.  c om
    });
}

From source file:Main.java

private void myPopupEvent(MouseEvent e) {
    int x = e.getX();
    int y = e.getY();
    JTree tree = (JTree) e.getSource();
    TreePath path = tree.getPathForLocation(x, y);
    if (path == null)
        return;/*ww w  .j  a v a2  s  .  com*/

    DefaultMutableTreeNode rightClickedNode = (DefaultMutableTreeNode) path.getLastPathComponent();

    TreePath[] selectionPaths = tree.getSelectionPaths();

    boolean isSelected = false;
    if (selectionPaths != null) {
        for (TreePath selectionPath : selectionPaths) {
            if (selectionPath.equals(path)) {
                isSelected = true;
            }
        }
    }
    if (!isSelected) {
        tree.setSelectionPath(path);
    }
    if (rightClickedNode.isLeaf()) {
        JPopupMenu popup = new JPopupMenu();
        final JMenuItem refreshMenuItem = new JMenuItem("refresh");
        refreshMenuItem.addActionListener(ev -> System.out.println("refresh!"));
        popup.add(refreshMenuItem);
        popup.show(tree, x, y);
    }
}

From source file:Main.java

public DrawPad() {
    setDoubleBuffered(false);//w w w  .j  a  v a 2 s  . c  o m
    addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            oldX = e.getX();
            oldY = e.getY();
        }
    });
    addMouseMotionListener(new MouseMotionAdapter() {
        public void mouseDragged(MouseEvent e) {
            currentX = e.getX();
            currentY = e.getY();
            if (graphics2D != null)
                graphics2D.drawLine(oldX, oldY, currentX, currentY);
            repaint();
            oldX = currentX;
            oldY = currentY;
        }
    });
}

From source file:SimpleDraw.java

public void mousePressed(MouseEvent me) {
    x1 = me.getX();
    y1 = me.getY();
}

From source file:MainClass.java

public MyCanvas() {
    addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent me) {
            TextHitInfo hit = mTextLayout.hitTestChar(me.getX() - mX, me.getY() - mY);
            System.out.println(hit.getCharIndex());
            System.out.println(hit.getInsertionIndex());
            System.out.println(hit.isLeadingEdge());
        }/*from ww  w  .j a  va  2  s  .c om*/
    });

}

From source file:SimpleDraw.java

public void mouseReleased(MouseEvent me) {
    x2 = me.getX();
    y2 = me.getY();/*from ww  w.  j a  v  a  2 s  .  co m*/
    Shape shape = null;
    if (shapeType.equals("Rectangle")) {
        // a Rectangle cannot have a zero width or height
        if (x1 != x2 || y1 != y2) {
            shape = new Rectangle(x1, y1, x2, y2);
        }
    }
    if (shape != null) {
        this.shapes.add(shape);
        this.repaint();
    }
}

From source file:MouseDragActionPanel.java

public void mouseMoved(MouseEvent evt) {
    int x = evt.getX();
    int y = evt.getY();

    if (getSquare(x, y) >= 0)
        setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
    else/*from   w  w  w  .  j a  va2s .c  om*/
        setCursor(Cursor.getDefaultCursor());
}

From source file:SaveYourDrawingToFile.java

public void mousePressed(MouseEvent e) {
    Point p = new Point(e.getX(), e.getY());
    displayList.add(p);
}