+/**
+ *
+ */
+package jlecture.swing;
+
+import java.awt.Color;
+import java.awt.Font;
+import java.awt.FontMetrics;
+import java.awt.Graphics2D;
+import java.awt.Point;
+
+import jlecture.simulation.Alignment;
+import jlecture.simulation.ITextWidget;
+import jlecture.simulation.Model;
+import jlecture.simulation.PointRW;
+import jlecture.simulation.WidgetType;
+
+/**
+ * @author hm
+ *
+ */
+public class TextSwing extends WidgetSwing implements ITextWidget {
+ /**
+ * font size in dots.
+ */
+ private final int size;
+ /**
+ * Text which will be shown.
+ */
+ private String text;
+ /**
+ * The width of the text in pixel.
+ */
+ private int textWidth = 0;
+ /**
+ * The height of the text in pixel.
+ */
+ private int textHeight = 0;
+ /**
+ * the text will be drawn with this font.
+ */
+ Font font = null;
+ /**
+ * Helper to calculate width and height. Will be initialized when a
+ * <code>Graphics</code> is available.
+ */
+ FontMetrics fontMetrics = null;
+ Point nativeCenter = new Point(0, 0);
+ Alignment horizontalAlignment;
+ Alignment verticalAlignment;
+
+ /**
+ * Contructor.
+ *
+ * @param text
+ * the text to show
+ * @param size
+ * the font size in dots
+ * @param center
+ * the text will be centered around this point
+ * @param model
+ * the simulation model
+ */
+ public TextSwing(final String text, final int size, final PointRW center,
+ final Alignment horizontalAlignment, final Alignment verticalAlignment,
+ final Model model) {
+ super(center, model);
+ this.size = size;
+ this.text = text;
+ this.font = new Font("Helvetica", Font.PLAIN, this.size);
+ this.horizontalAlignment = horizontalAlignment;
+ this.verticalAlignment = verticalAlignment;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see jlecture.swing.WidgetSwing#calculateContour()
+ */
+ @Override
+ protected void calculateContour() {
+ // TODO Auto-generated method stub
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see jlecture.swing.WidgetSwing#draw(java.awt.Graphics2D)
+ */
+ @Override
+ public void draw(final Graphics2D graphics) {
+ if (this.font == null) {
+ this.fontMetrics = graphics.getFontMetrics(this.font);
+ this.textWidth = this.fontMetrics.stringWidth(this.text);
+ this.textHeight = this.fontMetrics.getHeight();
+ }
+ final Color safeColor = graphics.getColor();
+ graphics.setColor(this.color);
+ this.model.transform(this.center, this.nativeCenter);
+ int x, y;
+ switch (this.horizontalAlignment) {
+ case LEFT:
+ x = this.nativeCenter.x;
+ break;
+ case RIGHT:
+ x = this.nativeCenter.x - this.textWidth;
+ break;
+ default:
+ x = this.nativeCenter.x - this.textWidth / 2;
+ break;
+ }
+ switch (this.verticalAlignment) {
+ case TOP:
+ y = this.nativeCenter.y + this.textHeight;
+ break;
+ case BOTTOM:
+ y = this.nativeCenter.y;
+ break;
+ default:
+ y = this.nativeCenter.y - this.textHeight / 2;
+ break;
+ }
+ graphics.drawString(this.text, x, y);
+ graphics.setColor(safeColor);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see jlecture.simulation.IWidget#getMaxDimension()
+ */
+ @Override
+ public double getMaxDimension() {
+ // TODO Auto-generated method stub
+ return 0;
+ }
+
+ @Override
+ public String getText() {
+ return this.text;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see jlecture.simulation.IWidget#getWidgetType()
+ */
+ @Override
+ public WidgetType getWidgetType() {
+ return WidgetType.TEXT;
+ }
+
+ @Override
+ public void setText(final String text) {
+ this.text = text;
+
+ }
+
+}