00001 package org.math.plot.plotObjects;
00002
00003 import java.awt.*;
00004
00005 import org.math.plot.render.*;
00006
00012 public class Label implements Plotable {
00013
00014 protected double[] coord;
00015
00016 protected double[] base_offset;
00017
00018 protected String label;
00019
00020 protected Color color;
00021
00022 protected double cornerN = 0.5;
00023
00024 protected double cornerE = 0.5;
00025
00026 boolean visible = true;
00027
00028 public double angle = 0;
00029
00030 public Font font = AbstractDrawer.DEFAULT_FONT;
00031
00032
00033
00034 public Label(String l, Color col, double... c) {
00035 label = l;
00036 coord = c;
00037 color = col;
00038 }
00039
00040 public Label(String l, double... c) {
00041 this(l, AbstractDrawer.DEFAULT_COLOR, c);
00042 }
00043
00047 public Label(double... c) {
00048 this(coordToString(c), AbstractDrawer.DEFAULT_COLOR, c);
00049 }
00050
00051 public void setText(String _t) {
00052 label = _t;
00053 }
00054
00055 public String getText() {
00056 return label;
00057 }
00058
00059 public void setCoord(double... _c) {
00060 coord = _c;
00061 }
00062
00063 public void setColor(Color c) {
00064 color = c;
00065 }
00066
00067 public Color getColor() {
00068 return color;
00069 }
00070
00074 public void setCorner(double north_south, double east_west) {
00075 cornerN = north_south;
00076 cornerE = east_west;
00077 }
00078
00079 public void setVisible(boolean v) {
00080 visible = v;
00081 }
00082
00083 public boolean getVisible() {
00084 return visible;
00085 }
00086
00090
00091
00092
00093
00094
00095
00099 public void plot(AbstractDrawer draw) {
00100 if (!visible) return;
00101
00102 draw.setColor(color);
00103 draw.setFont(font);
00104 draw.setBaseOffset(base_offset);
00105 draw.setTextOffset(cornerE, cornerN);
00106 draw.setTextAngle(angle);
00107 draw.drawText(label, coord);
00108 draw.setBaseOffset(null);
00109 }
00110
00111 public void rotate(double _angle) {
00112 angle = _angle;
00113 }
00114
00115 public void setFont(Font _font) {
00116 font = _font;
00117 }
00118
00119 public static double approx(double val, int decimal) {
00120
00121
00122
00123
00124
00125
00126
00127 if (decimal < 0) {
00128 return Math.rint(val / Math.pow(10, -decimal)) * Math.pow(10, -decimal);
00129 } else {
00130 return Math.rint(val * Math.pow(10, decimal)) / Math.pow(10, decimal);
00131 }
00132
00133 }
00134
00135 public static String coordToString(double... c) {
00136 StringBuffer sb = new StringBuffer("(");
00137 for (int i = 0; i < c.length; i++)
00138 sb.append(approx(c[i], 2)).append(",");
00139
00140
00141 sb.setLength(sb.length() - 1);
00142 if (sb.length() > 0)
00143 sb.append(")");
00144
00145 return sb.toString();
00146 }
00147
00148 public Font getFont() {
00149 return font;
00150 }
00151 }