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:game.Clue.ClueGameUI.java

public void mouseDragged(MouseEvent me) {
    if (player == null) {
        return;//from   ww w  .  j a  va 2 s  .  c o m
    }
    // Scanner scan=new Scanner(System.in);
    int valid = 0;

    player.setLocation(me.getX() + xAdjustment, me.getY() + yAdjustment);

}

From source file:com.xmage.launcher.XMageLauncher.java

private XMageLauncher() {
    locale = Locale.getDefault();
    //locale = new Locale("it", "IT");
    messages = ResourceBundle.getBundle("MessagesBundle", locale);
    localize();//from   w ww . j av  a  2s .  co m

    serverConsole = new XMageConsole("XMage Server console");
    clientConsole = new XMageConsole("XMage Client console");

    frame = new JFrame(messages.getString("frameTitle") + " " + Config.getVersion());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setPreferredSize(new Dimension(800, 500));
    frame.setResizable(false);

    createToolbar();

    ImageIcon icon = new ImageIcon(XMageLauncher.class.getResource("/icon-mage-flashed.png"));
    frame.setIconImage(icon.getImage());

    Random r = new Random();
    int imageNum = 1 + r.nextInt(17);
    ImageIcon background = new ImageIcon(new ImageIcon(
            XMageLauncher.class.getResource("/backgrounds/" + Integer.toString(imageNum) + ".jpg")).getImage()
                    .getScaledInstance(800, 480, Image.SCALE_SMOOTH));
    mainPanel = new JLabel(background) {
        @Override
        public Dimension getPreferredSize() {
            Dimension size = super.getPreferredSize();
            Dimension lmPrefSize = getLayout().preferredLayoutSize(this);
            size.width = Math.max(size.width, lmPrefSize.width);
            size.height = Math.max(size.height, lmPrefSize.height);
            return size;
        }
    };
    mainPanel.addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent e) {
            grabPoint = e.getPoint();
            mainPanel.getComponentAt(grabPoint);
        }
    });
    mainPanel.addMouseMotionListener(new MouseMotionAdapter() {
        @Override
        public void mouseDragged(MouseEvent e) {

            // get location of Window
            int thisX = frame.getLocation().x;
            int thisY = frame.getLocation().y;

            // Determine how much the mouse moved since the initial click
            int xMoved = (thisX + e.getX()) - (thisX + grabPoint.x);
            int yMoved = (thisY + e.getY()) - (thisY + grabPoint.y);

            // Move window to this position
            int X = thisX + xMoved;
            int Y = thisY + yMoved;
            frame.setLocation(X, Y);
        }
    });
    mainPanel.setLayout(new GridBagLayout());

    GridBagConstraints constraints = new GridBagConstraints();
    constraints.insets = new Insets(10, 10, 10, 10);

    Font font16 = new Font("Arial", Font.BOLD, 16);
    Font font12 = new Font("Arial", Font.PLAIN, 12);
    Font font12b = new Font("Arial", Font.BOLD, 12);

    mainPanel.add(Box.createRigidArea(new Dimension(250, 50)));

    ImageIcon logo = new ImageIcon(new ImageIcon(XMageLauncher.class.getResource("/label-xmage.png")).getImage()
            .getScaledInstance(150, 75, Image.SCALE_SMOOTH));
    xmageLogo = new JLabel(logo);
    constraints.gridx = 3;
    constraints.gridy = 0;
    constraints.gridheight = 1;
    constraints.gridwidth = GridBagConstraints.REMAINDER;
    constraints.anchor = GridBagConstraints.EAST;
    mainPanel.add(xmageLogo, constraints);

    textArea = new JTextArea(5, 40);
    textArea.setEditable(false);
    textArea.setForeground(Color.WHITE);
    textArea.setBackground(Color.BLACK);
    DefaultCaret caret = (DefaultCaret) textArea.getCaret();
    caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
    scrollPane = new JScrollPane(textArea);
    scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
    scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    constraints.gridx = 2;
    constraints.gridy = 1;
    constraints.weightx = 1.0;
    constraints.weighty = 1.0;
    constraints.fill = GridBagConstraints.BOTH;
    mainPanel.add(scrollPane, constraints);

    labelProgress = new JLabel(messages.getString("progress"));
    labelProgress.setFont(font12);
    labelProgress.setForeground(Color.WHITE);
    constraints.gridy = 2;
    constraints.weightx = 0.0;
    constraints.weighty = 0.0;
    constraints.gridwidth = 1;
    constraints.anchor = GridBagConstraints.WEST;
    mainPanel.add(labelProgress, constraints);

    progressBar = new JProgressBar(0, 100);
    constraints.gridx = 3;
    constraints.weightx = 1.0;
    constraints.gridwidth = GridBagConstraints.REMAINDER;
    constraints.fill = GridBagConstraints.HORIZONTAL;
    mainPanel.add(progressBar, constraints);

    JPanel pnlButtons = new JPanel();
    pnlButtons.setLayout(new GridBagLayout());
    pnlButtons.setOpaque(false);
    constraints.gridx = 0;
    constraints.gridy = 3;
    constraints.gridheight = GridBagConstraints.REMAINDER;
    constraints.fill = GridBagConstraints.BOTH;
    mainPanel.add(pnlButtons, constraints);

    btnLaunchClient = new JButton(messages.getString("launchClient"));
    btnLaunchClient.setToolTipText(messages.getString("launchClient.tooltip"));
    btnLaunchClient.setFont(font16);
    btnLaunchClient.setForeground(Color.GRAY);
    btnLaunchClient.setEnabled(false);
    btnLaunchClient.setPreferredSize(new Dimension(180, 60));
    btnLaunchClient.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            handleClient();
        }
    });

    constraints.gridx = GridBagConstraints.RELATIVE;
    constraints.gridy = 0;
    constraints.gridwidth = 1;
    constraints.fill = GridBagConstraints.BOTH;
    pnlButtons.add(btnLaunchClient, constraints);

    btnLaunchClientServer = new JButton(messages.getString("launchClientServer"));
    btnLaunchClientServer.setToolTipText(messages.getString("launchClientServer.tooltip"));
    btnLaunchClientServer.setFont(font12b);
    btnLaunchClientServer.setEnabled(false);
    btnLaunchClientServer.setForeground(Color.GRAY);
    btnLaunchClientServer.setPreferredSize(new Dimension(80, 40));
    btnLaunchClientServer.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            handleServer();
            handleClient();
        }
    });

    constraints.fill = GridBagConstraints.HORIZONTAL;
    pnlButtons.add(btnLaunchClientServer, constraints);

    btnLaunchServer = new JButton(messages.getString("launchServer"));
    btnLaunchServer.setToolTipText(messages.getString("launchServer.tooltip"));
    btnLaunchServer.setFont(font12b);
    btnLaunchServer.setEnabled(false);
    btnLaunchServer.setForeground(Color.GRAY);
    btnLaunchServer.setPreferredSize(new Dimension(80, 40));
    btnLaunchServer.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            handleServer();
        }
    });

    pnlButtons.add(btnLaunchServer, constraints);

    btnUpdate = new JButton(messages.getString("update.xmage"));
    btnUpdate.setToolTipText(messages.getString("update.xmage.tooltip"));
    btnUpdate.setFont(font12b);
    btnUpdate.setForeground(Color.BLACK);
    btnUpdate.setPreferredSize(new Dimension(80, 40));
    btnUpdate.setEnabled(true);

    btnUpdate.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            handleUpdate();
        }
    });

    pnlButtons.add(btnUpdate, constraints);

    btnCheck = new JButton(messages.getString("check.xmage"));
    btnCheck.setToolTipText(messages.getString("check.xmage.tooltip"));
    btnCheck.setFont(font12b);
    btnCheck.setForeground(Color.BLACK);
    btnCheck.setPreferredSize(new Dimension(80, 40));
    btnCheck.setEnabled(true);

    btnCheck.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            handleCheckUpdates();
        }
    });

    pnlButtons.add(btnCheck, constraints);

    frame.add(mainPanel);
    frame.pack();
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    frame.setLocation(dim.width / 2 - frame.getSize().width / 2, dim.height / 2 - frame.getSize().height / 2);
}

From source file:ucar.unidata.idv.control.chart.ScatterPlotChartWrapper.java

/**
 * Handle the event// w  w  w . j a  v  a2 s .  c  om
 *
 * @param event the event
 *
 * @return Should we pass on this event
 */
public boolean chartPanelMouseClicked(MouseEvent event) {
    if (SwingUtilities.isRightMouseButton(event) || (event.getClickCount() < 2)) {
        return super.chartPanelMousePressed(event);
    }
    List series = ((MyScatterPlot) plot).getSeries();
    //TODO: Check if click is inside data area
    double minDistance = 100;
    int minIdx = -1;
    double minTime = -1;
    for (int seriesIdx = 0; seriesIdx < series.size(); seriesIdx++) {
        NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(seriesIdx);
        NumberAxis domainAxis = (NumberAxis) plot.getDomainAxis(seriesIdx);
        double[][] data = (double[][]) series.get(seriesIdx);
        for (int i = 0; i < data[0].length; i++) {
            double x = domainAxis.valueToJava2D(data[0][i], getChartPanel().getScreenDataArea(),
                    plot.getDomainAxisEdge());
            double y = rangeAxis.valueToJava2D(data[1][i], getChartPanel().getScreenDataArea(),
                    plot.getRangeAxisEdge());

            double distance = Math
                    .sqrt((x - event.getX()) * (x - event.getX()) + (y - event.getY()) * (y - event.getY()));
            if (distance < minDistance) {
                minDistance = distance;
                minIdx = i;
            }
        }
        if (minIdx >= 0) {
            minTime = timeValues1[minIdx];
        }
    }
    if (minIdx < 0) {
        return EVENT_PASSON;
    }
    firePropertyChange(PROP_SELECTEDTIME, null, new Double(minTime));
    return EVENT_PASSON;
}

From source file:com.sshtools.sshterm.SshTermSessionPanel.java

/**
 *
 *
 * @param e/*from   ww w  .ja  v a 2  s .c o m*/
 */
public void mouseMoved(MouseEvent e) {
    if (isAutoHideTools()) {
        if (!isToolsVisible() && (e.getY() < 4)) {
            setToolsVisible(true);
        } else if (isToolsVisible() && (e.getY() > 12)) {
            setToolsVisible(false);
        }

        if (!scrollBar.isVisible() && (e.getX() > (getWidth() - 4))) {
            setScrollBarVisible(true);
        } else if (scrollBar.isVisible() && (e.getX() < (getWidth() - 12))) {
            setScrollBarVisible(false);
        }
    }
}

From source file:org.eurocarbdb.application.glycoworkbench.plugin.PeakListChartPanel.java

public void mouseReleased(MouseEvent e) {
    if (MouseUtils.isPopupTrigger(e)) {
        // clear all
        if (zoom_rectangle != null) {
            Graphics2D g2 = (Graphics2D) getGraphics();
            g2.setXORMode(java.awt.Color.gray);
            g2.draw(zoom_rectangle);//from  w  w  w  . jav  a  2 s.  co m
            g2.dispose();
        }
        mouse_start_point = null;
        zoom_rectangle = null;

        // open popup
        current_peak = findPeakAt(e.getPoint());
        enforceSelection(current_peak);
        createPopupMenu(current_peak != null).show(theChartPanel, e.getX(), e.getY());
    } else {
        if (zoom_rectangle != null && mouse_start_point != null) {
            if (Math.abs(e.getX() - mouse_start_point.getX()) > 10) {
                //if( e.getX() < mouse_start_point.getX() ) {
                // unzoom all
                //    onZoomNone();
                //}
                //else {        

                // zoom area           
                double start_x = Math.min(e.getX(), mouse_start_point.getX());
                double end_x = Math.max(e.getX(), mouse_start_point.getX());

                Rectangle2D data_area = theChartPanel.getScreenDataArea((int) start_x,
                        (int) mouse_start_point.getY());
                double new_lower_bound = screenToDataCoordX(start_x);
                double new_upper_bound = screenToDataCoordX(Math.min(end_x, data_area.getMaxX()));
                thePlot.getDomainAxis().setRange(new Range(new_lower_bound, new_upper_bound));
            } else {
                // clear rectangle
                Graphics2D g2 = (Graphics2D) getGraphics();
                g2.setXORMode(java.awt.Color.gray);
                g2.draw(zoom_rectangle);
                g2.dispose();
            }
        }

        // restore zooming
        if (!was_moving && is_moving)
            onActivateZooming();

        zoom_rectangle = null;
        mouse_start_point = null;
    }
}

From source file:game.Clue.ClueGameUI.java

public void mousePressed(MouseEvent e) {
    //player=null;
    Component j = e.getComponent();
    //System.out.println("Click component is : \n"+j.toString()+"\n "+(e.getSource().toString());
    Component c = gameBoard.findComponentAt(e.getX(), e.getY());
    if (c instanceof JPanel) {
        System.out.println("No character chosen");
        return;//  w  ww. j a  v a  2 s. c  o m
    }

    Point parentLocation = c.getParent().getLocation();
    xAdjustment = parentLocation.x - e.getX();
    yAdjustment = parentLocation.y - e.getY();
    player = (JLabel) c;
    previous_room_x = parentLocation.x + xAdjustment;
    previous_room_y = parentLocation.y + yAdjustment;
    player.setLocation(e.getX() + xAdjustment, e.getY() + yAdjustment);
    player.setSize(player.getWidth(), player.getHeight());
    jLayeredPane5.add(player, JLayeredPane.DRAG_LAYER);

}

From source file:org.jax.maanova.madata.gui.ArrayScatterPlotPanel.java

private void mouseMoved(MouseEvent e) {
    if (this.showTooltip) {
        Point2D chartPoint = this.chartPanel.toChartPoint(e.getPoint());

        // find the nearest probe
        XYProbeData xyProbeData = this.getXYData();
        double nearestDistance = Double.POSITIVE_INFINITY;
        int nearestDotIndex = -1;
        double[] xData = xyProbeData.getXData();
        double[] yData = xyProbeData.getYData();
        for (int dotIndex = 0; dotIndex < xData.length; dotIndex++) {
            double currDist = chartPoint.distanceSq(xData[dotIndex], yData[dotIndex]);
            if (currDist < nearestDistance) {
                nearestDistance = currDist;
                nearestDotIndex = dotIndex;
            }/*from  www.java 2s.c o  m*/
        }

        if (nearestDotIndex == -1) {
            this.clearProbePopup();
        } else {
            Point2D probeJava2DCoord = this.getJava2DCoordinates(xData[nearestDotIndex],
                    yData[nearestDotIndex]);
            double java2DDist = probeJava2DCoord.distance(e.getX(), e.getY());

            // is the probe close enough to be worth showing (in pixel distance)
            if (java2DDist <= PlotUtil.SCATTER_PLOT_DOT_SIZE_PIXELS * 2) {
                this.showProbePopup(xyProbeData.getProbeIndices()[nearestDotIndex], xData[nearestDotIndex],
                        yData[nearestDotIndex], e.getX(), e.getY());
            } else {
                this.clearProbePopup();
            }
        }
    }
}

From source file:org.gumtree.vis.hist2d.Hist2DPanel.java

/**
 * Handles a 'mouse dragged' event.//ww w  .  j a  v a 2 s.c o m
 *
 * @param e  the mouse event.
 */
public void mouseDragged(MouseEvent e) {

    setHorizontalTraceLocation(e.getX());
    setVerticalTraceLocation(e.getY());

    Insets insets = getInsets();
    int x = (int) ((e.getX() - insets.left) / getScaleX());
    int y = (int) ((e.getY() - insets.top) / getScaleY());

    EntityCollection entities = null;
    ChartEntity entity = null;
    if (getChartRenderingInfo() != null) {
        entities = getChartRenderingInfo().getEntityCollection();
        if (entities != null) {
            entity = entities.getEntity(x, y);
        }
    }
    if (entity instanceof XYItemEntity) {
        IDataset dataset = (IDataset) ((XYItemEntity) entity).getDataset();
        int item = ((XYItemEntity) entity).getItem();
        setChartX(dataset.getXValue(0, item));
        setChartY(dataset.getYValue(0, item));
        setChartZ(((XYZDataset) dataset).getZValue(0, item));
    }

    //        if (isMaskingEnabled() && (e.getModifiers() & maskingKeyMask) != 0) {
    if (isMaskingEnabled()) {
        int cursorType = findCursorOnSelectedItem(e.getX(), e.getY());
        setCursor(Cursor.getPredefinedCursor(cursorType));
    } else if (getCursor() != defaultCursor) {
        setCursor(defaultCursor);
    }

    // we can only generate events if the panel's chart is not null
    // (see bug report 1556951)
    Object[] listeners = getListeners(ChartMouseListener.class);
    if (getChart() != null) {
        XYZChartMouseEvent event = new XYZChartMouseEvent(getChart(), e, entity);
        event.setXYZ(getChartX(), getChartY(), getChartZ());
        for (int i = listeners.length - 1; i >= 0; i -= 1) {
            ((ChartMouseListener) listeners[i]).chartMouseMoved(event);
        }
    }
    if (getMaskDragIndicator() != Cursor.DEFAULT_CURSOR && getSelectedMask() != null
            && (e.getModifiers() & MouseEvent.BUTTON1_MASK) != 0) {
        changeSelectedMask(e, entities);
    } else if (isMaskingEnabled() && (e.getModifiers() & maskingKeyMask) != 0) {
        makeNewMask(e, entities);
    } else {
        super.mouseDragged(e);
    }
}

From source file:eu.europa.ec.markt.dss.applet.view.validationpolicy.EditView.java

private void valueLeafActionEdit(final MouseEvent mouseEvent, final TreePath selectedPath,
        final XmlDomAdapterNode clickedItem, final JTree tree) {

    final JPopupMenu popup = new JPopupMenu();

    // Basic type : edit
    final JMenuItem menuItem = new JMenuItem(ResourceUtils.getI18n("EDIT"));
    menuItem.addActionListener(new ActionListener() {
        @Override/*from   ww w . ja  v a  2 s . com*/
        public void actionPerformed(ActionEvent actionEvent) {
            final String newValue = JOptionPane.showInputDialog(ResourceUtils.getI18n("EDIT"),
                    clickedItem.node.getNodeValue());
            if (newValue != null) {
                try {
                    if (clickedItem.node instanceof Attr) {
                        ((Attr) clickedItem.node).setValue(newValue);
                    } else {
                        clickedItem.node.setNodeValue(newValue);
                    }
                    //clickedItem.setNewValue(newValue);
                } catch (NumberFormatException e) {
                    showErrorMessage(newValue, tree);
                }
                validationPolicyTreeModel.fireTreeChanged(selectedPath);
            }
        }
    });
    popup.add(menuItem);

    popup.show(tree, mouseEvent.getX(), mouseEvent.getY());

}

From source file:Filter3dTest.java

public synchronized void mouseMoved(MouseEvent e) {
    // this event is from re-centering the mouse - ignore it
    if (isRecentering && centerLocation.x == e.getX() && centerLocation.y == e.getY()) {
        isRecentering = false;//from   w ww.j  a v  a 2 s  . co  m
    } else {
        int dx = e.getX() - mouseLocation.x;
        int dy = e.getY() - mouseLocation.y;
        mouseHelper(MOUSE_MOVE_LEFT, MOUSE_MOVE_RIGHT, dx);
        mouseHelper(MOUSE_MOVE_UP, MOUSE_MOVE_DOWN, dy);

        if (isRelativeMouseMode()) {
            recenterMouse();
        }
    }

    mouseLocation.x = e.getX();
    mouseLocation.y = e.getY();

}