2013-10-27 200 views
0

有沒有類似於pt.distance()方法獲取(x,y)格式的Jbutton座標的方法。 jbutton利用setLayout(null)和setBounds(x,y,0,0)。 如何比較pt.distance()和Jbutton(x,y)的結果?如何獲取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); 
    } 

回答

2

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

這繪製一條線到哪裏鼠標光標並顯示線的長度(這是從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); 
    } 
} 
+0

我試過getLocation方法,並不斷獲取與它不相關的錯誤,而不是指針。至於如何計算x,y,我想如果有一個標準的方法來定義(x,y),我可以計算jbutton的x和y值並將它們與指針進行比較。現在我只使用一個jpanel,所以一切都應該是等價的。 – user1566796

+0

指針是什麼意思?你的意思是你得到一個NullPointerException或者你指的是Point?定義座標的標準方法取決於你如何得到要點。如果您在JButton上調用getLocation,它將成爲您添加按鈕的位置。它看起來像你從一個MouseEvent拉一個點。如果是這種情況,則MouseEvent座標將相對於觸發事件的任何事件。如果你正在聽一個面板,那麼它就是面板上的座標。如果你正在聽按鈕,那麼它就是按鈕上的座標。 – Radiodef

+0

或者你是指什麼是相對於座標? 0,0始終是Swing中的左上角。在x = 5,y = 10的JPanel上的點座標是從面板左側開始5個像素,從頂部開始向下10個像素。下面是一個小程序的答案,當你點擊一個面板時,如果你想親自看看,它將繪製一個座標:http://stackoverflow.com/a/10811315/2891664 – Radiodef

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. 
1

如果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負責計算值。

迴應你的編輯: 我不明白你在做什麼。在JButton上調用setLayout(null)不會讓您設置按鈕的座標,只有它是兒童。我覺得這是你想達到什麼目的:

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); 
} 
+0

我想讓我的指針導致按鈕移動得更快,因爲指針越來越近。所以我需要指針值保留,但我也需要按鈕的值,這樣我可以比較兩個值並確定它們是否在一定範圍內,那麼延遲將加速或減慢。 – user1566796