我们如何使文本自动滚动连续循环?

时间:2014-06-29 00:37:07

标签: java javafx fxml autoscroll

我正在寻找一种方法,使用JavaFX使一些文本行自动滚动连续循环。这与下面这些问题中的credits-like scrolling非常相似。它是工作站屏幕,而不是移动设备。文本有点固定,而不是来自Feed,所以我希望它能够传播......

一个简单的用例:您有30行文本,但任何时候都只能看到12行。所以我想在屏幕上滚动文本,稍微休息一下,然后文本环绕并继续滚动。

我以为我可以在底部添加文字并将其从顶部带走,但结果却不一样。使用向上滚动的视觉效果......字面上是规范的重要部分。所以我回到了开始。

我更喜欢的一些事情是不需要破坏文本或重新加载。更喜欢指向当前的文本顶部或其他东西,以便它包装。否则就像以前一样,我需要从顶部删除文本并将文本放在底部。需要JavaFX,不能使用Javascript for app。你最好的投篮是什么? 谢谢提前。

2 个答案:

答案 0 :(得分:1)

这里有一些滚动文字

import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Duration;

public class Scroll extends Application {

    @Override
    public void start(Stage primaryStage) {
        VBox vbox = new VBox();
        for (int i = 0; i < 30; i++)
            vbox.getChildren().add(new Text("line " + i));
        //add a copy of the first 12 lines that will be showing as wrapped
        for (int i = 0; i < 12; i++)
            vbox.getChildren().add(new Text("line " + i));

        ScrollPane sp = new ScrollPane(vbox);
        Scene scene = new Scene(sp, 300, 10*12);//guess height

        primaryStage.setScene(scene);
        primaryStage.show();
        //resize to exactly 12 lines
        double textHeight = vbox.getHeight() / vbox.getChildren().size();
        primaryStage.setHeight(textHeight*12+primaryStage.getHeight()-scene.getHeight());

        Timeline timeline = new Timeline();
        timeline.setCycleCount(Timeline.INDEFINITE);
        KeyValue kv = new KeyValue(sp.vvalueProperty(), sp.getVmax());
        KeyFrame kf = new KeyFrame(Duration.millis(5000), kv);
        timeline.getKeyFrames().addAll(kf);
        timeline.play();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

我意识到这不是你想要的。我尝试了一些技巧来拍摄vbox的图像然后一遍又一遍地播放,但除非你得到恰到好处,否则会有一些口吃。这只是为了满足只读一次文本的要求。你需要的是一个圆形结构,像圆柱一样平滑滚动。看看我的其他答案,以获得一些乐趣。

答案 1 :(得分:0)

好的,我认为我得到了所有的要求。

import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.geometry.Point3D;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.PointLight;
import javafx.scene.Scene;
import javafx.scene.SnapshotParameters;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Cylinder;
import javafx.scene.text.Text;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
import javafx.util.Duration;

public class Scroll extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        //a vbox to take a picture of
        VBox vbox = new VBox();
        for (int i = 0; i < 30; i++)
            vbox.getChildren().add(new Text(" longer line of text " + i + " "));

        //take a sideways picture to fit the cylinder
        SnapshotParameters snapshotParameters = new SnapshotParameters();
        snapshotParameters.setTransform(new Rotate(90));
        WritableImage snapshot = vbox.snapshot(snapshotParameters, null);

        //make sideways cyl with image
        PhongMaterial material = new PhongMaterial();
        final Cylinder cylinder = new Cylinder(500, snapshot.getWidth(),30);
        material.setDiffuseMap(snapshot);
        cylinder.setMaterial(material);
        cylinder.setRotate(-90);
        cylinder.setTranslateX(snapshot.getWidth());
        cylinder.setTranslateY(500);

        //lights camera show
        final Group root = new Group();
        root.getChildren().add(cylinder);

        final Scene scene = new Scene(root, snapshot.getWidth()*2, cylinder.getRadius()*2, true);
        PointLight pointLight = new PointLight(Color.ALICEBLUE);
        pointLight.setTranslateX(150);
        pointLight.setTranslateY(500);
        pointLight.setTranslateZ(-1000);
        PerspectiveCamera cam = new PerspectiveCamera(false);
        scene.setCamera(cam);
        root.getChildren().addAll(pointLight, cam);

        primaryStage.setScene(scene);
        primaryStage.show();

        //I'll spin bob 
        Rotate rx = new Rotate();
        rx.setAxis(Rotate.Y_AXIS);
        cylinder.getTransforms().add(rx);
        cam.setRotationAxis(Point3D.ZERO);
        Timeline timeline = new Timeline();
        timeline.setCycleCount(Timeline.INDEFINITE);
        final KeyValue kv = new KeyValue(rx.angleProperty(), -360);
        final KeyFrame kf = new KeyFrame(Duration.millis(10000), kv);
        timeline.getKeyFrames().add(kf);
        timeline.play();
    }
}

enter image description here