怎么样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 程序中显示这种几何图形的简单示例:
![距离示例](https://i.stack.imgur.com/qwLau.png)
这会在鼠标光标所在的位置绘制一条线并显示线的长度(即从光标中心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);
}
}