user3046287
user3046287

Reputation:

Pencil drawing on mouseDragged event on a JPanel?

How to make pencil-drawing on a JPanel via mouseDragged ?

I have a JPanel where I want to draw using the mouse, so whenever I drag the mouse on that JPanel, for each (x,y) an oval shape (with specified color and dimension) is painted.

Upvotes: 1

Views: 228

Answers (1)

camickr
camickr

Reputation: 324108

There are two common ways to do custom painting:

  1. Create a List of Objects you want to paint and then iterate through the List every time the component is repainted.

  2. Paint the objects directly to a BufferedImage and then when the component is repainted you paint the BufferedImage.

Check out Custom Painting Approaches for working examples of both approaches. The example actually draws colored Rectangles which is almost exactly what you want.

Upvotes: 1

Related Questions