Results 1 to 20 of 23
Thread: Drawing offscreen?
- 02-22-2012, 03:55 PM #1
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
- 02-22-2012, 04:05 PM #2
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.
How to Ask Questions the Smart Way
Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
- 02-22-2012, 04:42 PM #3
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?
- 02-22-2012, 04:49 PM #4
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?
Painting in AWT and Swing
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 02-22-2012, 04:51 PM #5
Re: Drawing offscreen?
maybe the quantity of the images is causing the lag?
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 02-22-2012, 04:54 PM #6
- 02-22-2012, 05:00 PM #7
- 02-22-2012, 10:59 PM #8
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.
- 02-22-2012, 11:34 PM #9
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
- 02-22-2012, 11:37 PM #10
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
- 02-23-2012, 12:44 PM #11
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); }
Thanks..
- 02-23-2012, 12:50 PM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Re: Drawing offscreen?
Depends what that is trying to do.
What is that subimage it is trying to extract?
- 02-23-2012, 12:51 PM #13
Re: Drawing offscreen?
a smaller part of the image ...
- 02-23-2012, 01:13 PM #14
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
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?
- 02-23-2012, 01:17 PM #15
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?
- 02-23-2012, 01:48 PM #16
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
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.
- 02-23-2012, 01:52 PM #17
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...
- 02-23-2012, 01:54 PM #18
- 02-23-2012, 02:00 PM #19
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
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?
- 02-23-2012, 02:15 PM #20
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 =)
Similar Threads
-
offscreen.getGraphics() error
By luis.depedro in forum AWT / SwingReplies: 10Last Post: 01-21-2012, 10:43 PM -
Drawing an arc
By berkeleybross in forum Java 2DReplies: 10Last Post: 12-09-2010, 02:32 AM -
Drawing a map
By Karp in forum AWT / SwingReplies: 4Last Post: 11-07-2008, 01:26 PM -
drawing an image to an offscreen image
By hunterbdb in forum Java 2DReplies: 9Last Post: 10-30-2008, 07:17 PM -
Problem Setting offscreen background
By D34N0 in forum Java AppletsReplies: 1Last Post: 07-14-2007, 12:46 PM
Bookmarks