package commons.graph.view;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Panel;
/**
 * Graphics transformer utility. Transforms the 'euclidian' coordinates (center 0/0) to the screen
 * and respects the current screen's size. Additionally a resize factor can be set to fit the
 * element to the screen.
 * @author ks
 */
public class GTU
{

int         width, height;
int         demiwidth, demiheight;
FontMetrics fontMetrics;
double      resizefactor;

public GTU(Graphics g, Panel p)
{
   this(g, p, 1D);
}

public GTU(Graphics g, Panel p, double resizefactor)
{
   width = p.getWidth();
   height = p.getHeight();
   demiwidth = width / 2;
   demiheight = height / 2;
   fontMetrics = g.getFontMetrics();
   this.resizefactor = resizefactor;
}

/**
 * Input euclidian, output screen metrics.
 */
public int x (int x)
{
   return (int) (((double) x + (double) demiwidth) * resizefactor);
}

/**
 * Input euclidian, output screen metrics.
 */
public int y (int y)
{
   return (int) (((double) y + (double) demiheight) * resizefactor);
}

/**
 * Center a text horizontally using an x start point.
 * @param text
 * @param x
 * @return
 */
public int tx (String text, int x)
{
   return x - fontMetrics.stringWidth(text) / 2;
}

/**
 * Center a text vertically using a y start point.
 * @param text
 * @param y
 * @return
 */
public int ty (String text, int y)
{
   return y + fontMetrics.getDescent() + 1;
}
}
