CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Sep 2008
    Posts
    16

    [RESOLVED] How do I repaint without removing previous drawings?

    Code:
    class ShapePanel extends JPanel {
    		Shape currentShape;
    		public void setShape(Shape shape) {
    			currentShape = shape; repaint();
    		}  
    		public void update(Graphics g) {
    			paint(g);
    		}
    		public void paintComponent(Graphics g) {
    			super.paintComponent(g);
    			if (currentShape != null) currentShape.draw(g);
    		}
    	}
    This is called whenever I want to draw a shape to a JPanel. However, everytime it is done, the panel removes all previous drawings, which is not what I intended to be. How do I do it?

  2. #2
    Join Date
    Apr 2007
    Posts
    442

    Re: How do I repaint without removing previous drawings?

    How do you intend it to be? Draw all the Shapes from day one? Draw a portion of the panel or what?

    Suggest the following...

    dont always call super. Often not a wise choice, but works for this particular implementation. Call super if you have no shapes to draw, else draw the shape. This should paint only the area defined by the shape, nothing else.

  3. #3
    Join Date
    Sep 2008
    Posts
    16

    Re: How do I repaint without removing previous drawings?

    i'll try that. thanks for the quick reply.

    there's this button that draws a shape on a panel, then upon another click, draws another shape [with different settings defined by the user*] but does not erase the previous drawing.

    *i already have coded this part properly.

  4. #4
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: How do I repaint without removing previous drawings?

    If that doesn't work, save all previously drawn shapes and draw them all each time.
    Norm

  5. #5
    Join Date
    Sep 2008
    Posts
    16

    Re: How do I repaint without removing previous drawings?

    How do I do that? :|

  6. #6
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: How do I repaint without removing previous drawings?

    The code you posted only paints the current shape, so that's all you will see.

    Java components like JPanels that can have other components added to them paint themselves and their contents by calling paint code on every component they contain. You can either do this by adding your Shapes to the panel (they will need to be subclasses of Component) so they are drawn automatically, or you can keep your own list of all the Shapes that get added to your panel and step through the list drawing each one youself inside the paint method.

    If you add them to the panel, the panel will look after them and do a lot of the legwork for you with its built-in facilities, but you lose some control over them. If you keep your own list, you have to manage everything yourself, but you have complete control.

    The unavoidable price of reliability is simplicity...
    C.A.R. Hoare
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  7. #7
    Join Date
    Sep 2008
    Posts
    16

    Re: How do I repaint without removing previous drawings?

    Yay thanks for the idea. My code's fixed now. I placed them in an ArrayList then just painted the ArrayList for each call to paint();.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured