Drawing a String using Java 2D Graphics API



This example shows how to draw a String on a JFrame using the Java 2D Graphics API.
We cast the graphics object that is sent to the paint method as an argument to an Graphics 2D object, and then call the drawString() method on it.
The first parameter is the string to be drawn, the second is the X coordinate, and the third parameter is the Y coordinate.
Both coordinates has the top left corner as origin, so according to the code below the line will be drawn 100 pixels down and 20 pixels in from the left margin.

This is the result of drawing a string with the Java 2D Graphics API and the code above:

Drawing a String with the Java 2D API

In the example above only parts of the class is presented to make it easier to view.
Here is the complete code of the frame class that uses the Java 2D API to draw a string:







Search for more Java info on this site here:
Custom Search


Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">