Question About Graphics.setClip()
I'm using the Graphics context of a Component.createImage() Image to do double-buffering. (I'm not using a BufferStrategy because it isn't that heavy-duty a game and I want low-end machines to be able to run it) It's a Diablo-style roam-n-kill type of thing. I don't want Sprites beyond a certain range of the viewport (the game runs in a JScrollPane) to be painted, in order to save system resources.
Is setting the clip a good way to prevent system resources from being spent on painting? Can anyone offer a better way?
Thanks very much,
Bret
[576 byte] By [
bh94704a] at [2007-9-28 6:42:22]

> I'm using the Graphics context of a
> Component.createImage() Image to do double-buffering.
> (I'm not using a BufferStrategy because it isn't that
> heavy-duty a game and I want low-end machines to be
> able to run it) It's a Diablo-style roam-n-kill type
> of thing. I don't want Sprites beyond a certain range
> of the viewport (the game runs in a JScrollPane) to be
> painted, in order to save system resources.
>
> Is setting the clip a good way to prevent system
> resources from being spent on painting? Can anyone
> offer a better way?
>
> Thanks very much,
> Bret
1st things 1st - don't use JScrollPane.
2ndly, assuming you are using JScrollPane, I also assume your using passive rendering (repaint()) - don't use passive rendering, use active rendering.
3rd, I'm afraid to ask, but does that mean every sprite is a Component, that has been added to the JScrollPane? You realy don't want to be doing that.
now, to the issue of setClip(..)
setClip is simply designed to limit the area on which draw operations effect. It will reduce the cost of drawing onto pixels outside the View rectangle considerably, however, it will not completely eliminate it.
What you need is a mechanism by which you can reject Sprites that are outside the View rectangle for zero, (or close to zero) cost.
A simple way of doing it, would be to maintain all the Sprites on a level, in a list, sorted by their x position. Whenever you process your Sprites, you simply have to update all Sprites between viewPort.left and viewPort.right.
Heh heh, I've been trying to get away from Swing components...:)
Each sprite was an image drawn onto an extension of JPanel, which was then the viewport argument to ScrollPane. I've gathered from reading this forum that Swing is inefficient, but I haven't thought of a good way to implement a scrolling map without it. If anyone knows a quick answer to that one, I'd appreciate it. :)
I had tried active rendering, but my getGraphics calls were always returning null pointers even though the component had been created/displayed. Folks rightly suggested that this wouldn't happen inside the Paint() method. I suppose I'll return to my battle stations on that one. (And again, if there's a handy rule of thumb on this problem, I'd love to know it).
Hmmm...I guess all game graphics should be g.drawn directly to a top-level window of some sort?
Sorry to be so full of questions. Thanks Abuse for your help. The sorted list suggestion is especially excellent.
Bret
setClip creates a **** load of garbage, if you can live without it, or code your own cliping method, I'd suggest not to use it.
It is the same kind of garbage that Graphics2D creates, bad either way. If I recong correct there is another way to clip, but I haven't done it in awhile.
Anyhow, thats for smooth animation. If you don't need smooth animation, then I guess it doesn't matter.