Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Resetting graphics

  1. #1
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Resetting graphics

    How can I reset the whole Graphics in my paint?
    For example for every rendering method that I have I would like to reset everything to default.

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

    Default Re: Resetting graphics

    Quote Originally Posted by PhQ View Post
    How can I reset the whole Graphics in my paint?
    For example for every rendering method that I have I would like to reset everything to default.
    Don't. Instead use the Graphics#create() method to make a copy of your Graphics object, change the properties of the copy, draw with the copy and then dispose it.

  3. #3
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: Resetting graphics

    Quote Originally Posted by Fubarable View Post
    Don't. Instead use the Graphics#create() method to make a copy of your Graphics object, change the properties of the copy, draw with the copy and then dispose it.
    :)
    Thank you.

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

    Default Re: Resetting graphics

    Quote Originally Posted by PhQ View Post
    :)
    Thank you.
    You're welcome, but again, don't forget to call dispose() on the copy lest you run out of resources.

  5. #5
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: Resetting graphics

    Quote Originally Posted by Fubarable View Post
    You're welcome, but again, don't forget to call dispose() on the copy lest you run out of resources.
    Is there a good way to add multi-tasking to each of my rendering methods?
    I just did it and started flickering and it took like 5 mins to close my program.
    Or multi-tasking isn't useful for painting graphics?
    Because in one of the rendering methods I am trying to make it sleep for a few ms.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Resetting graphics

    Quote Originally Posted by PhQ View Post
    Is there a good way to add multi-tasking to each of my rendering methods?
    I just did it and started flickering and it took like 5 mins to close my program.
    Or multi-tasking isn't useful for painting graphics?
    Because in one of the rendering methods I am trying to make it sleep for a few ms.
    Wait, you're surely not calling Thread.sleep(...) inside of a paint or paintComponent method, are you?

    ... and what do you mean by "adding multi-tasking to the rendering"?

  7. #7
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: Resetting graphics

    Quote Originally Posted by Fubarable View Post
    Wait, you're surely not calling Thread.sleep(...) inside of a paint or paintComponent method, are you?

    ... and what do you mean by "adding multi-tasking to the rendering"?
    :D I did call Thread.sleep().
    "adding multi-tasking to the rendering" - making it so it draws the graphics on a different Thread.
    Last edited by PhQ; 01-11-2012 at 12:31 AM.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Resetting graphics

    Quote Originally Posted by PhQ View Post
    :D I did call Thread.sleep.
    "adding multi-tasking to the rendering" - making it so it draws the graphics on a different Thread.
    Let me be clear: never, never, never call Thread.sleep inside of a paint or paintComponent method. Ever.

    I believe that you can draw to a BufferedImage within a background thread and then when complete have the paintComponent draw the image by calling repaint. But you should have no threading code within paint or paintComponent. These methods need to be lean, mean, and fast, and do nothing but draw.
    DarrylBurke likes this.

  9. #9
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: Resetting graphics

    Quote Originally Posted by Fubarable View Post
    Let me be clear: never, never, never call Thread.sleep inside of a paint or paintComponent method. Ever.

    I believe that you can draw to a BufferedImage within a background thread and then when complete have the paintComponent draw the image by calling repaint. But you should have no threading code within paint or paintComponent. These methods need to be lean, mean, and fast, and do nothing but draw.
    Ok thanks. I'm going to make my own method to delay my graphics.
    Last question, should I use System.currentTimeMillis(); or System.nanoTime(); ?

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Resetting graphics

    Quote Originally Posted by PhQ View Post
    Ok thanks. I'm going to make my own method to delay my graphics.
    Last question, should I use System.currentTimeMillis(); or System.nanoTime(); ?
    Again, you don't delay the graphics itself, but instead delay the progression of the game logic in your game loop. The slowing down of the graphics will follow through as a natural response to this. And also, never call Sleep in your event thread either.

  11. #11
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: Resetting graphics

    Quote Originally Posted by Fubarable View Post
    Again, you don't delay the graphics itself, but instead delay the progression of the game logic in your game loop. The slowing down of the graphics will follow through as a natural response to this. And also, never call Sleep in your event thread either.
    Yeah, I know, I will make it so it appears for a few seconds and then disappears. It will be like an animation.

Similar Threads

  1. Boolean value not resetting
    By BillyB in forum New To Java
    Replies: 1
    Last Post: 03-30-2011, 03:28 PM
  2. Drawing a graphics onto another Graphics ?
    By Ziden in forum Java Applets
    Replies: 0
    Last Post: 01-08-2011, 07:30 PM
  3. Help me with graphics
    By 7oclock in forum New To Java
    Replies: 12
    Last Post: 04-04-2009, 11:20 PM
  4. Help with 2d graphics please
    By xbox_nutter in forum New To Java
    Replies: 0
    Last Post: 04-02-2009, 11:48 AM
  5. Graphics
    By Joe2003 in forum Advanced Java
    Replies: 1
    Last Post: 01-25-2008, 06:24 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
  •