Mushahid Hussain
Mushahid Hussain

Reputation: 4045

Shape drawing controlled by mouse

How can we draw the shapes in Java like we do in paint?

For example if I want to draw the rectangle this command will draw it:

g2.fill3DRect(mt, mf, 45, 45, true); 

But how can I increase the or decrease the size of an object or shape during run-time using mouse like we did in paint?

Upvotes: 0

Views: 292

Answers (2)

ghostbust555
ghostbust555

Reputation: 2049

Use a mouse listener to get the position of the mouse after it has been pressed. i.e.

g2.fill3DRect(mt, mf, mouse.getX(), mouse.getY(), true); 

But clear the screen by drawing a rectangle over the entire screen before each draw so that there's not a million rectangles at the same time. This is the most basic example of course. Look into double buffering and practice.

Upvotes: 1

kutschkem
kutschkem

Reputation: 8163

Implement a shape object that holds the attributes of the shape.

Your Panel should maintain a List of shapes that it draws when it needs to. Also one shape can be attached to the mouse.

Then register a mouselistener to your panel that will:

  • update your shape and redraw the panel if you move the mouse.
  • creates a new shape when you mouseDown
  • releases the shape when you mouseUp

Upvotes: 0

Related Questions