[問題] 如何繪出圖形

看板java作者 (桃樂思)時間13年前 (2011/07/29 00:19), 編輯推噓1(106)
留言7則, 2人參與, 最新討論串1/1
想請問大家如何帶入座標點繪成圖形? 以下是我的程式碼 import java.awt.Color; import java.awt.Graphics; import javax.swing.JFrame; import javax.swing.JPanel; //這是執行可以畫出圖形的 public class dr extends JPanel { private int width; private int height; private int x; private int y; private Color color; public dr(int w,int h,int ulx,int uly,Color c){ this.width = w; this.height= h; this.x = ulx; this.y = uly; this.color = c; } public void paint(Graphics g) { g.setColor(color); g.fillRect(width,height,x,y); /*但是這裡就會出現錯誤 cannot find symbol constructor dr(int,int,double,double,java.awt.Color) 好像要 int int int int 不能 int int double double */ } public static void main(String[] args) { JFrame frame = new JFrame(); frame.getContentPane().add(new dr(5,5,0.911328 ,0.911328,Color.RED)); /*我在這裡輸入的座標0.911328 ,1.066057 會變成要改成 double才可以 但是上面會錯誤 */ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400,400); frame.setVisible(true); } } 我不能畫0.911328 的座標點 如果打 1 2 4 5 就可以 為什麼不能執行呢? 是因為我宣告的錯誤嗎? 如果可以執行要怎麼才能帶入大量的0.911328這種座標點? 請大家多多指教 謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 218.164.125.77 ※ 編輯: theory0724 來自: 218.164.125.77 (07/29 00:20)

07/29 03:11, , 1F
maybe "d" suffix ex: 0.911328d?
07/29 03:11, 1F

07/29 03:29, , 2F
cast to int premitive type: (int)0.911328
07/29 03:29, 2F

08/03 19:38, , 3F
JPanel最好override paintComponent()
08/03 19:38, 3F

08/03 19:51, , 4F
因為Graphics內的fillRect()函式參數為
08/03 19:51, 4F

08/03 19:55, , 5F
fillRect(int x,int y,int width,int height)
08/03 19:55, 5F

08/03 19:58, , 6F
所以不能用double,是否可把座標乘10或100倍再四捨五入至
08/03 19:58, 6F

08/03 20:00, , 7F
整數,因為是分群,相對位置不變
08/03 20:00, 7F
文章代碼(AID): #1ECOmSOz (java)