Creating Images with Java

Java is a powerful language that’s been popular in education for many years. Like all programming languages, it’s often helpful for learners to work with images. The visual feedback that images give can be more engaging and more intuitive than text alone. Yet, many instructors don’t know that you can use Java’s built-in libraries to easily make your own images!

In this quick post I’ll use the Java Trinket to create a simple image, a version of the Trinket logo. The code is based on an excellent example by Byron Kiourtzoglou over on Java Code Geeks.

In this example, we make both a png and a jpeg from the same Graphics2d object. Check it out:

I’ve left inline comments throughout the trinket. Click the bubbles on the left to expand the comments and learn more about the various parts of the program. To customize image, edit the code on the left and click Run!

Want more Java image examples? Java Code Geeks has a whole imageio category with other tutorials.

Want to go deeper with Java? Trinket offers a free interactive Java textbook, Think Java, by Allen Downey and Chris Mayfield.

Java trinkets are free to run, edit, and share, and a premium Trinket Connect subscription lets you create and save unlimited to your Trinket account.  A Trinket Connect subscription will also let you make cool inline comments like you see in the trinket above.

Enjoy!