JavaFX
Advertisement

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


Планетарная система[]

Planetary system


Класс Planet[]

class Planet {
    package var name: String;

    package var mass : Number;
    package var radius: Number;

    package var color: Color;

    package var coordinate: Number[] = [0.0,0.0];
    package var velocity : Number[] = [0.0,0.0];
    package var acceleration : Number[] = [0.0,0.0];

}

Класс PlanetarySystem[]

class PlanetarySystem {

    public var dimension: Integer = 2;
    public var dt: Number = 60* 60 * 24 * 2;
    public var G: Number = 6.67e-11;
    public var planets:  Planet[];

  public function run():Void{

    for(p1 in planets){

        for(i in [0..dimension-1]){
            p1.acceleration[i] = 0.0;
        }

        for(p2 in planets[p| p!=p1]){

            var k = G * p2.mass;

            var deltaR:Number[] = [0.0,0.0];

            for(i in [0..dimension-1]){
                deltaR[i] = p2.coordinate[i] - p1.coordinate[i];
            }

            var squareR = 0.0;
            for(i in [0..dimension-1]){
                squareR += square(deltaR[i]);
            }

            for(i in [0..dimension-1]){
                p1.acceleration[i] += k * deltaR[i] / (squareR * Math.sqrt(squareR));
            }

        }

    }

    for(p in planets){
        for(i in [0..dimension-1]){
            p.velocity[i] += p.acceleration[i] * dt;
            p.coordinate[i] += p.velocity[i] * dt;
        }

    }
  }

}

Класс Scale[]

public class Scale{
    attribute radiusScale: Number;
    attribute coordinateScale: Number;
}


Класс PlanetarySystemShape[]

class PlanetarySystemShape extends CustomNode{

    package var scale: Scale;
    package var planetarySystem: PlanetarySystem;

    override function create():Node{
      Group{
        content: [
         Group{
            content: for(i in  [1..15])
            Circle{
                centerX: Math.random() * 300 - 150
                centerY: Math.random() * 300 - 150
                radius: 2
                fill: Color.WHITE
            }
         },
          Group{
            content: bind for(planet in planetarySystem.planets)
            Group{
                content: [
                Circle{
                    centerX: bind planet.coordinate[0] * scale.coordinateScale
                    centerY: bind planet.coordinate[1] * scale.coordinateScale
                    radius: planet.radius * scale.radiusScale
                    fill: planet.color
                },
                Text{
                    content: planet.name
                    font: Font {name: "BOLD" size: 16}
                    x: bind planet.coordinate[0] * scale.coordinateScale + 15
                    y: bind planet.coordinate[1] * scale.coordinateScale - 15
                    strokeWidth: 3
                    fill: Color.WHITE
                }
                ]
            }
        }]

    };
  }

}

Определение Солнечной системы[]

var solarSystem = PlanetarySystem{
    planets: [
    Planet{
        name: "Sun"
        mass : 2e30
        color: Color.YELLOW
        radius:   14000
        coordinate : [ 0 , 0 ]
        velocity : [ 0, 0 ]
    },Planet{
        name: "Mercury"
        mass : 3.3e23
        color: Color.BLUE
        radius: 2400
        coordinate : [ -57e9 ,  0]
        velocity : [ 0, -47e3 ]
    },
    Planet{
        name: "Venus"
        mass : 4.8e24
        color: Color.PINK
        radius: 6000
        coordinate : [ 108e9 ,  0]
        velocity : [ 0, 35e3 ]
    },
    Planet{
        name: "Earth"
        mass : 6e24
        color: Color.GREEN
        radius: 6300
        coordinate : [ 0 , -150e9 ]
        velocity : [ 30e3, 0 ]
    },Planet{
        name: "Mars"
        mass : 6.4e23
        color: Color.RED
        radius: 3400
        coordinate : [ 0 , 228e9 ]
        velocity : [ -24e3, 0 ]
    }
    ]
};

Анимация[]

Timeline {
    repeatCount: Timeline.INDEFINITE
    keyFrames:  KeyFrame {
        time: 0.02s
        action: function() { solarSystem.run(); }
    }
}.play();

Основное окно приложения[]


var w = 600;
var h = 400;

Stage {
    title: "Planetary System"
    width: w
    height: h
    scene: Scene{
        content: Group{
           content: PlanetarySystemShape{
                transforms: bind [Transform.translate(w/2, h/2)]
                scale: Scale{ coordinateScale:  100.0/150e9 radiusScale: 20.0/14000.0}
                planetarySystem: solarSystem
            }
        }
        fill: Color.BLACK
    }
}

Полный пример приложения[]


import javafx.stage.*;

import javafx.scene.*;
import javafx.scene.text.*;
import javafx.scene.paint.*;
import javafx.scene.shape.*;
import javafx.scene.transform.*;

import javafx.animation.*;

import java.lang.Math;
import java.lang.System;


function square(x: Number): Number{
    return x * x;
}

class Scale{
    package var radiusScale: Number;
    package var coordinateScale: Number;
}


class Planet {
    package var name: String;

    package var mass : Number;
    package var radius: Number;

    package var color: Color;

    package var coordinate: Number[] = [0.0,0.0];
    package var velocity : Number[] = [0.0,0.0];
    package var acceleration : Number[] = [0.0,0.0];

}


class PlanetarySystem {

    public var dimension: Integer = 2;
    public var dt: Number = 60* 60 * 24 * 2;
    public var G: Number = 6.67e-11;
    public var planets:  Planet[];

  public function run():Void{

    for(p1 in planets){

        for(i in [0..dimension-1]){
            p1.acceleration[i] = 0.0;
        }

        for(p2 in planets[p| p!=p1]){

            var k = G * p2.mass;

            var deltaR:Number[] = [0.0,0.0];

            for(i in [0..dimension-1]){
                deltaR[i] = p2.coordinate[i] - p1.coordinate[i];
            }

            var squareR = 0.0;
            for(i in [0..dimension-1]){
                squareR += square(deltaR[i]);
            }

            for(i in [0..dimension-1]){
                p1.acceleration[i] += k * deltaR[i] / (squareR * Math.sqrt(squareR));
            }

        }

    }

    for(p in planets){
        for(i in [0..dimension-1]){
            p.velocity[i] += p.acceleration[i] * dt;
            p.coordinate[i] += p.velocity[i] * dt;
        }

    }
  }

}

class PlanetarySystemShape extends CustomNode{

    package var scale: Scale;
    package var planetarySystem: PlanetarySystem;

    override function create():Node{
      Group{
        content: [
         Group{
            content: for(i in  [1..15])
            Circle{
                centerX: Math.random() * 300 - 150
                centerY: Math.random() * 300 - 150
                radius: 2
                fill: Color.WHITE
            }
         },
          Group{
            content: bind for(planet in planetarySystem.planets)
            Group{
                content: [
                Circle{
                    centerX: bind planet.coordinate[0] * scale.coordinateScale
                    centerY: bind planet.coordinate[1] * scale.coordinateScale
                    radius: planet.radius * scale.radiusScale
                    fill: planet.color
                },
                Text{
                    content: planet.name
                    font: Font {name: "BOLD" size: 16}
                    x: bind planet.coordinate[0] * scale.coordinateScale + 15
                    y: bind planet.coordinate[1] * scale.coordinateScale - 15
                    strokeWidth: 3
                    fill: Color.WHITE
                }
                ]
            }
        }]

    };
  }

}



var solarSystem = PlanetarySystem{
    planets: [
    Planet{
        name: "Sun"
        mass : 2e30
        color: Color.YELLOW
        radius:   14000
        coordinate : [ 0 , 0 ]
        velocity : [ 0, 0 ]
    },Planet{
        name: "Mercury"
        mass : 3.3e23
        color: Color.BLUE
        radius: 2400
        coordinate : [ -57e9 ,  0]
        velocity : [ 0, -47e3 ]
    },
    Planet{
        name: "Venus"
        mass : 4.8e24
        color: Color.PINK
        radius: 6000
        coordinate : [ 108e9 ,  0]
        velocity : [ 0, 35e3 ]
    },
    Planet{
        name: "Earth"
        mass : 6e24
        color: Color.GREEN
        radius: 6300
        coordinate : [ 0 , -150e9 ]
        velocity : [ 30e3, 0 ]
    },Planet{
        name: "Mars"
        mass : 6.4e23
        color: Color.RED
        radius: 3400
        coordinate : [ 0 , 228e9 ]
        velocity : [ -24e3, 0 ]
    }
    ]
};


Timeline {
    repeatCount: Timeline.INDEFINITE
    keyFrames:  KeyFrame {
        time: 0.02s
        action: function() { solarSystem.run(); }
    }
}.play();


var w = 600;
var h = 400;

Stage {
    title: "Planetary System"
    width: w
    height: h
    scene: Scene{
        content: Group{
           content: PlanetarySystemShape{
                transforms: bind [Transform.translate(w/2, h/2)]
                scale: Scale{ coordinateScale:  100.0/150e9 radiusScale: 20.0/14000.0}
                planetarySystem: solarSystem
            }
        }
        fill: Color.BLACK
    }
}

Advertisement