CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2000
    Posts
    1

    Capturing and Redrawing an Image


    Hi,
    I'm building a small drawing application in Swing. I'm using JComponent as the drawing board,
    I capture the drawing on the JComponent using the following code:


    RepaintManager manager = RepaintManager.currentManager(drawBoard);
    drawing = manager.getOffscreenBuffer(drawBoard, drawBoard.getWidth(), drawBoard.getHeight());




    and I use the following code for redrawing the image on the JComponent


    getGraphics().drawImage(drawing,0,0,drawBoard.getWidth(), drawBoard.getHeight(),drawBoard);




    What happens when redrawing is that I get other parts on the GUI redrawn on the drawing board!
    Any one can tell me what am I doing wrong? should I try something else?

    Thanks in advance,
    Ayman


  2. #2
    Join Date
    Mar 1999
    Location
    Utah
    Posts
    66

    Re: Capturing and Redrawing an Image

    I've had similar problems. You are supposed to draw on a JComponent (such as JPanel) by overriding the
    method paintComponent. I was trying to draw outside of this method by getting a graphics context with
    getGraphics like you and having mixed results. I asked my teacher and he said to stay away from the
    getGraphics method and try to put all my rendering code inside paintComponent. Try this

    public void paintComponent(Graphics g)
    {
    super.paintComponent(g);
    //additional drawing code here using this g
    //g.drawImage(image,0,0,this);

    }



    if you forget to call the superclasses paintComponent you will get strange results too. I did it once
    and my menus were being painted on all my panels.


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