Rich Burgis
Posts: 17
Nickname: songbird
Registered: Mar, 2003
|
|
Re: Possible with two-colored line in a diagram??
|
Posted: Mar 30, 2003 7:08 PM
|
|
Sure. For example suppose that you want your line to be red to the left of the line x = 100 and green to the right.
Say further that the two points you want to connect are (75, 60 ) and (150, 35 ).
You'll need to draw two segments one ending at when x = 100 and the next going on to x = 150.
To do that you need to know the point where the color changes. You do that with the equation for a line
(y - y1) / ( x - x1) = slope
the slope of the line in question is (35 - 60)/( 150 -75) or -25/75 or -1/3. So the equation you need is (using the first point for x1 and y1)
( y - 60)/( x - 75) = -1/3. To get the point where the color changes set x = 100 or
(y-60)/(100-75) = 1/3 or
y-60 = -25/3 or y =60 - 25/3 = 51 and 2/3. (You should round the result since pixel positions are integers )
So your code in this case is
g.setColor( Color.red). g.drawLine( 75, 60, 100, 52 ); g.setColor( Color.green ); g.drawLine( 100, 52, 150, 35 );
|
|