net.kolls.railworld.segment
Class Signal

java.lang.Object
  extended by net.kolls.railworld.RailSegment
      extended by net.kolls.railworld.segment.Signal

public class Signal
extends RailSegment

Provides a signal to indicate whether a train should proceed. The signal is unidirectional -- it controls from begin to end only.

Author:
Steve Kollmansberger

Field Summary
static int POINT_BEGIN
          The begin segment of the signal.
static int POINT_END
          The destination segment.
 SignalProgram sp
          The SignalProgram controlling this signal.
 
Fields inherited from class net.kolls.railworld.RailSegment
dests, ec, MOUSE_NEAR, pts, RAIL_BED_WIDTH, RAIL_WIDTH, trains
 
Constructor Summary
Signal(RailSegment begin, RailSegment end)
          Create a signal.
 
Method Summary
 boolean canErase()
          Can this segment be erased? In some cases, other segments must be erased first.
 boolean click(java.awt.geom.Point2D pos, RailCanvas rc)
          Whenever a user clicks on the canvas, all dynamic segments receive this event.
 SegmentEditPoint createSEP(int ptIdx, RailSegment attach)
          Given a point index in the pts array and possibly a railsegment to attach, return a segmenteditpoint if such attachment is possible.
static RailSegment[] createSignals(RailSegment[] lines)
          Generates signals at switches and four ways in a reasonable way.
 RailSegment dest(RailSegment source)
          Each rail segment must at the very least tell us where it comes from and where it goes, currently.
 void draw(int z, java.awt.Graphics2D gc)
          Draw the segment.
 javax.swing.JPanel editPanel()
          You are guaranteed that the variable RailSegment.ec will be set prior to this call.
 void enter(Train t)
          Notification that a train is in the segment.
 java.awt.geom.Point2D getPoint(RailSegment start, double myPos)
           
 boolean isDynamic()
          Dynamic segments receive additional events while the game is running; non-dynamic segments are only asked to draw z levels 1 and 2 at draw static time.
 Distance length()
          all segments have a length can be 0 for signals, etc.
 java.lang.String mouseOver(java.awt.geom.Point2D pos)
          Whenever a user mouses over the canvas, all dynamic segments receive this event.
 void recomp()
          Called when changes are made; recompute or update any associated values.
 boolean singleton()
          Singleton segments may only have one train in them at a time.
 
Methods inherited from class net.kolls.railworld.RailSegment
carHidden, destNZ, getDest, getDests, getPoint, getPoints, nearEditPoint, pixelStep, setDest, setPoint, step, trains, update, update
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

POINT_BEGIN

public static final int POINT_BEGIN
The begin segment of the signal. The signal controls traffic from this segment.

See Also:
Constant Field Values

POINT_END

public static final int POINT_END
The destination segment.

See Also:
Constant Field Values

sp

public SignalProgram sp
The SignalProgram controlling this signal.

Constructor Detail

Signal

public Signal(RailSegment begin,
              RailSegment end)
Create a signal. Normally these are created automatically by createSignals(RailSegment[]). The signal operates in only one direction; from begin to end.

Parameters:
begin - The begin (origin) segment
end - The destination segment
Method Detail

mouseOver

public java.lang.String mouseOver(java.awt.geom.Point2D pos)
Description copied from class: RailSegment
Whenever a user mouses over the canvas, all dynamic segments receive this event. Check to see if the position is at your segment (as in click). Return a describe if applicable, otherwise null. Defaults to returning null.

Overrides:
mouseOver in class RailSegment
Parameters:
pos - The mouse position
Returns:
Description if applicable, otherwise null
See Also:
RailSegment.isDynamic()

createSignals

public static RailSegment[] createSignals(RailSegment[] lines)
Generates signals at switches and four ways in a reasonable way. Both legs of the switch must be track segments. If a switch is connected to another switch by one segment, that side will not have a signal. Signals will not be placed if a previous signal is within 300 feet.

Parameters:
lines - The original rail segments.
Returns:
New rail segments with signals added.

recomp

public void recomp()
Description copied from class: RailSegment
Called when changes are made; recompute or update any associated values.

Specified by:
recomp in class RailSegment

canErase

public boolean canErase()
Description copied from class: RailSegment
Can this segment be erased? In some cases, other segments must be erased first.

Specified by:
canErase in class RailSegment
Returns:
Indicates if the segment may be erased as-is.

click

public boolean click(java.awt.geom.Point2D pos,
                     RailCanvas rc)
Description copied from class: RailSegment
Whenever a user clicks on the canvas, all dynamic segments receive this event. Check to see if the position is at your segment (within some reasonable grace) Indicate if you performed an action or not. This tells the system to continue to try to find a segment to handle the event or not. Defaults to no action, returns false to indicate nothing happened, system should try another segment.

Overrides:
click in class RailSegment
Parameters:
pos - The click position
rc - The current canvas which contains this segment
Returns:
If an action/change was performed
See Also:
RailSegment.isDynamic()

dest

public RailSegment dest(RailSegment source)
Description copied from class: RailSegment
Each rail segment must at the very least tell us where it comes from and where it goes, currently. Indicate where you are coming from, it says where you're going. Null value means end of line. These values may be dynamic, as in a switch.

Specified by:
dest in class RailSegment
Parameters:
source - The rail segment of origin
Returns:
The destination rail segment, or null if there is none.

draw

public void draw(int z,
                 java.awt.Graphics2D gc)
Description copied from class: RailSegment
Draw the segment. Segments have several z-layers to allowed slightly overlaping pieces to still look nice. z = 1 is a static layer (lowest) z = 2 is a static layer (highest) z = 3 is reserved z = 4 is the dynamic layer. This is redrawn frequently, if requested by the segment.

Specified by:
draw in class RailSegment
Parameters:
z - The currently z-layer to draw
gc - A Graphics2D graphics context.
See Also:
RailSegment.isDynamic()

editPanel

public javax.swing.JPanel editPanel()
Description copied from class: RailSegment
You are guaranteed that the variable RailSegment.ec will be set prior to this call. So if you need to refresh the canvas or make any changes to it, you can use that variable.

Specified by:
editPanel in class RailSegment
Returns:
A JPanel to display in the sidebar to edit this segment's properties. Please keep width at 200 pixels top.

enter

public void enter(Train t)
Description copied from class: RailSegment
Notification that a train is in the segment. You may be notified arbitrarily often/many times. Check RailSegment.trains() to see if it remains. Defaults to no behavior.

Overrides:
enter in class RailSegment
Parameters:
t - The train in the segment.

getPoint

public java.awt.geom.Point2D getPoint(RailSegment start,
                                      double myPos)
Specified by:
getPoint in class RailSegment
Parameters:
start - The segment of origin
myPos - Percentage (0-1) along the segment
Returns:
A Point2D indicating the actual point that location represents

isDynamic

public boolean isDynamic()
Description copied from class: RailSegment
Dynamic segments receive additional events while the game is running; non-dynamic segments are only asked to draw z levels 1 and 2 at draw static time. Defaults to false (not dynamic). Non-dynamic segments also may not receive notifications for various events.

Overrides:
isDynamic in class RailSegment
Returns:
Indicates if the segment should receive z=4 draw events and click events.
See Also:
RailSegment.draw(int, Graphics2D), RailSegment.click(Point2D, RailCanvas)

length

public Distance length()
Description copied from class: RailSegment
all segments have a length can be 0 for signals, etc. length must be the same no mattter which 'way' you traverse the segment Distance may be a 0-foot distance, but not may be null.

Specified by:
length in class RailSegment
Returns:
The Distance of the segment.

createSEP

public SegmentEditPoint createSEP(int ptIdx,
                                  RailSegment attach)
Description copied from class: RailSegment
Given a point index in the pts array and possibly a railsegment to attach, return a segmenteditpoint if such attachment is possible. If the attach segment is null, no segment is attaching, just user editing.

Specified by:
createSEP in class RailSegment
Parameters:
ptIdx - The index into the pts array.
attach - The segment that desires to attach. (Note: do not attach the segment automatically; it will be called through the anchor method in the edit point).
Returns:
A SegmentEditPoint if possible, otherwise null.

singleton

public boolean singleton()
Description copied from class: RailSegment
Singleton segments may only have one train in them at a time. If additional trains are detected, a rail accident occurs. Defaults to false (not a singleton).

Overrides:
singleton in class RailSegment
Returns:
If the segment may only have one train on it at a time