JavaFX
Advertisement

Главная | Описание языка | FXD | API | Примеры | Инструменты Разработки | Новости | Ресурсы | Форум


JavaFX Электронный Симулятор[]

Electronic simulator off Electronic simulator on

Описание[]

Данная программа позволяет описать на языке JavaFX Script электрическую схему


Код программы[]

import javafx.ext.swing.*;
import javafx.animation.*;
import javafx.scene.input.*;
import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.paint.*;
import javafx.scene.shape.*;
import java.lang.System;


function abs(x: Number):Number{
    return if ( x < 0 ) then -x else x;
}

function min(x: Number, y: Number):Number{
    return if ( x < y ) then x else y;
}

//  ============  XY  ====================//

class XY{
    var x: Number = 0; // on replace{ System.out.println("[xy] x: {x}")};
    var y: Number = 0; // on replace{ System.out.println("[xy] y: {y}")};
}

//  ============  Pin  ====================//

class Pin extends CustomNode{
    var pos: XY;
    var level: Number;

    override function create():Node{
      return Group{
        content: [ Circle{
            centerX: bind pos.x
            centerY: bind pos.y
            radius: 4
            fill: Color.BLUE
        }]
      };
    }

}


//  ============  ElectroScheme  ====================//

class ElectroScheme{

    var components: ElectronicComponent[];

    public function simulate(){
      for(comp in components){
        comp.simulate();
      }
    }

}

//  ============  ElectronicComponent  ====================//

abstract class ElectronicComponent extends CustomNode{

    var name: String;

    var pos: XY;
    var compWidth:  Number = 20.0;
    var compHeight: Number = 15.0;

    var pin1: Pin;
    var pin2: Pin;

    abstract function simulate():Void;

    function switchLevels(){
      var level = pin1.level;
      pin1.level = pin2.level;
      pin2.level = level;
    }

}

//  ============  Wire ====================//


class Wire extends ElectronicComponent{
    init {
        name = "Wire";
        pin1 = Pin{};
        pin2 = Pin{};
    }

    override function create():Node{
      return Group{
        content: [ Line{
            startX: bind pin1.pos.x
            startY: bind pin1.pos.y
            endX: bind pin2.pos.x
            endY: bind pin2.pos.y
            stroke: Color.GREEN
        }]
      };
    }

   override function simulate():Void{
      switchLevels();
   }


}



//  ============  Lamp ====================//


class Lamp extends ElectronicComponent{

    var switchOn: Boolean = false;
    override var pin1 on replace oldPin=newPin {
       pin1.pos = XY{x: bind pos.x - compWidth / 2  y: bind pos.y + 2 * compHeight -5};
    }

    override var pin2 on replace oldPin=newPin {
      pin2.pos = XY{x: bind pos.x + compWidth / 2  y: bind pos.y + 2 * compHeight - 5};
    }

    init {
        name = "Lamp";
    }

    override function simulate():Void{
      if( abs( pin1.level - pin2.level) > 5 ){
        switchOn = true;
      }else{
        switchOn = false;
      }
     switchLevels();
    }

    override function create(): Node{
      var r = min(compWidth, compHeight);
      var h = r / 2.0;

      return Group{
        content: [Rectangle{
            x: bind pos.x - h
            y: bind pos.y
            width: r
            height: 2* r
            fill: Color.SILVER
        }, Circle{
            centerX: bind pos.x
            centerY: bind pos.y
            radius: r
            fill: bind if (switchOn) then Color.RED else Color.GREY
        }, pin1, pin2 ]
        onMouseDragged: function (e: MouseEvent){
            pos.x = e.x; 
            pos.y = e.y; 
         }
      };
    }


}



//  ============  Battery ====================//


class Battery extends  ElectronicComponent {

    override var pin1 on replace oldPin=newPin {
       pin1.pos = XY {x: bind pos.x + compWidth - 5  y: bind pos.y - compHeight};
    }

    override var pin2 on replace oldPin=newPin {
      pin2.pos = XY{x: bind pos.x - compWidth + 5  y: bind pos.y - compHeight};
    }

    init {
        name = "Battery";
    }


    override function simulate():Void{
      pin1.level =  3.0;
      pin2.level = -3.0;
    }

    override function create():Node{
      var w = compWidth;
      var h = compHeight;

      return Group{
        content: [Rectangle{
            x: bind pos.x - w
            y: bind pos.y - h
            width:  2 * w
            height: 2 * h
            fill: Color.CHOCOLATE
            stroke: Color.BROWN
        },Line{
            startX: bind pos.x + w - 5 + 3
            startY: bind pos.y - h + 10
            endX: bind pos.x + w - 5 - 3
            endY: bind pos.y - h  + 10

            stroke: Color.DARKBLUE
            strokeWidth: 2
        },Line{
            startX: bind pos.x - w + 5 + 3
            startY: bind pos.y - h + 10
            endX: bind pos.x - w  + 5 - 3
            endY: bind pos.y - h  + 10

            stroke: Color.DARKBLUE
            strokeWidth: 2
        },Line{
            startX: bind pos.x - w + 5
            startY: bind pos.y - h + 10 + 3
            endX: bind pos.x - w  + 5
            endY: bind pos.y - h  + 10 -3

            stroke: Color.DARKBLUE
            strokeWidth: 2
        },pin1, pin2]
         onMouseDragged: function(e: MouseEvent){
            pos.x = e.x; 
            pos.y = e.y; 
         }
      };
    }

}


//  ============  Switch ====================//




class Switch extends ElectronicComponent{
    var switchOn: Boolean = false;
    override var pin1 on replace oldPin=newPin {
        pin1.pos = XY{x: bind pos.x + compWidth   y: bind pos.y };
    }
    override var pin2 on replace oldPin=newPin {
        pin2.pos = XY{x: bind pos.x - compWidth   y: bind pos.y };
    }

    init {
        name = "Switch";
    }

    override function simulate():Void{
      if(switchOn){
        switchLevels();
      }
    }

    override function create(): Node{
      return Group{
        content:  [ Circle{
            centerX: bind pos.x
            centerY: bind pos.y
            radius:  min(compWidth, compHeight)
            fill: Color.ORANGE
            stroke: Color.BROWN
            onMouseClicked: function(e){
                switchOn = not switchOn;
            }
            onMouseDragged: function(e:MouseEvent){
                pos.x = e.x; 
                pos.y = e.y; 
            }
        }, Group{
            content: bind if (switchOn) then Line{
              startX: bind pos.x - compWidth
              startY: bind pos.y
              endX: bind pos.x + compWidth
              endY: bind pos.y
              stroke: Color.BROWN
              strokeWidth: 2
          } else Line{
              startX: bind pos.x - compWidth / 1.7
              startY: bind pos.y + compHeight /1.7
              endX: bind pos.x + compWidth / 1.7
              endY: bind pos.y - compHeight /1.7
              stroke: Color.BROWN
              strokeWidth: 2
          }
        } ,pin1, pin2]

      };
    }

}

var scheme = ElectroScheme{
    var wire1 = Wire{}
    var wire2 = Wire{}
    var wire3 = Wire{}

    components:[
    Lamp{
        pos: XY{ x: 160  y: 25}
        pin1: wire1.pin2
        pin2: wire3.pin1
    },
    Battery{
        pos: XY{ x: 100  y: 120}
        pin1: wire2.pin2
        pin2: wire1.pin1
    },Switch{
        pos: XY{ x: 180  y: 105}
        pin1: wire3.pin2
        pin2: wire2.pin1

    },

    wire1, wire2, wire3 ]
};


var timeline = Timeline {
    keyFrames:  KeyFrame {
        time: 0.02s
        action: function() { scheme.simulate(); }
    }
    repeatCount: Timeline.INDEFINITE
}

timeline.play();


Stage {
    title: "Simple Electro Simulator"
    width: 300
    height: 200
    scene: Scene{
        content: bind [ scheme.components ]
    }
}

Advertisement