This discussion is archived
0 Replies Latest reply: Dec 1, 2012 6:31 PM by 977433 RSS

JavaFX2 Application - Wall Board News Ticker (General assistance request)

977433 Newbie
Currently Being Moderated
Hello,

I'm a newcomer to java 7 and javafx, I'm teaching myself and really enjoying working with and through the api's on offer. I work on a service desk and started to develop a means of communicating important news items to our team when they occur, sort of like news flash items that the whole team can see by displaying it using the wall board monitor. We do use Office communicator and email, but with some much going on, its easy to ignore.

This is what I have so far, most of which has been pulled from the detailed tutorials available,my own knowledge and of course Google:
package canvastestfx;

import java.awt.Dimension;
import java.awt.Toolkit;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontSmoothingType;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.util.Duration;


public class CanvasTestFX extends Application {
    
    private double textXPos;
    private double textYPos;
    private double textSize;
    
    private Toolkit tk;
    private Dimension screenDimension;
    private String stringToDisplay;
    private Text text;
    
    private Canvas canvas;
    private GraphicsContext canvasgc;
    
    @Override
    public void init(){
        
        tk = Toolkit.getDefaultToolkit();
        screenDimension = tk.getScreenSize();
    
        stringToDisplay = "Text to display";
        text = new Text(stringToDisplay);
        textSize = 60.00;
        text.setFont(Font.font("Verdana", textSize));
        textXPos = screenDimension.getWidth();
        textYPos = textSize;
        text.setX(textXPos);
        text.setY(textYPos);
        text.setFill(Color.SPRINGGREEN);
        text.setFontSmoothingType(FontSmoothingType.LCD);
    }
    
    @Override
    public void start(Stage primaryStage) {
        
        primaryStage.initStyle(StageStyle.UNDECORATED);
        primaryStage.setX(0);
        primaryStage.setY(0);
        primaryStage.setTitle("JavaFX 2 Canvas Test");

        Group root = new Group();
        canvas = new Canvas(screenDimension.getWidth(), textSize+10);
        canvasgc = canvas.getGraphicsContext2D();
        canvasgc.setFill(Color.BLACK);
        canvasgc.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
        
        Animate();

        root.getChildren().add(canvas);
        root.getChildren().add(text);
        
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }
    
    private void Animate(){
        
        final Timeline timeline = new Timeline();
        timeline.setCycleCount(Timeline.INDEFINITE);
        
        final KeyValue keyvalue = new KeyValue(text.xProperty(), screenDimension.getWidth()-screenDimension.getWidth()-text.getBoundsInLocal().getWidth());
        final KeyFrame keyframe = new KeyFrame(Duration.millis(10000), keyvalue);
        timeline.getKeyFrames().add(keyframe);
        timeline.play();
    }
    
    @Override
    public void stop()
    {
        Platform.exit();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
It seems to work quite well, except after a couple of cycles, the text starts to skip forward a few pixels, before normalising again and I'm not sure why this is. Also, if I set the stage style to StageStyle.UTILITY, it tends to stabilise it further. I don't really want the frame or decoration around the ticker as we have other information below where the ticker will be displayed, such as calls waiting and abandon rate graphs (which I'll be looking to convert to java aswell, runs with VB from an excel doc at the moment).

In addition, I'm looking at a way to network this application, create a client GUI which will reside on the team leaders machine so she can use it to change the text displayed on the wallboard machine.

Any and all suggestions/comments on the above code with regard to improvement would be most welcome as would suggestions or pointers on how's best to network this application.