低悬的图形编程水果?

Ido*_*eli 3 java graphics java-2d

我目前正在研究Java2D中基于磁贴的游戏,我正在考虑添加一些便宜的眼睛糖果.

例如,实现一个简单的粒子系统(可能像这样对于爆炸和/或烟雾).

您是否有任何关于相对容易编程的效果的建议,这些效果不需要大量(或根本)绘制新艺术?

这些效果的教程和代码示例也是最受欢迎的!

-我做.

PS - 如果绝对必要,我可以切换到像LWJGL/JOGL甚至是Slick这样的东西 - 但我宁愿继续使用Java2D.

coo*_*ird 6

实现模糊和其他图像过滤效果非常简单.

例如,要对a执行模糊BufferedImage,可以使用以下ConvolveOp指定的卷积矩阵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);
Run Code Online (Sandbox Code Playgroud)

不太确定何时需要模糊效果,但它可能会在某个时候派上用场.但我认为它易于实施,这是一个悬而未决的成果.

这是卷积矩阵的一些信息.它可用于实现锐化,浮雕,边缘增强等效果.


coo*_*ird 5

执行像素化效果是一个简单的水果操作BufferedImage.

这可以分两步执行:

  1. 确定像素化的一个块的颜色.
  2. 填写图像上的像素化​​块.

第1步:确定颜色:

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);
}
Run Code Online (Sandbox Code Playgroud)

在该determineColor方法中,BufferedImage确定来自中心的像素颜色,并将其传递回调用者.

第2步:使用确定的颜色填充像素化块:

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();
Run Code Online (Sandbox Code Playgroud)

虽然有相当多的代码,但这种影响在智力上是一个悬而未决的成果 - 没有太多复杂的事情发生.它基本上是找到一个块的中心颜色,并用这种颜色填充一个盒子.这是一个相当天真的实现,因此可能有更好的方法来实现它.

以下是执行上述像素化效果之前和之后的比较:

非像素化图像http://coobird.net/img/grad64.png 像素化图像http://coobird.net/img/grad64p.png