2014-05-12 82 views
-1

當我畫像下面的Java:矩形寬度和高度

drawRect(0,0,39,39); 

一個矩形像素40兩者的寬度和高度?當我在矩形上繪製一個我知道是40x40像素的圖像並將該矩形放在它上面時,它完全適合。所以我有點困惑,因爲java網站說第三和第四個變量指的是寬度和高度,但如果是這種情況,它並不真正指向真正的寬度,只是實際的寬度-1。我覺得我錯過了一些東西或者犯了一個錯誤。 S

+0

請加入更多內容和信息。這是什麼'Rect'功能?它來自哪裏,它有什麼作用? – ApproachingDarknessFish

+0

想着他的意思就是android – james

+0

如果我要做drawRect(0,0,39,39)。這是否與矩形(​​0,0,39,39)相同或是否爲矩形(0,0,40,40)。我認爲這是在讓我感到困惑的兩者之間跳躍。謝謝你們的幫助。對不起,沒有更明確。 – user3630439

回答

3

它將是40x40。

http://developer.android.com/reference/android/graphics/Rect.html#Rect(int,INT,INT,INT)

public Rect (int left, int top, int right, int bottom) 

Added in API level 1 
Create a new rectangle with the specified coordinates. Note: no range checking is performed, so the caller must ensure that left <= right and top <= bottom. 

Parameters 
left The X coordinate of the left side of the rectangle 
top The Y coordinate of the top of the rectangle 
right The X coordinate of the right side of the rectangle 
bottom The Y coordinate of the bottom of the rectangle 

最後2個參數指示右側和底部,所以因此,如果您從0開始,到39這40個像素。

+1

不好意思問一下關於drawRect以及如何與Rectangle類相關的問題。我正在做碰撞,需要繪製一個矩形,但是我也需要創建一個矩形。所以我可以說drawRect(0,0,39,39),併爲我創建了一個40x40的矩形。但是,我想知道當我創建矩形實例。它應該是矩形(0,0,39,39)或矩形(0,0,40,40)以匹配所繪製的矩形。我猜這是現在的第二個。謝謝 – user3630439

+0

這是在這裏記錄的第二個:http://docs.oracle.com/javase/7/docs/api/java/awt/Rectangle.html#Rectangle%28int,%20int,%20int,%20int%29。公平地說,爲什麼Java開發人員會爲一個方法選擇一種格式,而爲Rectangle類的構造函數選擇不同的參數格式,這是令人困惑的。 – ajacian81

+0

非常感謝。我很感激幫助。 – user3630439

1

你的問題是混淆兩個類。

  • Java的java.awt.Rectangle類有一個帶參數的構造函數爲你描述:Rectangle(int x, int y, int width, int height)

  • Android android.graphics.Rect類有一個構造函數,它具有不同的參數:Rect (int left, int top, int right, int bottom)

Rect沒有記錄在Java網站上,因爲它不是Java的一部分。如果您已經在Java網站上找到了文檔,則該文檔適用於Rectangle類......這是不同的。


如果錯誤提供Rect論據,X,Y,寬度,高度,你很可能會引火燒身。爲Rect構造的Javadoc說:

「注意:不進行範圍檢查,所以調用者必須確保左< =右側和頂部< =底部。」

如果你不那麼期待奇怪的事情發生。

(注意Rect(0,0,39,39)違反記錄的約束...)

+0

抱歉意味着詢問drawRect以及它如何與Rectangle類相關。我正在做碰撞,需要繪製一個矩形,但是我也需要創建一個矩形。所以我可以說drawRect(0,0,39,39),併爲我創建了一個40x40的矩形。但是,我想知道當我創建矩形實例。它應該是矩形(0,0,39,39)或矩形(0,0,40,40)以匹配所繪製的矩形。我猜這是現在的第二個。謝謝 – user3630439

+1

停止猜測。這是一種糟糕的編程技術。閱讀各自的javadoc,並假設他們所說的是正確的。這將回答你的問題。 –

+0

感謝您的回覆。 – user3630439