我正在使用Graphics2D
绘制形状,例如矩形。但是,我已经让代码工作了,它在我的GUI上绘制了矩形,但是准确性是偏离的,而不是我拖动它的大小(get方法默认返回int)。此外,每次点击面板时,形状大小和形状数量似乎都是随机的。
int a,b,a2,b2;
public void MyPaintMethod(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
Rectangle2D rectangle = new Rectangle2D.Double(a,b,a2,b2);
g2D.draw(rectangle);
repaint();
}
public void mousePressed(MouseEvent e) {
// ML
a = e.getX();
b = e.getY();
}
public void mouseReleased(MouseEvent e) {
// ML
a2 = e.getX();
b2 = e.getY();
}
答案 0 :(得分:4)
Rectangle2D由(x,y,width,height)构成。你给它(x1,y1,x2,y2)。尝试:
Rectangle2D rectangle = new Rectangle2D.Double(a, b, a2-a, b2-b);