// Transition.java
// Transition class to store a transition 
//   store the state to which it goes, and the input symbol.

import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;

/** Transition which originates from a particular 
  * <a href="State.html">State</a>.
 *  @author <a href="mailto:bergmann@rowan.edu"> Seth Bergmann </a>
 *  @author <a href="mailto:gspyo@jersey.net"> Greg Safko </a>
 */
public class Transition extends JComponent
{

/** The states to and from  which this transition is directed.
 */
    public State toState, fromState;		

/** The label (input symbol) on this transition
 */
   public TransLabel lbl;

/** The input symbol of this transition.
 */
    public char inp;			// Input symbol
    private static int lblWidth=18;
    private static int lblHeight=20;

/** Create a transition
 * @param to The State to which this transition is directed.
 * @param i The input symbol for this transition.
 */
public Transition (State to, State from, char i)
{	toState = to;
	fromState = from;
	inp = i;

   String s = (new Character (inp)).toString();
   lbl = new TransLabel (s,this);
}

/** For debugging purposes
 */
public String toString()
{  return "Transition, toState=" + toState + ", inp=" + inp;
}

/** Paint this Transition as an Arrow with a label.  If the transition is to
 *  itself, draw it as an arc.
 */
public void paint (Graphics g)
{  super.paint (g);
   Graphics2D graphics2D = (Graphics2D) g;

   Arrow arrow;		// arrow used to draw this transition
   double arcArrowX, arcArrowY;
   if (toState == fromState)		// arc arrow
     {	arcArrowX = toState.locX+0.5*State.r;
	arcArrowY = toState.locY-0.5*State.r;
	graphics2D.draw (new Arc2D.Double (arcArrowX, arcArrowY, 
		(double) State.r, (double) State.r,
		-0.01*State.r, 180+0.01*State.r,Arc2D.OPEN));
	lbl.setLocation (toState.locX+2*State.r, toState.locY-4*State.r);
     }
   else
     { 	arrow = new Arrow (fromState.locX+State.r, fromState.locY+State.r, 
			toState.locX+State.r, toState.locY+State.r);
   	arrow.paint(g);
     }
}


}
