Drawing a part of an image (Java)

前端 未结 3 2057
粉色の甜心
粉色の甜心 2020-12-21 22:32

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 m

相关标签:
3条回答
  • 2020-12-21 23:00

    This isn't a great solution, but what if you drew the entire floor, then a black image with a transparent hole in its center?

    0 讨论(0)
  • 2020-12-21 23:06

    You can do this with the setClip() method in Graphics.

    It needs some other work, but:

    import java.awt.Graphics;
    import java.awt.geom.QuadCurve2D;
    //...
    
      g.setClip(new QuadCurve()); // Set the bounding curve for the image.
      g.drawImage(...);
    

    As I said, it needs more work, meaning the QuadCurve2D object might need to be defined differently, but you can check the doc for that.

    0 讨论(0)
  • 2020-12-21 23:06

    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.

    0 讨论(0)
提交回复
热议问题