Howabout getLocation
,返回其父組件上的組件的座標,或getLocationOnScreen
,返回顯示屏上的組件的座標?
你的第二個問題是關於x和y是如何計算的,我不確定你的意思是'計算'。座標將相對於某些東西。通常,父組件(如JButton
所在的JPanel
)或屏幕上的位置(例如getLocation
將返回JFrame
)。
像Point.distance
這樣的方法將減去兩個座標的x和y值,並告訴你的差異。這只是基本的幾何。
例如,這裏是將返回來自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);
}
下面是示出了在搖擺程序這種幾何結構的簡單的例子:
![distance example](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);
}
}
我試過getLocation方法,並不斷獲取與它不相關的錯誤,而不是指針。至於如何計算x,y,我想如果有一個標準的方法來定義(x,y),我可以計算jbutton的x和y值並將它們與指針進行比較。現在我只使用一個jpanel,所以一切都應該是等價的。 – user1566796
指針是什麼意思?你的意思是你得到一個NullPointerException或者你指的是Point?定義座標的標準方法取決於你如何得到要點。如果您在JButton上調用getLocation,它將成爲您添加按鈕的位置。它看起來像你從一個MouseEvent拉一個點。如果是這種情況,則MouseEvent座標將相對於觸發事件的任何事件。如果你正在聽一個面板,那麼它就是面板上的座標。如果你正在聽按鈕,那麼它就是按鈕上的座標。 – Radiodef
或者你是指什麼是相對於座標? 0,0始終是Swing中的左上角。在x = 5,y = 10的JPanel上的點座標是從面板左側開始5個像素,從頂部開始向下10個像素。下面是一個小程序的答案,當你點擊一個面板時,如果你想親自看看,它將繪製一個座標:http://stackoverflow.com/a/10811315/2891664 – Radiodef