1 /*
2  *  (C) 2001 by Argonne National Laboratory
3  *      See COPYRIGHT in top-level directory.
4  */
5 
6 /*
7  *  @author  Anthony Chan
8  */
9 
10 package base.topology;
11 
12 import java.awt.Graphics2D;
13 import java.awt.Insets;
14 import java.awt.Color;
15 import java.awt.Point;
16 import base.drawable.CoordPixelXform;
17 import base.drawable.DrawnBox;
18 
19 public class State
20 {
21     private static StateBorder BorderStyle  = StateBorder.WHITE_RAISED_BORDER;
22 
setBorderStyle( final StateBorder state_border )23     public static void setBorderStyle( final StateBorder state_border )
24     {
25         BorderStyle = state_border;
26     }
27 
28     /*
29         Draw a Rectangle between left-upper vertex (start_time, start_ypos)
30         and right-lower vertex (final_time, final_ypos)
31         Assume caller guarantees the order of timestamps and ypos, such that
32         start_time <= final_time  and  start_ypos <= final_ypos.
33     */
drawForward( Graphics2D g, Color color, Insets insets, CoordPixelXform coord_xform, DrawnBox last_drawn_pos, double start_time, float start_ypos, double final_time, float final_ypos )34     private static int  drawForward( Graphics2D g, Color color, Insets insets,
35                                      CoordPixelXform    coord_xform,
36                                      DrawnBox           last_drawn_pos,
37                                      double start_time, float start_ypos,
38                                      double final_time, float final_ypos )
39     {
40         int      iStart, jStart, iFinal, jFinal;
41         iStart   = coord_xform.convertTimeToPixel( start_time );
42         iFinal   = coord_xform.convertTimeToPixel( final_time );
43 
44         /* Determine if State should be drawn */
45         if ( last_drawn_pos.coversState( iStart, iFinal ) )
46             return 0; // too small to be drawn in previously drawn location
47         last_drawn_pos.set( iStart, iFinal );
48 
49         jStart   = coord_xform.convertRowToPixel( start_ypos );
50         jFinal   = coord_xform.convertRowToPixel( final_ypos );
51 
52         if ( insets != null ) {
53             iStart += insets.left;
54             iFinal -= insets.right;
55             jStart += insets.top;
56             jFinal -= insets.bottom;
57         }
58 
59         boolean  isStartVtxInImg, isFinalVtxInImg;
60         isStartVtxInImg = ( iStart >= 0 ) ;
61         isFinalVtxInImg = ( iFinal <  coord_xform.getPixelWidth() );
62 
63         int iHead, iTail, jHead, jTail;
64         // jHead = slope * ( iHead - iStart ) + jStart
65         if ( isStartVtxInImg )
66             iHead = iStart;
67         else
68             iHead = 0;
69         jHead    = jStart;
70 
71         // jTail = slope * ( iTail - iFinal ) + jFinal
72         if ( isFinalVtxInImg )
73             iTail = iFinal;
74         else
75             iTail = coord_xform.getPixelWidth() - 1;
76         jTail    = jFinal;
77 
78         // Fill the color of the rectangle
79         g.setColor( color );
80         g.fillRect( iHead, jHead, iTail-iHead+1, jTail-jHead+1 );
81 
82         BorderStyle.paintStateBorder( g, color,
83                                       iHead, jHead, isStartVtxInImg,
84                                       iTail, jTail, isFinalVtxInImg );
85         return 1;
86     }
87 
88     /*
89         Check if a point in pixel coordinate is in a Rectangle
90         specified between left-upper vertex (start_time, start_ypos)
91         and right-lower vertex (final_time, final_ypos)
92         Assume caller guarantees the order of timestamps and ypos, such that
93         start_time <= final_time  and  start_ypos <= final_ypos
94     */
isPixelIn( CoordPixelXform coord_xform, Point pt, double start_time, float start_ypos, double final_time, float final_ypos )95     private static boolean isPixelIn( CoordPixelXform coord_xform, Point pt,
96                                       double start_time, float start_ypos,
97                                       double final_time, float final_ypos )
98     {
99         int      iStart, jStart, iFinal, jFinal;
100         int      pt_x, pt_y;
101 
102         pt_y     = pt.y;
103 
104         jStart   = coord_xform.convertRowToPixel( start_ypos );
105         if ( pt_y < jStart  )
106             return false;
107 
108         jFinal   = coord_xform.convertRowToPixel( final_ypos );
109         if ( pt_y > jFinal )
110             return false;
111 
112         pt_x     = pt.x;
113 
114         iStart   = coord_xform.convertTimeToPixel( start_time );
115         if ( pt_x < iStart )
116             return false;
117 
118         iFinal   = coord_xform.convertTimeToPixel( final_time );
119         if ( pt_x > iFinal )
120             return false;
121 
122         return true;
123     }
124 
125 
draw( Graphics2D g, Color color, Insets insets, CoordPixelXform coord_xform, DrawnBox last_drawn_pos, double start_time, float start_ypos, double final_time, float final_ypos )126     public static int  draw( Graphics2D g, Color color, Insets insets,
127                              CoordPixelXform    coord_xform,
128                              DrawnBox           last_drawn_pos,
129                              double start_time, float start_ypos,
130                              double final_time, float final_ypos )
131     {
132          if ( start_time < final_time ) {
133              if ( start_ypos < final_ypos )
134                  return drawForward( g, color, insets,
135                                      coord_xform, last_drawn_pos,
136                                      start_time, start_ypos,
137                                      final_time, final_ypos );
138              else
139                  return drawForward( g, color, insets,
140                                      coord_xform, last_drawn_pos,
141                                      start_time, final_ypos,
142                                      final_time, start_ypos );
143          }
144          else {
145              if ( start_ypos < final_ypos )
146                  return drawForward( g, color, insets,
147                                      coord_xform, last_drawn_pos,
148                                      final_time, start_ypos,
149                                      start_time, final_ypos );
150              else
151                  return drawForward( g, color, insets,
152                                      coord_xform, last_drawn_pos,
153                                      final_time, final_ypos,
154                                      start_time, start_ypos );
155          }
156     }
157 
containsPixel( CoordPixelXform coord_xform, Point pt, double start_time, float start_ypos, double final_time, float final_ypos )158     public static boolean containsPixel( CoordPixelXform coord_xform, Point pt,
159                                          double start_time, float start_ypos,
160                                          double final_time, float final_ypos )
161     {
162          if ( start_time < final_time ) {
163              if ( start_ypos < final_ypos )
164                  return isPixelIn( coord_xform, pt,
165                                    start_time, start_ypos,
166                                    final_time, final_ypos );
167              else
168                  return isPixelIn( coord_xform, pt,
169                                    start_time, final_ypos,
170                                    final_time, start_ypos );
171          }
172          else {
173              if ( start_ypos < final_ypos )
174                  return isPixelIn( coord_xform, pt,
175                                    final_time, start_ypos,
176                                    start_time, final_ypos );
177              else
178                  return isPixelIn( coord_xform, pt,
179                                    final_time, final_ypos,
180                                    start_time, start_ypos );
181          }
182     }
183 }
184