| Text.java |
1 package fi.jyu.mit.graphics;
2
3 import java.awt.*;
4
5 /**
6 * Luokka tekstille
7 * @author Markus Kivioja
8 *
9 */
10 public class Text extends BasicShape {
11
12 String text;
13 RPoint leftBottom;
14 double tx = 1.0;
15 double ty = 1.0;
16
17 /**
18 * Luo uuden tekstin jonka z-koordinaatti on 0
19 * @param text piirrettävä teksti
20 * @param x vasemman alakulman x-koordinaatti
21 * @param y vasemman alakulman y-koordinaatti
22 */
23 public Text(String text, double x, double y) {
24 super();
25 this.text = text;
26 this.leftBottom = new RPoint(x, y);
27 }
28
29 /**
30 * Asetetaan tekstin suhteellinen siirto
31 * @param sx -1 = oikea reuna, 0 = keskitys, 1 = vasen reuna
32 * @param sy -1 = ylänurkka, 0 = keskitys, 1 = alanurkka
33 */
34 public void setTranslate(double sx,double sy) {
35 tx = sx;
36 ty = sy;
37 }
38
39
40 /**
41 * Luo uuden tekstin
42 * @param text piirrettävä teksti
43 * @param x vasemman alakulman x-koordinaatti
44 * @param y vasemman alakulman y-koordinaatti
45 * @param z vasemman alakulman z-koordinaatti
46 */
47 public Text(String text, double x, double y, double z) {
48 super();
49 this.text = text;
50 this.leftBottom = new RPoint(x, y, z);
51 }
52
53 /**
54 * Piirretään teksti
55 * @see fi.jyu.mit.graphics.BasicShape#drawShape(java.awt.Graphics, fi.jyu.mit.graphics.Matrix)
56 */
57 @Override
58 protected void drawShape(Graphics g, Matrix a) {
59 FontMetrics fm = g.getFontMetrics();
60 int w = fm.stringWidth(text);
61 int h = fm.getAscent();
62 SPoint sp = a.transform(this.leftBottom);
63 g.drawString(this.text, (int)(sp.getX()+w/2.0*(tx-1)), (int)(sp.getY()-h/2.0*(ty-1)));
64 }
65 }
66