Provide information in textual form as your program runs.
As you write more sophisticated programs, you may find the need
to display textual or numeric information on the screen. Sofia
supports adding text using the TextShape
class
in the package sofia.graphics
.
You can think of a TextShape
object as being similar
to an actor, but one that is visually represented on the screen
as a single line of text rather than an image. TextShape
objects are added to the world just like actors, and they can be
positioned and rotated the same way.
The primary difference between
a TextShape
and an Actor
is that the
TextShape
class comes from Sofia's general-purpose
2D drawing library (which goes well beyond Actor/World micro-world
classes), and uses a floating point coordinate system instead of
an integer-based one. As a result, you use
setX()
/setY()
/setLocation()
and
getX()
/getY()
methods, all of which use
float
values, instead of using the integer-based methods
available in the Actor
class (setGridX()
,
setGridY()
, setGridLocation()
,
getGridX()
, and getGridY()
).
When you position a text shape, you specify the grid cell location where the text should appear. By default, the text is centered at the specified location (ask on Piazza if you want to use a different alignment strategy). Here is a simple example:
import sofia.graphics.*; // To access TextShape and Color classes // ... inside some method within a World class, for example ... TextShape text = new TextShape("Hello World!"); this.add(text, 5, 5); // Add text to this world at the given position // ... change the size of the text, in points (can be an int or float) text.setTypeSize(25); // ... change the color of the text text.setColor(Color.black); //... use Hex color codes text.setColor(Color.rgb(0xff, 0xaa, 0x88)); // ... change the font (uses android.graphics.Typeface class) text.setTypeface(Typeface.create("times new roman", Typeface.BOLD));