1

i am trying to build a PaintBrush with Swings. I wanted to do free hand drawing on one of the JPanels, so i overridded it's PaintComponent, and drew some thing, it is fine. But i don't get any background, i needed white background, so i called the super.paintComponent(g) inside paintComponent(), now i am able to get the background but everytime my repaint() is called on mouseDragged event, i lose my previous drawing on the JPanel. Is there any way by which i can get a background color, and also my previous drawing is not lost?

Any help would be appreciated.Thanks

JPanel paintComponent()

public void paintComponent(Graphics g) {
        // super.paintComponent(g);
      g.setColor(Color.red);
      g.fillOval(xpos, ypos, 5, 5);

    }

mouseDragged()

public void mouseDragged(MouseEvent arg0) {
        xpos= arg0.getX();
         ypos= arg0.getY();
         repaint();
            }
Nikhar
  • 1,074
  • 8
  • 23
  • @HovercraftFullOfEels hey, i have added the code, all painting is done in paintComponent only. – Nikhar Apr 14 '12 at 12:31

1 Answers1

2

This often happens if one draws with a Graphics object obtained by calling getGraphics() on a component since the Graphics object thus obtained is not long-lasting. All drawing should be done from within paintComponent or a method called from paintComponent.

Edit
OK, thanks for posting some code. So I see that you are drawing only a single oval in your code, so it's not surprising that super will prevent you from drawing a line.

Possible solutions:

  • In your mouseDragged(...) method, add Points to a List<Point> and in your paintComponent(...) method, iterate through that list drawing your line (setting the Graphics2D Stroke to change line thickness)
  • or Draw to a BufferedImage and display the BufferedImage in your paintComponent(...) method.

For example, please have a look at my StackOverflow answer to this question: Changing JPanel Graphics g color drawing line

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • i haven't used getGraphic() anywhere. my mouseDragged() gives me new coordinates, i just call repaint from that. – Nikhar Apr 14 '12 at 12:35
  • @Nikhar: please see edit to answer. Also, please see my code in the link I've provided to see an example of my second suggestion. – Hovercraft Full Of Eels Apr 14 '12 at 12:35
  • Thanks man, but can u plz tell me why, when i was drawing ovals, it was not getting drawn with super.PaintComponent(g)? – Nikhar Apr 14 '12 at 17:50
  • @Nikhar: was the behavior that you were seeing a single dot and not a line? Your current code is written to just paint a single dot that's it. – Hovercraft Full Of Eels Apr 14 '12 at 17:51
  • when i call repaint() without super.paintComponent(g), i get a line(not as clear as ur freehandline() code, with breaks, but it was a line), but if i call super.paintComponent(g),and i drag the mouse all i get was a dot visible at the current mouse XY position only , and when i release it, even it is gone. – Nikhar Apr 14 '12 at 17:59
  • The line is only present because you're not resetting the JPanel via super.paintComponent(), and is not a good approach. Again, your current code is written to draw a single small filled oval, one that moves with the mouse, but only a single one. Again, if you want to connect ovals, you must follow my recommendations above. – Hovercraft Full Of Eels Apr 14 '12 at 18:02
  • No, i don't want to connect ovals..lol, i was just experimenting, i would go with ur line code. Thanks. – Nikhar Apr 14 '12 at 18:06