pmcg521
pmcg521

Reputation: 39

Swing: Update Graphics every millisecond?

I created a little sketchpad program with basic GUI. I used the paintComponent method. I want to update the graphic being drawn every millisecond. That way, the user can see what they're about to draw before they release the mouse click. For example, if I'm drawing a rectangle, I want to see the rectangle while I'm drawing it. If you're confused as to what exactly I'm talking about, open MS Paint and click on the rectangle tool. Draw a rectangle. Notice how it updates continuously, as opposed to after you release the mouse. I figured that there must be some way I can get it to update my graphics every millisecond. What is the best way to go about doing this? Sorry if this is an easy question, I'm new to Swing! :)

Upvotes: 0

Views: 134

Answers (1)

camickr
camickr

Reputation: 324197

Well you don't update every millisecond.

You use a MouseMotionListener and you update every time a mouseDragged event is generated to redraw the Rectangle.

Check out Custom Painting Approaches for two approaches on how to do this.

The examples show how to draw multiple rectangles by either:

  1. Adding Objects to a List and then repainting each object every time the component is repainted
  2. Painting directly onto a BufferedImage and the just painting the BufferedImage.

Upvotes: 2

Related Questions