簡體   English   中英

使用 fillRect() 的正確方法是什么?

[英]What is the correct way to use filledRect()?

我真的是 Java 的初學者。 今天我寫了一些代碼,它應該畫一些矩形,但它們沒有。 我的代碼有問題嗎?

導入 java.awt.*;

導入 java.awt.event.*;

公共 class NewMain 擴展框架{

 static int oldx = 0; static int newx = 0; static int oldy = 0; static int newy = 0; static Color drawColor = Color.white; public static void main(String[] args) { Frame myFrame = new Frame(); Graphics myGraphics = myFrame.getGraphics(); myFrame.setSize(1360,760); myFrame.setTitle("Fun Blackboard"); myFrame.setVisible(true); myFrame.setBackground(Color.black); myFrame.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); myGraphics.setColor(Color.white); myGraphics.fillRect(0,0,20,20); myGraphics.setColor(Color.lightGray); myGraphics.fillRect(0,0,20,40); myGraphics.setColor(Color.gray); myGraphics.fillRect(0,0,20,60); myFrame.addMouseMotionListener(new MouseMotionAdapter(){ public void mouseDragged(MouseEvent e){ System.out.print("X:"); System.out.print(e.getX()); System.out.print(", Y:"); System.out.println(e.getY()); } }); }

}

我還嘗試將它們放入 mousePressed() 事物中,但仍然無法正常工作。

您可以嘗試使用 Graphics class 的 drawRect,有一個簡單的方法:

paint(Graphics g) {
Rectangle r = new Rectangle(5,5,30,30);
g.drawRect(r.x,r.y,r.width,r.height);
}

反正awt已經過時了,你可以試試用JavaFX創建東西,使用起來更方便快捷,創建圖形界面也更容易

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM