低垂的图形编程成果?

发布于 2024-07-24 18:03:45 字数 346 浏览 3 评论 0原文

我目前正在使用 Java2D 开发一款基于图块的游戏,我正在考虑添加一些廉价的视觉效果。

例如,实现一个简单的粒子系统(可能类似于这个)用于爆炸和/或吸烟。

对于相对容易编程且不需要绘制大量(或根本不需要)新艺术的效果,您有什么建议吗?

上述效果的教程和代码示例也将受到欢迎!

-我愿意。

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

I'm currently working on a tile-based game in Java2D, and I was thinking of adding some cheap eye candy.

For example, implementing a simple particle system (maybe something like this) for explosions and/or smoke.

Do you have any suggestion for relatively easy to program effects that wouldn't require drawing a lot (or at all) of new art?

Tutorials and code samples for said effects would also be most welcome!

-Ido.

PS - if absolutely necessary I could switch to something like LWJGL/JOGL or even Slick - but I rather stay with Java2D.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

骄兵必败 2024-07-31 18:03:45

实现模糊和其他图像过滤效果相当简单。

例如,要对 < code>BufferedImage,可以使用 ConvolveOpKernel

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);

不太确定何时需要模糊效果,但有时可能会派上用场。 但我认为这是一个容易实现的目标,因为它易于实施。

以下是有关卷积矩阵的一些信息。 它也可用于实现锐化、浮雕、边缘增强等效果。

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.

起风了 2024-07-31 18:03:45

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);
}

defineColor 方法中,确定 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();

虽然有相当多的代码,但这种效果在智力上是一个容易实现的成果——没有太多复杂的事情发生。 它基本上是找到一个块的中心颜色,并用该颜色填充一个盒子。 这是一个相当幼稚的实现,因此可能有更好的方法来做到这一点。

下面是执行上述像素化效果的前后对比:

非像素化图像< img src="https://i.sstatic.net/bxVN2.png" alt="像素化图像">

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:

Non-pixelated imagePixelated image

蘸点软妹酱 2024-07-31 18:03:45

Filthy Rich Clients 详细描述了许多非常好的 Java2D/Swing 效果。 它还为这些效应提供了极好的理论背景。 我不确定有多少唾手可得的成果,但它是一个很好的浏览资源。

一种可能是使用 alpha 合成进行一些操作。 也许将 alpha 复合与 时序框架 结合起来。 根据游戏规则,有选择地、随时间使对象半透明对游戏玩法来说甚至可能很重要。

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.

你没皮卡萌 2024-07-31 18:03:45

透明效果(例如烟雾)无需太多努力即可产生很大的差异。 不知道这是否可以在 Java2d 中完成。

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.

久夏青 2024-07-31 18:03:45

任何看起来很真实的东西、从其他东西上弹起的东西、滚动的东西等等都可能很酷,如果你的游戏是横向滚动 2D 而不是自上而下的 2D,你可能可以使用现成的物理引擎就像 Box2D 一样,只需很少的努力就能做一些很酷的事情。 这是您可以使用的 Box2D 的 Java 端口

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文