Package net.sf.mrailsim.rails

Source Code of net.sf.mrailsim.rails.TrackSwitchX

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

    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.rails;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

import net.sf.mrailsim.shared.Coordinates;

public class TrackSwitchX extends Track {

  /**
   * Class constructor; initializes a new crossing switch. The parameters are
   * passed to the super constructor.
   * @param id        The unique track id
   * @param length    The length of this track
   * @param junction  The junctions
   * @see  Junction
   */
  public TrackSwitchX( long id, int length, Junction junction ) {
    super( id, length, junction );
  }

  /**
   * Draws this track into the specified graphics context.
   * @param g  The graphics context
   */
  public void draw( Graphics g ) {
    Coordinates coord1 = junction.getPosition( 0 ).getNodeConnectorList().get( 0 ).getNode( 0 ).getCoordinates();
    Coordinates coord2 = junction.getPosition( 0 ).getNodeConnectorList().get( 0 ).getNode( 1 ).getCoordinates();
    Coordinates coord3 = junction.getPosition( 0 ).getNodeConnectorList().get( 1 ).getNode( 0 ).getCoordinates();
    Coordinates coord4 = junction.getPosition( 0 ).getNodeConnectorList().get( 1 ).getNode( 1 ).getCoordinates();
   
    // Draw both track elements
    if ( coord1 != null && coord2 != null && coord3 != null && coord4 != null ) {
      Graphics2D g2 = (Graphics2D) g;

      g.setColor( Color.BLACK );
      g2.setStroke( new BasicStroke( 8 ) );

      g2.drawLine( coord1.getX(), coord1.getY(), coord2.getX(), coord2.getY() );
      g2.drawLine( coord3.getX(), coord3.getY(), coord4.getX(), coord4.getY() );
      g2.drawLine( coord1.getX(), coord1.getY(), coord3.getX(), coord3.getY() );
      g2.drawLine( coord2.getX(), coord2.getY(), coord4.getX(), coord4.getY() );
    }
   
    // Draw current position
    Coordinates currentcoord1 = junction.getCurrentPosition().getNodeConnectorList().get( 0 ).getNode( 0 ).getCoordinates();
    Coordinates currentcoord2 = junction.getCurrentPosition().getNodeConnectorList().get( 0 ).getNode( 1 ).getCoordinates();
    Coordinates currentcoord3 = junction.getCurrentPosition().getNodeConnectorList().get( 1 ).getNode( 0 ).getCoordinates();
    Coordinates currentcoord4 = junction.getCurrentPosition().getNodeConnectorList().get( 1 ).getNode( 1 ).getCoordinates();

    if ( currentcoord1 != null && currentcoord2 != null ) {
      Graphics2D g2 = (Graphics2D) g;

      float[] dashs1 = { 2.0f, 5.0f };
      g.setColor( Color.LIGHT_GRAY );
      g2.setStroke( new BasicStroke( 3.0f, BasicStroke.CAP_BUTT,
          BasicStroke.JOIN_MITER, 1.0f, dashs1, 0.0f ) );
      g2.drawLine( currentcoord1.getX(), currentcoord1.getY(), currentcoord2.getX(), currentcoord2.getY() );
      g2.drawLine( currentcoord3.getX(), currentcoord3.getY(), currentcoord4.getX(), currentcoord4.getY() );

      if ( isOccupied() ) {
        float[] dashs2 = { 20.0f, 15.0f };
        g.setColor( Color.RED );
        g2.setStroke( new BasicStroke( 3.0f, BasicStroke.CAP_BUTT,
            BasicStroke.JOIN_MITER, 1.0f, dashs2, 0.0f ) );
        g2.drawLine( currentcoord1.getX(), currentcoord1.getY(), currentcoord2.getX(), currentcoord2.getY() );
        g2.drawLine( currentcoord3.getX(), currentcoord3.getY(), currentcoord4.getX(), currentcoord4.getY() );
      }
    }

  }

}
TOP

Related Classes of net.sf.mrailsim.rails.TrackSwitchX

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.