Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Wrapping Up a Java2D Game: Custom Fonts and Transparent PNGs

DZone's Guide to

Wrapping Up a Java2D Game: Custom Fonts and Transparent PNGs

The retro-modern aesthetic is really popular in gaming, especially with indie titles. Here's how to create custom fonts and transparent PNGs to capture that old-school vibe.

· Java Zone
Free Resource

Bitbucket is for the code that takes us to Mars, decodes the human genome, or drives your next car. What will your code do? Get started with Bitbucket today, it's free.

A while back (turns out it was about 8 years ago) I started working on a simple 2d game in the style of Nintendo Game and Watch LCD games that were popular in the 1980s, but I never finished it completely.

I dug up the source and committed it to GitHub in its original state, how I last left it, and then took a look at wrapping up the last few issues and adding finishing touches.

Here’s the source for the game, and here’s the source for a generic 2d game framework that I abstracted from the game as I was developing it. Here’s a couple of posts from when I was actively working on developing this, here and here.

There was a couple of bugs in the animation that I never fixed, and I wanted to polish it up a bit.

It never really had the look of an old monochrome LCD displ

ay, so I first changed the background (using a color dropper to pick a color from online photos), but then realized none of the images were transparent so it ended up looking like this:

Notice the bank on the top-left now has the first of the sprites updated to add transparency. I did this by adding an alpha layer in Gimp per simple steps here. I updated each of the image sprites to resave them as PNGs with a transparent layer.

The end result now looks like this:

The LCD segment font is from http://www.styleseven.com/. To load the custom font, use:


ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, System.class.getResourceAsStream("/fonts/digital-7.ttf")));

And then when you need to use it, create a new Font using it’s name and you can use it in place of any regular system font:


g.setFont(new Font("digital-7", Font.BOLD, 20));

Updated results for the game are here and the 2d game engine here.

Bitbucket is the Git solution for professional teams who code with a purpose, not just as a hobby. Get started today, it's free.

Topics:
java ,game dev

Published at DZone with permission of Kevin Hooke, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}