I am using Graphics2D
to draw shapes e.g. rectangles. 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). 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). You're giving it (x1,y1,x2,y2). Try:
Rectangle2D rectangle = new Rectangle2D.Double(a, b, a2-a, b2-b);