2012-11-22 76 views
0

我只使用圖片,而我用來查看我的應用程序的窗口的尺寸在不同的系統上可能會有所不同。我有一個鼠標動作監聽器,它正在監聽我程序主視圖上的點擊。我有一個看起來像一個按鈕的圓角矩形。我想這樣做,鼠標動作偵聽器只能偵聽圓角矩形的區域,而不是所有系統上的整個圖像。就像標題所說的那樣,並不是整個圖像都有內容,尤其是角落看起來不像是圖像的一部分,所以我不想讓用戶無需點擊圖像的某些部分即可內容並獲得與單擊包含內容的部分相同的結果。 我的形象看起來類似於此image http://www.youthedesigner.com/wp-content/uploads/2011/02/gimp-tutorial-on-creating-buttons-421.png 所以我只想程序,如果用戶點擊圖像,而非按鈕周圍漂亮裏面的東西按鈕做一些事情。 這就是我現在所擁有的收聽點擊:如何讓點擊區域只是圖像非矩形部分的一部分?

@Override 
public void mouseClicked(MouseEvent e) { 
    for(int i = 0; i <= 200; i++) { 
     if(e.getY() >= 100+i && e.getY() <= 300) { 
      if(e.getX() >= 10+100-Math.pow(10000-(Math.pow((i-100),2.0)),.5)) && e.getX() <= 10+400-Math.pow(10000-(Math.pow((i-100),2.0)),.5))) { 
       // do stuff 
       i = 201; 
      } 
     } 
    } 
} 

我用我的代碼數學公式看起來像110-(10000-(Y-100)^ 2)^(1/2) ),如果作圖的話,它看起來像一個左括號,並且看起來像離開第一個圖400個單位的右括號,並且是410+(10000-(y-100)^ 2)^(1/2)) 。

的代碼工作正常,我的系統上,但在其他系統上,它並沒有在所有的工作,我很好奇我怎麼會動,我聽到對應的圖像是如何縮放的位置。

非常感謝您的幫助,您可以提供。

+1

爲了更好地提供幫助,請發佈[SSCCE](http://sscce.org/)。 –

回答

0

所以,我用喬普的答案來解決我的問題。他的回答並不是我想要的,但它給了我需要解決我的問題的想法。我來的解決方案是:

private boolean insideButton(Rectangle buttonRect, Point mousePt) { 
    if (buttonRect.contains(mousePt)) { 
     int r = (int)buttonRect.getHeight()/2; // radius of either of the circles that make up the sides of the rectangle 
     if(mousePt.x <= buttonRect.getWidth()/2) { // if it is on the left of the button 
      Point center = new Point((int)buttonRect.getX()+r, (int)buttonRect.getY()+r); // the center of the circle on the left 
      double lengthToPoint = Math.pow(Math.pow(mousePt.x-center.x, 2)+Math.pow(mousePt.y-center.y, 2), 1.0/2); // length from center to the point that the user clicked at 
      if(lengthToPoint > r && mousePt.x < center.x) { // if it is to the left of the center and out of the circle 
       return false; 
      } else { 
       return true; 
      } 
     } else { // if it is on the right, the rest of the code is just about the same as the left circle 
      Point center = new Point((int)buttonRect.getWidth()-r, (int)buttonRect.getY()+r); 
      double lengthToPoint = Math.pow(Math.pow(mousePt.x-center.x, 2)+Math.pow(mousePt.y-center.y, 2), 1.0/2); 
      if(lengthToPoint > r && mousePt.x > center.x) { 
       return false; 
      } else { 
       return true; 
      } 
     } 
    } else { 
     return false; 
    } 
} 

我知道這是去有點過分與計算和低效率的,但我想目前這種方式來顯示我的解決方案是如何工作的一個更好的主意。

+0

它不是一個左右兩個圓圈的矩形嗎?我的版本使用像你的版本包含rect,但是我檢查'(|'和'|)':鼠標x 寬度 - r(右)。很高興你成功了,並感謝你的反饋。 –

+0

也許吧,但是當我使用你的代碼時,它不起作用,而且我很難弄清楚如何調整它以適應我的需要,但它給了我這個如何去做的想法。 – Pillager225

0

我能想到的至少有兩種方法。

首先是產生一個掩模圖像(黑色和白色),其中(例如)白將指示可點擊區域。基本上,您可以比較基於原始圖像的點擊拾取點的蒙版的像素顏色。

另一種方法是建立一個影像地圖,基本上都採用類似Shape API的東西,以允許非矩形的形狀。這將允許使用Shape#contains,以確定是否鼠標單擊它裏面還是不

在這兩種情況下,你需要考慮到的原始圖像

1

的for循環是多餘的X/Y位置。

你可以保證按鈕(巴紐)外的像素有一定的透明度,然後檢查阿爾法顏色分量。

在這種情況下,你可以添加一個矩形,並尋找:

private boolean insideButton(Rectangle buttonRect, Point mousePt) { 
    if (buttonRect.contains(mousePt)) { 
     int r = buttonRect.height()/2; 
     if (mousePt.x < r) { 
      // Left circle with O at (r, r) 
      int xFromO = r - mousePt.x; 
      int yFromO = r - mousePt.y; 
      if (xFromO * xFromO + yFromO * yFromO > r * r) { 
       return false; // Outside circle 
      } 
     } 
     if (mousePt.x > buttonRect.right - r) { 
      // Right circle: 
      ... 
     } 
     return true; 
    } 
    return false; 
} 
+0

當你說「0 at(r,r)」時,你的意思是它的中心位於點(r,r)? – Pillager225

+0

是的,在我的學校數學O代表'起源','半徑'代表。' –