Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
733 views
in Technique[技术] by (71.8m points)

graphics - Drawing a part of an image (Java)

Here's my problem- I'm trying to draw a circular part of a single image.

I'm doing some work on a top-down dungeon crawler sort of game, and I'm attempting to make a light radius around the player. The floor is a single image, and I need to draw only a small, circular part of it. I've been looking at this method:

drawImage(Image img,
          int dx1, 
          int dy1,
          int dx2,
          int dy2,
          int sx1,
          int sy1,
          int sx2,
          int sy2,
          Color bgcolor, 
          ImageObserver observer) 

But, that looks like it would only draw a square subsection. Does anyone happen to know an easier method than drawing tons of little squares to give the illusion of a circle?

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The neatest effect for a light radius would be to use an overlay with a gradient in its alpha channel.

Something like this:

// do this once during setup
overlay = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGBA);
for (int x = 0; x < width; ++x)
{
    for (int y = 0; y < height; ++y)
    {
        double range = 100;
        double distance = Math.sqrt(Math.pow(x - width / 2, 2) + Math.pow(y - height / 2, 2));
        int value = Math.max(100, (int)Math.round(255 - 100 * distance / range));
        overlay.setRGB(x, y, new Color(0, 0, 0, value));
    }
}
....
// do this every frame
gfx.drawImage(overlay, 0, 0, null);

I did not compile this so it's probably full of errors!

If you want some "flicker" in it you can generate several maps, and add some noise to the alpha values. Or even tune the colors so you get warmer light.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...