Results 1 to 3 of 3
  1. #1
    martypapa is offline Member
    Join Date
    Feb 2010
    Posts
    21
    Rep Power
    0

    Default How do I improve the background image efficiency?

    Hi,
    I am making a game that uses a thread to animate. When I repaint a large percentage of my CPU is taken up. I am assuming this has something to do with the large background image.

    Firstly, is it better to use an Image or Buffered Image for the background in this case, keeping in mind that there are buttons and shapes overlapping it that get repainted regularly.

    If there is no difference, would it help to draw the background onto a different graphics context, if that is possible and makes any sense.

    Thanks

  2. #2
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    Java Code:
    BufferedImage backimage = ((Graphics2D)this.getGraphics()).getDeviceConfiguration().createCompatibleImage(
             width, height,java.awt.Transparency.OPAQUE);
    What i do is render to a backimage but only when things change.

    I have a background scenery with ground tiles (many of them), roads, trees that for most of the time don't update. If the user moves the scene then i re-render this part to the backimage.

    For those things that do change constantly, like animations, i draw them to the graphics object everytime the panel is painted (paintComponent).

    Flow for paintComponent:
    - Did the scene move (they moved it with the mouse)?
    yes - redraw all the non animated images to backimage
    no - leave backimage alone
    - draw the backimage to the screen
    - draw all the animated images to the screen
    My Hobby Project: LegacyClone

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

    Default

    RXD, the question relates to Java2D, not to Java ME. Please read more carefully so that you don't again post a response which, while correct in its own way, is totally out of context.

    db

Similar Threads

  1. Need help with JFrame background image
    By ProGenius in forum New To Java
    Replies: 6
    Last Post: 12-27-2009, 04:17 PM
  2. Background image
    By leiferouis in forum New To Java
    Replies: 9
    Last Post: 03-08-2009, 05:49 PM
  3. how i can put image in background of frame??
    By ahmed13 in forum NetBeans
    Replies: 4
    Last Post: 01-15-2009, 05:47 PM
  4. Image as background
    By Java.child in forum AWT / Swing
    Replies: 2
    Last Post: 10-02-2008, 11:37 PM
  5. Background image in java
    By toby in forum AWT / Swing
    Replies: 1
    Last Post: 07-29-2007, 07:15 AM

Posting Permissions

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