Unconnected ovals while drawing too fast

111 views Asked by At

I'm writing Applet which have to draw smooth connected lines, I'm doing it with this line (user is entering the pen's width)

    public void mouseDragged(MouseEvent arg0)
    {   
    g.setColor(kolor);
    int width=Integer.parseInt(szPedzel.getText());
    g.fillOval(arg0.getX(), arg0.getY(), width, width); 
    }

where g = this.getGraphics();

When I'm drawing kinda slow it is good, but when I'm doing it faster single ovals are displayed, is there any method which allows to cennect this ovals?

img: http://i42.tinypic.com/1536x04.png

4

There are 4 answers

0
Tim B On

You will need to remember the previous mouse position and then draw the oval at every point between the old and new position....

...or see if the draw line functions can do what you need and do the same thing there.

1
MadProgrammer On

Don not use getGraphics this is not how custom painting is done.

This is unreliable, as it can return null, is only a snap-shot of what was painted during the last paint cycle and the contents will be discarded on the next paint cycle.

Custom painting is best achieved by overriding the paintComponent method of a component that extends from JComponent

Take a look at Performing Custom Painting for more details and Painting in AWT and Swing, because everybody who wants to do custom painting in Swing/AWT should know how it works

For example

0
Radnyx On

Your mouse doesn't move pixel-by-pixel at a time. To go faster it will skip a few. If you want to draw ovals in between then I suggest you use a "line drawing" method to draw ovals in the empty space.

You can't really get any smoothness with pixels being skipped over. An algorithm to do that would be fairly complex.
The best you can do is to draw a line of ovals in between, remember the last point "mousegrab" was used at.

0
Hovercraft Full Of Eels On

No, you don't want to draw like this:

public void mouseDragged(MouseEvent arg0)
{   
  g.setColor(kolor);
  int width=Integer.parseInt(szPedzel.getText());
  g.fillOval(arg0.getX(), arg0.getY(), width, width); 
}

where g = this.getGraphics();

If you use getGraphics() on a component, then the Graphics object thus obtained will be short-lived and all your resultant images at risk for not being drawn properly. Don't believe me? Then minimize and then restore your GUI while it's running and watch your ovals disappear.

Instead, I suggest:

  • Give your drawing component an ArrayList<Point>
  • In your GUI's paintComponent(Graphics g) method (if this is a Swing GUI), iterate through the List drawing lines between each point.
  • In your MouseMotionListener, add Points to the List and call repaint().
  • If you want a thicker line, then cast your Graphics object to a Graphics2D object and call setStroke(Stroke s) on it, passing in a BasicStroke object with a thickness of > 1.

  • For example, please check out this link.