Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Performance JavaFX画布应用程序的计时_Performance_Swing_Javafx_Graphics - Fatal编程技术网

Performance JavaFX画布应用程序的计时

Performance JavaFX画布应用程序的计时,performance,swing,javafx,graphics,Performance,Swing,Javafx,Graphics,为了练习JavaFX,我构建了一个简单的应用程序来绘制 导入javafx.application.application; 导入javafx.application.Platform; 导入javafx.concurrent.Task; 导入javafx.geometry.Insets; 导入javafx.geometry.Point2D; 导入javafx.geometry.Pos; 导入javafx.scene.scene; 导入javafx.scene.canvas.canvas; 导入j

为了练习JavaFX,我构建了一个简单的应用程序来绘制

导入javafx.application.application;
导入javafx.application.Platform;
导入javafx.concurrent.Task;
导入javafx.geometry.Insets;
导入javafx.geometry.Point2D;
导入javafx.geometry.Pos;
导入javafx.scene.scene;
导入javafx.scene.canvas.canvas;
导入javafx.scene.canvas.GraphicsContext;
导入javafx.scene.layout.ancorpane;
导入javafx.scene.layout.BorderPane;
导入javafx.scene.layout.Pane;
导入javafx.scene.paint.Color;
导入javafx.stage.stage;
公共类SierpinskiTriangles扩展了应用程序{
私有最终整数填充=5;
私有静态整数层;
公共静态无效启动(字符串…参数){
等级数=8;
如果((args!=null)&&(args.length>0)){
int num=-1;
试一试{
num=Integer.parseInt(args[0]);
}捕获(NumberFormatException ex){
例如printStackTrace();
返回;
}
numberOfLevels=(num>0)?num:numberOfLevels;
}
应用程序启动(args);
}
@凌驾
公众假期开始(阶段){
stage.setOnCloseRequest((ae)->{
Platform.exit();
系统出口(0);
});
舞台。片名(“西格宾斯基三角形(fx)”;
BorderPane mainPane=新建BorderPane();
主窗格。设置填充(新插入(填充));
窗格triPanel=新三角形();
边框窗格。设置对齐(三面板,位置中心);
主窗格。设置中心(triPanel);
场景=新场景(主窗格);
舞台场景;
stage.centerOnScreen();
阶段。可设置大小(假);
stage.show();
}
类三角形扩展锚链{
专用静态最终内部面板宽度=600,面板高度=600;
专用静态最终内部TRI_宽度=500,TRI_高度=500;
专用静态最终内部侧间隙=(面板宽度-三角宽度)/2;
专用静态最终内部顶部间隙=(面板高度-三角高度)/2;
私有整数三角形;
私人长启动时间;
私有点2D顶部、左侧、右侧;
私人帆布;
私有图形上下文gc;
三角形(){
setPrefSize(面板宽度、面板高度);
canvas=getCanvas();
gc=canvas.getGraphicsContext2D();
getChildren().add(画布);
绘制(层数);
}
无效绘制(整数级){
Platform.runLater(新的Runnable(){
@凌驾
公开募捐{
clearCanvas();
设置起点();
startTime=System.currentTimeMillis();
可数三角形=0;
RunTask task=新的RunTask(数级、上、左、右);
线程线程=新线程(任务);
setDaemon(true);
thread.start();
}
});
}
私有void drawTriangle(int Level、Point2D top、Point2D left、Point2D right){
如果(级别<0){//添加停止条件
返回;
}
strokePolygon(//使用strokeLine实现没有多大区别
新双[]{
top.getX(),left.getX(),right.getX()
},
新双[]{
top.getY(),left.getY(),right.getY()
},3
);
可数三角形++;
//获取三角形中每条边的中点
点2D p12=中点(顶部,左侧);
点2D p23=中点(左、右);
点2D p31=中点(右,顶部);
//在3个三角形区域上递归
drawTriangle(标高-1,顶部,p12,p31);
drawTriangle(标高-1、p12、左侧、p23);
drawTriangle(级别-1,第31页,第23页,右侧);
}
私有无效设置起点(){
顶部=新点2D(getPrefWidth()/2,顶部间隙);
左=新点2D(侧面间隙、顶部间隙+三角高度);
右=新点2D(侧面间隙+三角宽度,顶部间隙+三角宽度);
}
专用点2D中点(点2D p1、点2D p2){
返回新的点2d((p1.getX()+p2.getX())/
2,(p1.getY()+p2.getY())/2);
}
私有void updateGraphics(布尔成功){
如果(成功){
gc.fillText(“三角形数:“+countTriangles,5,15”);
gc.fillText(“时间:”+(System.currentTimeMillis()-startTime)+“毫秒”,5,35);
gc.fillText(“级别:”+numberOfLevels,5,55);
}
System.out.println(“在以下时间后完成:”+
(System.currentTimeMillis()-startTime)+“毫秒”
+“三角形:“+countTriangles+”失败:“+!成功);
}
私有画布getCanvas(){
画布=新画布();
canvas.widthProperty().bind(widthProperty());
canvas.heightProperty().bind(heightProperty());
canvas.getGraphicsContext2D().setStroke(Color.RED);
canvas.getGraphicsContext2D().setLineWidth(0.3f);
返回画布;
}
私有void clearCanvas(){
gc.clearRect(0,0,canvas.getWidth(),canvas.getHeight());
}
类RunTask扩展了任务{
私人智力水平;
私有点2D顶部,左侧;
私权;
RunTask(int级别、Point2D顶部、Point2D左侧、Point2D右侧){
这个。级别=级别;
this.top=top;
this.left=左;
这个。右=右;
startTime=System.currentTimeMillis();
三角伯爵
import javafx.application.Application;
import javafx.application.Platform;
import javafx.concurrent.Task;
import javafx.geometry.Insets;
import javafx.geometry.Point2D;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class SierpinskiTriangles extends Application {

    private final int PADDING = 5;
    private static int numberOfLevels;

    public static void launch(String... args){

        numberOfLevels = 8;

        if((args != null) && (args.length > 0)) {

            int num = -1;
            try {
                num = Integer.parseInt(args[0]);
            } catch (NumberFormatException ex) {
                            ex.printStackTrace();
                return;
            }

            numberOfLevels = (num > 0) ? num : numberOfLevels;
        }

        Application.launch(args);
    }

    @Override
    public void start(Stage stage) {

        stage.setOnCloseRequest((ae) -> {
            Platform.exit();
            System.exit(0);
        });

        stage.setTitle("Sierpinski Triangles (fx)");

        BorderPane mainPane = new BorderPane();
        mainPane.setPadding(new Insets(PADDING));

        Pane triPanel = new Triangles();

        BorderPane.setAlignment(triPanel, Pos.CENTER);
        mainPane.setCenter(triPanel);

        Scene scene = new Scene(mainPane);

        stage.setScene(scene);
        stage.centerOnScreen();

        stage.setResizable(false);
        stage.show();
    }

    class Triangles  extends AnchorPane{

        private static final int PANEL_WIDTH =600, PANEL_HEIGHT = 600;
        private static final int TRI_WIDTH= 500, TRI_HEIGHT= 500;
        private static final int SIDE_GAP = (PANEL_WIDTH - TRI_WIDTH)/2;
        private static final int TOP_GAP = (PANEL_HEIGHT - TRI_HEIGHT)/2;
        private int countTriangles;
        private long startTime;
        private Point2D top, left, right;

        private Canvas canvas;
        private GraphicsContext gc;

        Triangles(){

            setPrefSize(PANEL_WIDTH, PANEL_HEIGHT);

            canvas = getCanvas();
            gc = canvas.getGraphicsContext2D();
            getChildren().add(canvas);
            draw(numberOfLevels);
        }

        void draw(int numberLevels) {

            Platform.runLater(new Runnable() {

                @Override
                public void run() {

                    clearCanvas();
                    setStartPoints();

                    startTime = System.currentTimeMillis();
                    countTriangles = 0;

                    RunTask task = new RunTask(numberLevels, top, left, right);
                    Thread thread = new Thread(task);
                    thread.setDaemon(true);
                    thread.start();
                }
            });

        }

        private void drawTriangle( int levels, Point2D top, Point2D left, Point2D right) {

            if(levels < 0) {//add stop criteria
                return ;
            }

            gc.strokePolygon( //implementing with strokeLine did not make much difference
                    new double[]{
                            top.getX(),left.getX(),right.getX()
                    },
                    new double[]{
                            top.getY(),left.getY(), right.getY()
                    },3
                    );

            countTriangles++;

            //Get the midpoint on each edge in the triangle
            Point2D p12 = midpoint(top, left);
            Point2D p23 = midpoint(left, right);
            Point2D p31 = midpoint(right, top);

            // recurse on 3 triangular areas
            drawTriangle(levels - 1, top, p12, p31);
            drawTriangle(levels - 1, p12, left, p23);
            drawTriangle(levels - 1, p31, p23, right);
        }

        private void setStartPoints() {

            top = new Point2D(getPrefWidth()/2, TOP_GAP);
            left = new Point2D(SIDE_GAP, TOP_GAP + TRI_HEIGHT);
            right = new Point2D(SIDE_GAP + TRI_WIDTH, TOP_GAP + TRI_WIDTH);
        }

        private Point2D midpoint(Point2D p1, Point2D p2) {

            return new Point2D((p1.getX() + p2.getX()) /
                    2, (p1.getY() + p2.getY()) / 2);
        }

        private void updateGraphics(boolean success){

            if(success) {

                gc.fillText("Number of triangles: "+ countTriangles,5,15);
                gc.fillText("Time : "+ (System.currentTimeMillis()- startTime)+ " mili seconds", 5,35);
                gc.fillText("Levels: "+ numberOfLevels,5,55);
            }

            System.out.println("Completed after: "+
                    (System.currentTimeMillis()- startTime)+ " mili seconds"
                    +"  Triangles: " + countTriangles  +"  Failed: "+ !success );
        }

        private Canvas getCanvas() {

            Canvas canvas = new Canvas();
            canvas.widthProperty().bind(widthProperty());
            canvas.heightProperty().bind(heightProperty());
            canvas.getGraphicsContext2D().setStroke(Color.RED);
            canvas.getGraphicsContext2D().setLineWidth(0.3f);

            return canvas;
        }

        private void clearCanvas() {

            gc.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
        }

        class RunTask extends Task<Void>{

            private int levels;
            private Point2D top, left;
            private Point2D right;

            RunTask(int levels, Point2D top, Point2D left, Point2D right){

                this.levels = levels;
                this.top = top;
                this.left = left;
                this.right = right;

                startTime = System.currentTimeMillis();
                countTriangles = 0;
            }

            @Override public Void call() {
                drawTriangle(levels,top, left, right);
                return null;
            }

            @Override
            protected void succeeded() {

                updateGraphics(true);
                super.succeeded();
            }

            @Override
            protected void failed() {

                updateGraphics(false);
            }
        }
    }

    public static void main(String[] args) {
        launch("13");
    }
}
import javafx.application.Application;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Task;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/**
 * @see https://stackoverflow.com/a/44056730/230513
 */
public class CanvasTaskTest extends Application {

    private static final int W = 800;
    private static final int H = 600;

    @Override
    public void start(Stage stage) {
        stage.setTitle("CanvasTaskTest");
        StackPane root = new StackPane();
        Canvas canvas = new Canvas(W, H);
        root.getChildren().add(canvas);
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
        CanvasTask task = new CanvasTask();
        task.valueProperty().addListener((ObservableValue<? extends Canvas> observable, Canvas oldValue, Canvas newValue) -> {
            root.getChildren().remove(oldValue);
            root.getChildren().add(newValue);
        });
        Thread thread = new Thread(task);
        thread.setDaemon(true);
        thread.start();
    }

    private static class CanvasTask extends Task<Canvas> {

        private int strokeCount;

        @Override
        protected Canvas call() throws Exception {
            Canvas canvas = null;
            for (int i = 1; i < 15; i++) {
                canvas = new Canvas(W, H);
                GraphicsContext gc = canvas.getGraphicsContext2D();
                strokeCount = 0;
                long start = System.nanoTime();
                drawTree(gc, W / 2, H - 50, -Math.PI / 2, i);
                double dt = (System.nanoTime() - start) / 1_000d;
                gc.fillText("Depth: " + i
                    + "; Strokes: " + strokeCount
                    + "; Time : " + String.format("%1$07.1f", dt) + " µs", 8, H - 8);
                Thread.sleep(200); // simulate rendering latency
                updateValue(canvas);
            }
            return canvas;
        }

        private void drawTree(GraphicsContext gc, int x1, int y1, double angle, int depth) {
            if (depth == 0) {
                return;
            }
            int x2 = x1 + (int) (Math.cos(angle) * depth * 5);
            int y2 = y1 + (int) (Math.sin(angle) * depth * 5);
            gc.strokeLine(x1, y1, x2, y2);
            strokeCount++;
            drawTree(gc, x2, y2, angle - Math.PI / 8, depth - 1);
            drawTree(gc, x2, y2, angle + Math.PI / 8, depth - 1);
        }
    }

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

}