I have been looking at the Java2D tutorials and was wondering how to draw a shape using the mouse to define its size(i.e the size of the shape is not fixed). I haven’t come across a tutorial specifically for this and was wondering how I could implement this for a rectangle for example.
Share
Basically, the size is FIXED at every moment. When you add a
MouseMotionListener, before the next event is captured, you can paint the shape on the screen with the size depending on currentMouseEvent.getPoint()which tells you the coordinates of your mouse location.Override the
paintComponent(Graphics g)method of the component. and callrepaint()method after each update of the mouse location and the size of the shape: