2010-03-01 82 views
1

我正在做這些iTunes Stanford課程,並且我一直在學習Java的開始。事情進展順利,但他們最近引入了事件 - 特別是MouseEvents。我一直在閱讀本書中的章節,並通過示例代碼,而有些東西只是沒有爲我點擊右鍵......總是會有異步的東西給我帶來麻煩:-D難以理解Java MouseEvent

此前,有些人提到我提到「addMouseListener」是Graphics導入中的類是很重要的。據我所知,這只是在畫布上添加一個全屏鼠標監聽器。

我對這件事還是很新的,所以我可能不會像我應該那樣描述事物。

這是我爲了更好地理解它而試圖簡化的一段代碼。目前,它會建立一個紅色的矩形,我可以點擊它並沿x軸拖動它。大!!!

import java.awt.*; 
import java.awt.event.*; 
import acm.graphics.*; 
import acm.program.*; 

/** This class displays a mouse-draggable rectangle and oval */ 

public class DragObject extends GraphicsProgram { 

    /* Build a rectangle */ 
    public void run() { 

     GRect rect = new GRect(100, 100, 150, 100); 
     rect.setFilled(true); 
     rect.setColor(Color.RED); 
     add(rect); 
     addMouseListeners(); 
    } 

/** Called on mouse press to record the coordinates of the click */ 
    public void mousePressed(MouseEvent e) { 
     lastX = e.getX(); 
     lastY = e.getY(); 
     gobj = getElementAt(lastX, lastY); 
    } 

/** Called on mouse drag to reposition the object */ 
    public void mouseDragged(MouseEvent e) { 

      if((lastX) > 100){ 
      gobj.move(e.getX() - lastX, 0); 

      lastX = e.getX(); 
      lastY = e.getY(); 
     } 
    } 

/** Called on mouse click to move this object to the front */ 
    public void mouseClicked(MouseEvent e) { 
     if (gobj != null) gobj.sendToFront(); 
    } 

/* Instance variables */ 
private GObject gobj; /* The object being dragged */ 
private double lastX; /* The last mouse X position */ 
private double lastY; /* The last mouse Y position */ 
} 

如果我拖動鼠標線從畫布,我想矩形留在畫面內,而不是動過它(相同的行爲,如果你超越與滾動區域水平滾動條會做鼠標按鈕仍然點擊)。我怎樣才能做到這一點?

我一直在試圖沿着這些線路的東西,但它不工作的權利:

if ((lastX > (getWidth() - PADDLE_WIDTH)) || (lastX < PADDLE_WIDTH)) { 
     gobj.move(0, 0); 
    } else { 
     gobj.move(e.getX() - lastX, 0); 
    } 

回答

0

爲了做到這一點,你需要知道Canvas對象的寬度,我敢肯定會有是一種可以提供這個價值的方法。然後,您可以根據畫布寬度檢查MouseEvent的當前x位置,並且一旦超過畫布寬度,就不會增加形狀對象的x座標。根據您希望保留在畫布中的形狀的多少,您可能還需要考慮形狀對象的寬度。

有一件事情可以幫助我處理w /動畫和移動gui中的對象時,在紙上繪製了一些場景,並注意座標如何變化。

+0

感謝您的回答。我有畫布寬度。困難(對我而言)是動畫只是將對象與其過去的座標相關聯 - 而不是與畫布相關。 gobj.move(e.getX() - lastX,0);所以我一直在嘗試像if(lastX <0){lastX = 0}這樣的事情,但這只是在做與鼠標移動有關的時髦事情 - 而不是畫布:( – Joel 2010-03-01 04:54:49

2

您的代碼正在相對於鼠標的最後位置移動矩形。當你簡單地移動東西時,這可以很好地工作,但是當你想要在邊界停止時需要使用絕對定位。

// When the mouse is pressed, calculate the offset between the mouse and the rectangle 
public void mousePressed(MouseEvent e) { 
    lastX = e.getX(); 
    lastY = e.getY(); 
    gobj = getElementAt(lastX, lastY); 
} 

public void mouseDragged(MouseEvent e) { 
     double newX; 

     // Assuming you can get the absolute X position of the object. 
     newX = gobj.getX() + e.getX() - lastX; 
     // Limit the range to fall within your canvas. Adjust for your paddle width as necessary. 
     newX = Math.max(0, Math.min(newX, getWidth())); 
     // Set the new position of the paddle, assuming you can set the absolute position. 
     gobj.setX(newX); 

     lastX = e.getX(); 
     lastY = e.getY(); 
    } 
} 

,這可能不是很你想要的,因爲一旦你熄滅的邊緣,對象將停止移動,但隨後當你移回到畫布,你的槳將立即移動而不是等待鼠標達到與其開始的槳相同的相對位置。

你可以嘗試讓它做你想做的事。