Jump to content
Jett

The basics of 2D graphics production in Java

Recommended Posts

[CENTER][I][COLOR="#FF0000"]THIS TUTORIAL IS NOT COMPLETE![/COLOR][/I]
[I]This is an add-on to the "RuneLocus Java tutorials". Please read [URL="http://www.runelocus.com/forums/showthread.php?34729-The-Basics-of-Java"]The Basics of Java[/URL] posted by [URL="http://www.runelocus.com/forums/member.php?153-Trey"]Trey[/URL] [B]before[/B] you dive into this tutorial.[/I]

[SIZE=4][COLOR="#FF8C00"]Syllabus:[/COLOR][/SIZE]
Introduction
Expectations
Getting started
References

[SIZE=4][COLOR="#FF8C00"]Introduction:[/COLOR][/SIZE]
Welcome to my tutorial on the basics of 2D graphics production in Java. I am Scott, I commonly go by the name of Jett because I am a MapleStory nerd. I have been programming for many years now, having progressed a lot farther than I had initially thought I ever would in the field. A lot (I dare to say to much) of my time has been spent in RuneScape private server communities since 2006 and after all of this time I am more interested in sharing the knowledge I have collected over the years with these communities rather than keep it all to myself. With that said, hopefully I will be able to provide you with further insight into the Java programming language and what it provides in the way of 2D graphics production.

[SIZE=4][COLOR="#FF8C00"]Expectations:[/COLOR][/SIZE]
This tutorial, while dumbed down a little, still assumes that you have a basic understanding of the Java programming language as well as the AWT (Abstract Window Toolkit), and the Swing API's provided in the Java API. If you think you need to brush up on some stuff before diving into this tutorial I urge you to reference [URL="http://www.runelocus.com/forums/showthread.php?34729-The-Basics-of-Java"]The Basics of Java[/URL] posted by [URL="http://www.runelocus.com/forums/member.php?153-Trey"]Trey[/URL], or the good 'ol [URL="http://docs.oracle.com/javase/tutorial/java/"]Java™ Tutorials[/URL].

[SIZE=4][COLOR="#FF8C00"]Getting started:[/COLOR][/SIZE]
The Java API provides a very robust API for rendering any kind of graphics you can think of. The first thing you are going to want to look at when you are looking into 2D graphics production is [I]java.awt.Graphics2D[/I]. [I]java.awt.Graphics2D[/I] provides all of the methods and utilities you will ever need to render and manipulate 2D graphics on a basic, and even advanced level. Getting to know your API is very crucial so before moving on, please read the Java documentation for the following classes:
[URL="http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html"]Graphics.java[/URL]
[URL="http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics2D.html"]Graphics2D.java[/URL]
You will notice right away that you have two discernible groups of methods; the methods that actually draw the graphics, and those that manipulate graphics attributes in order to produce different graphical effects. If you could not discern between the two groups, here are a couple basic examples illustrating the two different kinds of methods you are looking at:

[spoiler=Examples]
[URL="http://docs.oracle.com/javase/8/docs/api/java/awt/Graphics.html#drawRect-int-int-int-int-"]drawRect(int, int, int, int)[/URL]
[IMG]http://i.imgur.com/XgFUtfD.png[/IMG]

[URL="http://docs.oracle.com/javase/8/docs/api/java/awt/Graphics.html#fillRect-int-int-int-int-"]fillRect(int, int, int, int)[/URL]
[IMG]http://i.imgur.com/TDSOIqR.png[/IMG]

[URL="http://docs.oracle.com/javase/8/docs/api/java/awt/Graphics.html#drawString-java.lang.String-int-int-"]drawString(java.lang.String, int, int)[/URL]
[IMG]http://i.imgur.com/y3KmzgD.png[/IMG]

[URL="http://docs.oracle.com/javase/8/docs/api/java/awt/Graphics.html#setColor-java.awt.Color-"]setColor(Color)[/URL]
[IMG]http://i.imgur.com/jc2fA62.png[/IMG][/spoiler]The methods for actually drawing the shapes, images, and text rely on the applications pixel coordinate grid to position the graphics where you tell them to be, and the methods for manipulating graphics rely on both position and user input in regards to what kinds of manipulation need to happen (for example: color, gradient, font type, etc ...). Here is an application I wrote that uses color, text, and basic shapes to help you get a feel for both the coordinate grid and the basics of producing your own graphics:

(Click the picture to view the source code -- Java 8 is required to run this application!)
[URL="http://pastebin.ubuntu.com/8368689/"][IMG]http://i.imgur.com/gN10qfA.png[/IMG][/URL]

[COLOR="#FF8C00"][SIZE=4]To be continued...[/SIZE][/COLOR]
I will be adding onto and revising every section of this tutorial over time. Please tell me if there are areas that I could be more clear, or just didn't elaborate enough on. Thank you for your continued support!

[COLOR="#FF8C00"][SIZE=4]References:[/SIZE][/COLOR]
[URL="http://docs.oracle.com/javase/tutorial/2d/basic2d/"]Getting Started with Graphics[/URL]
[URL="http://docs.oracle.com/javase/tutorial/2d/geometry/index.html"]Working with Geometry[/URL][/CENTER] Edited by Jett

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×