/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * AndroidWorld Library, Copyright 2011 Bryan Chadwick * * * * FILE: ./android/image/Text.java * * * * This file is part of AndroidWorld. * * * * AndroidWorld is free software: you can redistribute it and/or * * modify it under the terms of the GNU General Public License * * as published by the Free Software Foundation, either version * * 3 of the License, or (at your option) any later version. * * * * AndroidWorld is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with AndroidWorld. If not, see . * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ package android.image; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Typeface; /** * * * A Class representing an Image of a String. * *
 *    new Text("Hello", 24, "olive")
* *
* *
 *    new Text("Goodbye", 36, "indigo")
* *
* */ public class Text extends Image{ protected String str; protected int size; protected Paint paint; protected double height, width; /** Construct a text Image of the given String, size, and color */ public Text(String str, int size, String color){ this(str, size, "solid", color); } /** Construct a text Image of the given String, size, and color */ public Text(String str, int size, String mode, String color){ super(0,0); this.str = str; this.size = size; this.paint = painter(color(color), mode(mode)); paint.setTypeface(Typeface.SERIF); paint.setTextSize(size); width = paint.measureText(str); height = (paint.getFontMetrics().descent-paint.getFontMetrics().ascent); pinholeX = width/2; pinholeY = height/2; } /** Paint this Image into the given graphics */ public void paint(Canvas c, int x, int y){ c.drawText(str, (float)(x-pinholeX), (float)(y+pinholeY), paint); } /** Return the Width of this Image */ public int width(){ return ceil(width); } /** Return the Height of this Image */ public int height(){ return ceil(height); } }