0

是否有一种方法可以为类似于 pt.distance() 方法的 Jbutton 获取 (x,y) 格式的坐标。jbutton 使用 setLayout(null) 和 setBounds(x,y,0,0)。我将如何比较 pt.distance() 和 Jbutton(x,y) 的结果?

最后,(x,y) 是如何计算的?

 Point pt = evt.getPoint();
    double u = pt.distance((double)x,(double)y);
    double k = st1.getAlignmentX();
    double p = st1.getAlignmentY();
    if(u > ){ // u has to be measured to (x,y) value of jbutton
    tim.setDelay(500);
    }
    if(u < ){
    tim.setDelay(100);
    }
4

3 回答 3

2

如果 by pt.distance(),您指的是该Point2D.distance()方法,那么您可以这样进行:

Point location = button.getLocation(); // where button is your JButton object
double distance = pt.distance(location); // where pt is your Point2D object

或者:

double distance = pt.distance(button.getX(), button.getY());

然后Point将包含您的按钮的 x 和 y 坐标。如果您不使用布局,这些值将是您设置的值。但是,如果您使用的是布局,则LayoutManager父级负责计算值。

回应您的编辑: 我不明白您要做什么。调用不会让你设置按钮的坐标,只有它的孩子setLayout(null)JButton我认为这就是您要实现的目标:

Point pt = evt.getPoint();
double distance = pt.distance(button);
int someLength = 100; // the distance away from the button the point has to be to decide the length of the delay    

if (distance < someLength) {
    tim.setDelay(500);
} else {
    tim.setDelay(100);
}
于 2013-10-27T05:10:48.400 回答
2

怎么样getLocation,返回组件在其父组件上的getLocationOnScreen坐标,或者,返回组件在显示器上的坐标?


关于如何计算 x 和 y 的第二个问题,我不确定“计算”是什么意思。坐标将相对于某物。通常是父组件(例如 aJPanel所在JButton的位置)或屏幕上的某个位置(例如getLocation返回 a JFrame)。

像这样的方法Point.distance将减去两个坐标的 x 和 y 值并告诉您差异。这只是基本几何。

例如,下面的方法将返回一个点到 a 中心的距离JButton

public static double getDistance(Point point, JComponent comp) {

    Point loc = comp.getLocation();

    loc.x += comp.getWidth() / 2;
    loc.y += comp.getHeight() / 2;

    double xdif = Math.abs(loc.x - point.x);
    double ydif = Math.abs(loc.y - point.y);

    return Math.sqrt((xdif * xdif) + (ydif * ydif));
}

这将返回三角形的斜边作为像素测量值,这意味着如果您给它的点(如光标坐标)在对角线上,它将为您提供有用的距离。

Point.distance会做这样的事情。


我注意到我的这个旧答案已经获得了很多观点,所以这里有一个更好的方法来完成上述操作(但并没有真正显示数学):

public static double distance(Point p, JComponent comp) {
    Point2D.Float center =
        // note: use (0, 0) instead of (getX(), getY())
        // if the Point 'p' is in the coordinates of 'comp'
        // instead of the parent of 'comp'
        new Point2D.Float(comp.getX(), comp.getY());

    center.x += comp.getWidth() / 2f;
    center.y += comp.getHeight() / 2f;

    return center.distance(p);
}

这是一个在 Swing 程序中显示这种几何图形的简单示例:

距离示例

这会在鼠标光标所在的位置绘制一条线并显示线的长度(即从光标中心JPanel到光标的距离)。

import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;

class DistanceExample implements Runnable {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new DistanceExample());
    }

    @Override
    public void run() {
        JLabel distanceLabel = new JLabel("--");
        MousePanel clickPanel = new MousePanel();

        Listener listener =
            new Listener(distanceLabel, clickPanel);
        clickPanel.addMouseListener(listener);
        clickPanel.addMouseMotionListener(listener);

        JPanel content = new JPanel(new BorderLayout());
        content.setBackground(Color.white);
        content.add(distanceLabel, BorderLayout.NORTH);
        content.add(clickPanel, BorderLayout.CENTER);

        JFrame frame = new JFrame();
        frame.setContentPane(content);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    static class MousePanel extends JPanel {
        Point2D.Float mousePos;

        MousePanel() {
            setOpaque(false);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            if (mousePos != null) {
                g.setColor(Color.red);
                Point2D.Float center = centerOf(this);
                g.drawLine(Math.round(center.x),
                           Math.round(center.y),
                           Math.round(mousePos.x),
                           Math.round(mousePos.y));
            }
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(100, 100);
        }
    }

    static class Listener extends MouseAdapter {
        JLabel distanceLabel;
        MousePanel mousePanel;

        Listener(JLabel distanceLabel, MousePanel mousePanel) {
            this.distanceLabel = distanceLabel;
            this.mousePanel = mousePanel;
        }

        @Override
        public void mouseMoved(MouseEvent e) {
            Point2D.Float mousePos =
                new Point2D.Float(e.getX(), e.getY());

            mousePanel.mousePos = mousePos;
            mousePanel.repaint();

            double dist = distance(mousePos, mousePanel);

            distanceLabel.setText(String.format("%.2f", dist));
        }

        @Override
        public void mouseExited(MouseEvent e) {
            mousePanel.mousePos = null;
            mousePanel.repaint();

            distanceLabel.setText("--");
        }
    }

    static Point2D.Float centerOf(JComponent comp) {
        Point2D.Float center =
            new Point2D.Float((comp.getWidth() / 2f),
                              (comp.getHeight() / 2f));
        return center;
    }

    static double distance(Point2D p, JComponent comp) {
        return centerOf(comp).distance(p);
    }
}
于 2013-10-27T05:15:01.987 回答
1

像这样的东西?:

JButton b = new JButton();
b.getAlignmentX();
b.getAlignmentY();

你也可以使用这个:

Rectangle r = b.getBounds(); // The bounds specify this component's width, height, 
                             // and location relative to its parent.
于 2013-10-27T05:07:15.333 回答