Package net.sf.mrailsim.signals

Source Code of net.sf.mrailsim.signals.PreSignal

/************************************************************************************

    MRailSim - a model railway simulation program - http://mrailsim.sourceforge.net/
    Copyright (C) 2004,2007  Bernd Arnold

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

   
************************************************************************************/


package net.sf.mrailsim.signals;

import java.awt.Color;
import java.awt.Graphics;

import net.sf.mrailsim.rails.Node;
import net.sf.mrailsim.rails.Track;
import net.sf.mrailsim.rails.TrackWalker;

public class PreSignal extends Signal {

  private MainSignal mainSignal = null;
  private int distanceToMainSignal;
 
 
  public PreSignal( long id ) {
    super( id );
  }

  public MainSignal getMainSignal() {
    return mainSignal;
  }

  public void setMainSignal( MainSignal mainSignal ) {
    this.mainSignal = mainSignal;
    mainSignal.registerPreSignal( this );
  }

  public void updateFromMain( SignalDisplay display ) {
    this.display = display;
  }

  public void draw( Graphics g ) {
    if ( coordinates != null ) {
      g.setColor( Color.GRAY );
      g.fillRect( coordinates.getX(), coordinates.getY(), 8, 8 );
     
      if ( display == SignalDisplay.GREEN ) {
        g.setColor( Color.GREEN );
      } else if ( display == SignalDisplay.RED ) {
        g.setColor( Color.RED );
      } else {
        g.setColor( Color.BLACK );
      }

      g.fillOval( coordinates.getX() + 2, coordinates.getY() + 2, 4, 4 );
    }
  }
 
  public void setBound( Track boundTrack, Node boundNode ) {
    super.setBound( boundTrack, boundNode );

    boundTrack.addPreSignal( this );
   
    TrackWalker walker = new TrackWalker();
    walker.startAt( boundTrack, boundNode );
    walker.walkTo( mainSignal.getBoundTrack() );
   
    distanceToMainSignal = walker.getDistanceWalked();
  }

  public int getDistanceToMainSignal() {
    return distanceToMainSignal;
  }

}
TOP

Related Classes of net.sf.mrailsim.signals.PreSignal

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.