001    /*
002     * @(#)Arrow2D.java
003     */
004    
005    import java.awt.*;
006    import javax.swing.*;
007    import java.awt.geom.*;
008    
009    /**
010     * Arrow2D.java - Desenho de uma seta usando Graphics2D.
011     *
012     * <p>
013     * Descrição:
014     * <p>
015     * ===============================================================
016     * <p>
017     * Esta classe define um objeto para desenhar setas em canvas.
018     *
019     * <p>
020     * O código original foi obtido de 
021     * <code>http://www.stat.vt.edu/~sundar/java/code/Arrow.html</code>
022     * e modificado.
023     *
024     * <p>
025     * ===============================================================
026     *
027     * @version    1.0 04/09/2004
028     * @author     Luiz Fernando Martha
029     * @author     Alonso Juvinao Carbono
030     * @author     Anderson Resende Pereira
031     * @author     Fernando Busato Ramires
032     * @author     Paôla Reginal Dalcanal
033     * @author     Ricardo Rodrigues de Araujo  
034     *
035     */
036    
037    public class Arrow2D
038    {
039      public static final int SIDE_NONE=0,
040                              SIDE_LEAD=1,
041                              SIDE_TRAIL=2,
042                              SIDE_BOTH=3,
043                              SIDE_LEAD_LEFT=4,
044                              SIDE_LEAD_RIGHT=5,
045                              SIDE_TRAIL_LEFT=6,
046                              SIDE_TRAIL_RIGHT=7;
047                              
048      public final double pi=Math.PI;
049    
050      protected Line2D.Double line;
051      protected Line2D.Double tip;
052      
053      public Arrow2D()
054      { 
055       line = new Line2D.Double( );
056       tip = new Line2D.Double( );
057      }
058    
059     /** Draw Arrow */
060      public void DrawArrow(Graphics2D g,
061                            double xi, double yi,
062                            double alfa, double length,
063                            int side,
064                            double beta, double tip_length) {
065               
066        // xi & yi     ->  Initial points of the arrow
067        // alfa        ->  Angle of the arrow in relation to the horizontal line
068        // length      ->  Length of the arrow
069        // side        ->  Side of the arrow
070        // beta        ->  Angle of opening of the arrow
071        // tip_length  ->  Length of the tip of the arrow
072        
073        try {
074          if (length < 0) { 
075            alfa+=pi;
076            length*=-1;
077          }
078          
079          double xf;
080          double yf;
081    
082          xf = xi + length*Math.cos(alfa);
083          yf = yi + length*Math.sin(alfa);
084          
085          line.setLine( xi, yi, xf, yf );
086          
087          g.draw( line );
088    
089          switch (side) {
090            case SIDE_NONE :
091              break;
092            case SIDE_LEAD :
093              DrawTipArrow( g, xf, yf, alfa+(3*pi/2.0)-beta, tip_length );
094              DrawTipArrow( g, xf, yf, alfa+(pi/2.0)+beta, tip_length );
095              break;
096            case SIDE_TRAIL :
097              DrawTipArrow( g, xi, yi, alfa-beta, tip_length );
098              DrawTipArrow( g, xi, yi, alfa+beta, tip_length );
099              break;
100            case SIDE_BOTH :
101              DrawTipArrow( g, xi, yi, alfa-beta, tip_length );
102              DrawTipArrow( g, xi, yi, alfa+beta, tip_length );
103              DrawTipArrow( g, xf, yf, alfa+(3*pi/2.0)-beta, tip_length );
104              DrawTipArrow( g, xf, yf, alfa+(pi/2.0)+beta, tip_length );
105              break;
106            case SIDE_LEAD_LEFT :
107              DrawTipArrow( g, xf, yf, alfa+(pi/2.0)+beta, tip_length );
108              break;
109            case SIDE_LEAD_RIGHT :
110              DrawTipArrow( g, xf, yf, alfa+(3*pi/2.0)-beta, tip_length );
111              break;
112            case SIDE_TRAIL_LEFT :
113              DrawTipArrow( g, xi, yi, alfa+beta, tip_length );
114              break;
115            case SIDE_TRAIL_RIGHT :
116              DrawTipArrow( g, xi, yi, alfa-beta, tip_length );
117              break;
118            default:
119              throw new IllegalArgumentException();
120          }
121        }
122        catch (IllegalArgumentException iae) {
123          System.out.println("Invalid value for variable side.");
124        }
125      }
126      
127      protected void DrawTipArrow(Graphics2D g,
128                                  double x1, double y1,
129                                  double beta, double tip_length)
130      {
131       double x2;
132       double y2;
133    
134       x2 = x1 + tip_length*Math.cos(beta);
135       y2 = y1 + tip_length*Math.sin(beta);
136    
137       tip.setLine( x1, y1, x2, y2 );
138    
139       g.draw( tip );
140      }
141    }
142