Results 1 to 4 of 4
  1. #1
    gib65 is offline Member
    Join Date
    Jun 2010
    Posts
    86
    Rep Power
    0

    Default do I need to dispose of my Graphics object?

    I've got the following method. It takes in a BufferedImage, creates a Graphics object from it, uses that Graphics object to draw into the BufferedImage, and then returns.

    Java Code:
    	public void draw(BufferedImage BI) {
    
    		Graphics2D G = BI.createGraphics();
    
    		G.setColor(Color.BLUE);
    
    		if (side == NORTH || side == SOUTH)
    			G.drawRect(X, Y, LENGTH, THICKNESS);
    		else
    			G.drawRect(X, Y, THICKNESS, LENGTH);
    	}
    Now, I've been told that the last thing I should do in a method like this is to add G.dispose(), but I thought that whatever is created in a method is local to that method only, and is therefore automatically released or destroyed at the end of the method.

    Is it important to explicitely dispose of the Graphics object at the end of the method.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Yes you should, you must dispose of the Graphics object here. Even though the variable goes out of scope, I don't think that the system resources that are used by the Graphics object are released unless you explicitly call dispose.

    Note that on the other hand, should not dispose the Graphics object passed into a paintComponent override.

    edit: good golly, as always, the API is a font of knowledge. Please have a look at the Graphics section and in particular the dispose method:

    Disposes of this graphics context and releases any system resources that it is using. A Graphics object cannot be used after disposehas been called.

    When a Java program runs, a large number of Graphics objects can be created within a short time frame. Although the finalization process of the garbage collector also disposes of the same system resources, it is preferable to manually free the associated resources by calling this method rather than to rely on a finalization process which may not run to completion for a long period of time.

    Graphics objects which are provided as arguments to the paint and update methods of components are automatically released by the system when those methods return. For efficiency, programmers should call dispose when finished using a Graphics object only if it was created directly from a component or another Graphics object.
    Last edited by Fubarable; 07-06-2010 at 04:38 AM.

  3. #3
    gib65 is offline Member
    Join Date
    Jun 2010
    Posts
    86
    Rep Power
    0

    Default

    Thanks very much Fubarable.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. dispose() does not work here
    By arunkumarinfo in forum NetBeans
    Replies: 5
    Last Post: 12-28-2013, 06:42 PM
  2. how to dispose?
    By justlynn in forum NetBeans
    Replies: 15
    Last Post: 07-01-2010, 03:17 AM
  3. dispose()'ing a frame from itself.
    By musasabi in forum New To Java
    Replies: 12
    Last Post: 05-13-2010, 08:41 PM
  4. dispose
    By simontkk2005 in forum AWT / Swing
    Replies: 3
    Last Post: 11-19-2009, 12:42 AM
  5. How to set line width in Graphics object?
    By inc_123 in forum New To Java
    Replies: 8
    Last Post: 08-15-2009, 11:34 PM

Posting Permissions

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