作者theory0724 (桃乐思)
看板java
标题[问题] 如何绘出图形
时间Fri Jul 29 00:19:37 2011
想请问大家如何带入座标点绘成图形?
以下是我的程式码
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)
1F:→ xlk:maybe "d" suffix ex: 0.911328d? 07/29 03:11
2F:→ xlk:cast to int premitive type: (int)0.911328 07/29 03:29
3F:→ cashWANG:JPanel最好override paintComponent() 08/03 19:38
4F:推 cashWANG:因为Graphics内的fillRect()函式参数为 08/03 19:51
5F:→ cashWANG:fillRect(int x,int y,int width,int height) 08/03 19:55
6F:→ cashWANG:所以不能用double,是否可把座标乘10或100倍再四舍五入至 08/03 19:58
7F:→ cashWANG:整数,因为是分群,相对位置不变 08/03 20:00