Low-hanging graphics programming fruits?

假装没事ソ 提交于 2019-12-03 08:46:14

Implementing blurs and other image filtering effects are fairly simple to perform.

For example, to perform a blur on a BufferedImage, one can use the ConvolveOp with a convolution matrix specified in a Kernel:

BufferedImageOp op = new ConvolveOp(new Kernel(3, 3,
    new float[] { 
        1/9f, 1/9f, 1/9f,
        1/9f, 1/9f, 1/9f,
        1/9f, 1/9f, 1/9f
    }
));

BufferedImage resultImg = op.filter(originalImg, resultImage);

Not quite sure when a blur effect is needed, but it may come in handy some time. But I'd say it's a low-hanging fruit for its ease of implementation.

Here's some information on convolution matrices. It can be used to implement effects such as sharpen, emboss, edge enhance as well.

coobird

Performing a pixelation effect is a low-hanging fruit operation on a BufferedImage.

This can be performed in two steps:

  1. Determine the color of the one block of the pixelation.
  2. Fill in the block of pixelation on the image.

Step 1: Determine the color:

public static Color determineColor(BufferedImage img, int x, int y, int w, int h) {
    int cx = x + (int)(w / 2);
    int cy = y + (int)(h / 2);
    return new Color(img.getRGB(cx, cy), true);
}

In the determineColor method, the pixel color from the center of the BufferedImage is determined, and is passed back to the caller.

Step 2: Fill in the pixelation block with determined color:

BufferedImage sourceImg = ...;  // Source Image.
BufferedImage destimg = ...;    // Destination Image.
Graphics g = destImg.createGraphics();

int blockSize = 8;
for (int i = 0; i < sourceImg.getWidth(); i += blockSize) {
    for (int j = 0; j < sourceImg.getHeight(); j += blockSize) {
        Color c = determineColor(sourceImg, i, j, blockSize, blockSize);
        g.setColor(c);
        g.fillRect(i, j, blockSize, blockSize);
    }
}
g.dispose();

Although there is quite a bit of code, this effect is intellectually a low-hanging fruit -- there isn't much complex that is going on. It is basically finding the center color of a block, and filling a box with that color. This is a fairly naive implementation, so there may be better ways to do it.

The following is a before and after comparison of performing the above pixelation effect:

Filthy Rich Clients describes in great detail a number of very nice Java2D/Swing effects. It also gives an excellent theoretical background to these effects. I'm not sure how much low-hanging fruit there is, but it's a great resource for browsing.

One possibility might be to do something with alpha compositing. Maybe combine alpha composite with Timing Framework. Depending on the rules of your game, it might even be important to gameplay to selectively and time-dependently make objects semi-transparent.

Transparency effects (e.g. for smoke) can make a big difference without too much effort. No idea if this can be done in Java2d though.

Anything that looks kind of realistic, things bouncing off of other things, rolling off, etc. can be kind of cool and if your game is a side scrolling 2D rather than top-down 2D you might be able to use a ready made physics engine like Box2D to do something cool with very little effort. Here's a Java port of Box2D that you could use for it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!