Page 1 of 2 12 LastLast
Results 1 to 20 of 23
Like Tree1Likes

Thread: Drawing offscreen?

  1. #1
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    5

    Default Drawing offscreen?

    If you are going to draw ~400 objects to the screen and then draw more things on them ex. first draw the tree then draw the leafs and birds etc.
    Would it not be beneficial to draw everything in a background thread on a buffered image and then pass that image to paintComponent to draw on the screen or is the event dispatch thread capable to draw everything on its own with a good frame rate? Thanks

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,988
    Rep Power
    9

    Default Re: Drawing offscreen?

    What happened when you tried just drawing it from paintComponent? Whether you get a good frame rate depends entirely on quite a few things that only you can know, so the only way to get an answer is to try it yourself.
    DarrylBurke likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    5

    Default Re: Drawing offscreen?

    Well ... I tried... but I am experiencing lag. This lag must come from the paintComponent because I have to much logic in it. As i'm only painting the "ground" now it will get worse when i add more objects. So basically i'm going to migrate all the logic to another thread who will be constructing the screen and then pass it to the paintComponent to draw.
    My only questions are about the approach, so whats the judgement? Good approach, bad approach? It's a whole lot of work to change it so i need to be sure that im on the right track.

    I was told that the paintComponent already draws double buffered images ... that sounds like bull to me. How can bufferedImages in a array being drawn img by img be double buffered? ... maybe the quantity of the images is causing the lag?

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Drawing offscreen?

    I was told that the paintComponent already draws double buffered images ... that sounds like bull to me. How can bufferedImages in a array being drawn img by img be double buffered?
    The Swing system maintains its own back buffer.
    Painting in AWT and Swing

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Drawing offscreen?

    maybe the quantity of the images is causing the lag?
    I hope you're not loading the images in the painting method override.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    5

    Default Re: Drawing offscreen?

    Quote Originally Posted by DarrylBurke View Post
    I hope you're not loading the images in the painting method override.

    db
    They a loaded before GUI is shown ... but still... something is obviously wrong.

  7. #7
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    5

    Default Re: Drawing offscreen?

    Quote Originally Posted by DarrylBurke View Post
    The Swing system maintains its own back buffer.
    Painting in AWT and Swing

    db
    for an example: if I have an multidimensional array with 100x100 buffered images in it and i draw them all to the JPanel using a for loop, swing would then first loop them all to a buffer and then draw them all together?

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: Drawing offscreen?

    Do you need to draw all 10,000 images every time
    Have you timed how long that takes?

    Are there any that don't move and could be predrawn on an image and then a copy of that image be drawn over with the moving images.

  9. #9
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    5

    Default Re: Drawing offscreen?

    Problem is ... In the game the hero is centered an the world moves ... objects moves... other players move... enemy's move ... it's a mess =P
    Takes about 50 ms to draw it witch is to much ... So im guessing a background thread for the logic is a good idea? Thanks

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: Drawing offscreen?

    Using a thread could help if you have more then one core/cpu. It there is only one, then there is no way to overlap processing and have concurrent processing for the background and foreground.

    Try making a background thread and see what happens

  11. #11
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    5

    Default Re: Drawing offscreen?

    Okey so I located the villain in my code... basically the getImage + getSubimage + drawImage takes ~1 ms to execute and thats to much !
    How can i get a subimage from a buffered image in a faster way?

    Java Code:
    public void paintMe(Graphics2D g2d, int posX, int posY,int subPosX,int subPosY, int subSizeX, int subSizeY){    
    BufferedImage draw = currentBackground.getImg().getSubimage(subPosX, subPosY, subSizeX, subSizeY);
     g2d.drawImage(draw, posX, posY, draw.getWidth(), draw.getHeight(), null);
    }
    The problem...

    Thanks..

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: Drawing offscreen?

    Depends what that is trying to do.
    What is that subimage it is trying to extract?

  13. #13
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    5

    Default Re: Drawing offscreen?

    a smaller part of the image ...

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: Drawing offscreen?

    But what is that image?
    That's what I'm asking.

    Is it the full map, only part of which you are displaying to the user?
    Is it a bitmap containing the various images you are using?
    What is it?

  15. #15
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    5

    Default Re: Drawing offscreen?

    It is a part of a 40x40 pixel image that is one of ~340 blocks creating the background ... or in game programming words "one tile" ...

    So is there a faster way to get a subimage out of a bufferedImage?

  16. #16
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: Drawing offscreen?

    Not as far as I'm aware, especially since timing how long that takes is getting into the realms of clock accuracy.

    Why are you redrawing a single tile based on the original graphic?
    I'm trying to see if the drawing logic itself could be the problem.

  17. #17
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    5

    Default Re: Drawing offscreen?

    well it might be the problem ... basically I'm drawing exactly the objects that fits in the screen, so if one object is outside of the jpanel i "cut it" so that i get only the piece of it that i want to display ... But i'm starting to think its better to draw some part of the images outside of the panel then to cut them...

  18. #18
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    5

    Default Re: Drawing offscreen?

    Quote Originally Posted by Tolls View Post
    Not as far as I'm aware, especially since timing how long that takes is getting into the realms of clock accuracy.

    Why are you redrawing a single tile based on the original graphic?
    I'm trying to see if the drawing logic itself could be the problem.
    Well the background moves and the hero is centered ...

  19. #19
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: Drawing offscreen?

    SO the background is held in a buffered image?
    Then why not simply blit the image with a new offset and then blit the hero onto that image?

  20. #20
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    5

    Default Re: Drawing offscreen?

    blit? every time the background moves i have to repaint it with a new image? The the background is held in a 1000x1000 2d array .. each of the objects in this array has reference to a 40x40 pixel tile ....

    lets say all of them are "grass images" and i want to create some water i my map , i just click on the object in that position and change the 40x40 pix grass image to a water image. This is the reason why i need to draw each one of them until the screen is filled.

    And thank you for your fast replys =)

Page 1 of 2 12 LastLast

Similar Threads

  1. offscreen.getGraphics() error
    By luis.depedro in forum AWT / Swing
    Replies: 10
    Last Post: 01-21-2012, 09:43 PM
  2. Drawing an arc
    By berkeleybross in forum Java 2D
    Replies: 10
    Last Post: 12-09-2010, 01:32 AM
  3. Drawing a map
    By Karp in forum AWT / Swing
    Replies: 4
    Last Post: 11-07-2008, 12:26 PM
  4. drawing an image to an offscreen image
    By hunterbdb in forum Java 2D
    Replies: 9
    Last Post: 10-30-2008, 06:17 PM
  5. Problem Setting offscreen background
    By D34N0 in forum Java Applets
    Replies: 1
    Last Post: 07-14-2007, 11:46 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
  •