我想在一個矩形內建立一個矩形。矩形「牆」應位於高度爲bg.getHeight() - ground.getHeight()的特定位置。出於某種原因,矩形「牆」仍然延伸到小程序的底部。所有其他值,如寬度和位置工作正常。對於一個300高度的applet,如果我減去一個靜態120,它可以工作,但ground.getHeight的值實際上是60,如果我減去60,它仍然一直延伸到邊界。我對這種不一致感到困惑。java Rectangle getHeight()奇怪的行爲
import javax.swing.JApplet;
import java.awt.*;
public class House_JM extends JApplet {
public void paint(Graphics page) {
Rectangle ground = new Rectangle(0, (int) (getSize().height * .8), getSize().width,
(int) (getSize().height * .2));
Rectangle bg = new Rectangle(0, 0, getSize().width, (int) (getSize().height * .8));
page.setColor(Color.green);
page.fillRect((int) ground.getX(), (int) ground.getY(), (int) ground.getWidth(), (int) ground.getHeight());
page.setColor(Color.blue);
page.fillRect((int) bg.getX(), (int) bg.getY(), (int) bg.getWidth(), (int) bg.getHeight());
Rectangle walls = new Rectangle((int) (bg.getWidth()/2 - bg.getWidth() * .1), (int) (bg.getHeight() * .5),
(int) (bg.getWidth() * .4), (int) ((bg.getHeight() - ground.getHeight())));
page.setColor(Color.red);
page.fillRect((int) walls.getX(), (int) walls.getY(), (int) walls.getWidth(), (int) walls.getHeight());
System.out.println(bg.getHeight());
}
}
請創建併發佈一個有效的[mcve],一個***小***代碼,我們可以複製,粘貼,編譯和運行,並演示您的問題。不要求整個程序,只是我們可以完全測試。 –
另外,你是否直接在applet中繪圖?不要這樣做,而是在小應用程序顯示的JPanel內繪製。另外,爲什麼你甚至使用小程序,因爲這項技術已被Oracle棄用了? –
完整的代碼編輯後。我正在使用一個小程序,因爲它是一個學校作業。 – user1660886