简体   繁体   English

如何更精确地绘制此形状?

[英]How to draw this shape with better accuracy?

I am using Graphics2D to draw shapes eg rectangles. 我正在使用Graphics2D绘制形状,例如矩形。 However, I have got the code working and it draws rectangles on my GUI, but the accuracy is way off and not the size I drag it to be (the get methods return ints by default). 但是,我已经使代码正常工作,并且在我的GUI上绘制了矩形,但是精确度远不及我拖动它的大小(get方法默认情况下返回int)。 Also it seems like the shape size and the number of shapes is random every time I click on the panel. 同样,每次单击面板时,形状大小和形状数量似乎都是随机的。

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();
}

Rectangle2D is constructed with (x,y,width,height). Rectangle2D由(x,y,width,height)构造。 You're giving it (x1,y1,x2,y2). 您正在给它(x1,y1,x2,y2)。 Try: 尝试:

Rectangle2D rectangle = new Rectangle2D.Double(a, b, a2-a, b2-b);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM