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.
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.
Performing a pixelation effect is a low-hanging fruit operation on a BufferedImage
.
This can be performed in two steps:
- Determine the color of the one block of the pixelation.
- 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:
![](https://www.manongdao.com/static/images/pcload.jpg)
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.