Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.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
在JavaFX中使用画布绘制笛卡尔平面图_Java_Javafx_Javafx 2 - Fatal编程技术网

在JavaFX中使用画布绘制笛卡尔平面图

在JavaFX中使用画布绘制笛卡尔平面图,java,javafx,javafx-2,Java,Javafx,Javafx 2,我有这个方法,可以使用画布在JavaFX中绘制笛卡尔平面 public class Grafics extends StackPane { private Canvas canvas; public void Grafics(){ GridPane grid = new GridPane(); grid.setPadding(new Insets(5)); grid.set

我有这个方法,可以使用画布在JavaFX中绘制笛卡尔平面

    public class Grafics extends StackPane {

       private Canvas canvas;

           public void Grafics(){ 

            GridPane grid = new GridPane();
            grid.setPadding(new Insets(5));
            grid.setHgap(10);
            grid.setVgap(10);             

            canvas = new Canvas();
            canvas.setHeight(500);
            canvas.setWidth(700);
            GridPane.setHalignment(canvas, HPos.CENTER);
            grid.add(canvas, 0, 2);

            GraphicsContext gc = canvas.getGraphicsContext2D();
            gc.setFill(Color.BLACK);
            gc.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
            gc.setFill(Color.WHITE);
            gc.fillRect(1, 1, canvas.getWidth() - 2, canvas.getHeight() - 2);

           drawAxesXY(gc); //call the method drawAxes

           getChildren().addAll(grid);// add an gridpane in stackpane
         }



private void drawAxesXY(GraphicsContext gc1) {

            gc1 = canvas.getGraphicsContext2D().getPixelWriter();  

            PixelWriter pixelWriter = gc1.getPixelWriter();

            gc1.setFill(Color.BLACK);
            gc1.setStroke(Color.BLACK);
            gc1.setLineWidth(1.5);
            gc1.strokeText("Y", 350, 30);
            gc1.scale(1, 1);  
            gc1.translate((canvas.getHeight() / 50) - (canvas.getWidth() / 10), canvas.getHeight() / 50);
           gc1.strokeLine(canvas.getWidth() - 300, canvas.getWidth() - 300, canvas.getHeight() - 100, canvas.getHeight() / 30) ;
           pixelWriter.setColor(300, 300, Color.RED); //use here


           gc1.strokeText("X", 620, 220);  
           gc1.translate(canvas.getWidth() - (canvas.getHeight() / 10), -220);
           gc1.rotate(90.0);
           gc1.setFill(Color.BLACK);
           gc1.setStroke(Color.BLACK);
           gc1.setLineWidth(1.5);
           gc1.strokeLine(canvas.getWidth() - 250, canvas.getWidth() - 250,
                                    canvas.getHeight() - 50, canvas.getHeight() / 30);

           pixelWriter.setColor(620, 220, Color.RED);//use here

                                }
                            }
这是我的代码图

我想画这样的例子

在另一篇文章中,他们建议我使用PixelWriter在画布上写入像素。我试过了,但没用

我认为我在JavaFX中使用canvas绘制笛卡尔平面的方法是不正确的,没有其他方法在JavaFX中绘制笛卡尔平面,而不使用PixelWriter

如何在JavaFX中使用画布绘制笛卡尔平面,并显示轴(x,y)和(-x,-y)的坐标,如示例所示

我建议使用和内置节点,而不是使用

下面的代码不是一个通用函数绘图仪,而是提供了一个如何创建函数绘图仪的示例

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.geometry.*;
import javafx.scene.Scene;
import javafx.scene.chart.NumberAxis;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.stage.Stage;

import java.util.function.Function;

// Java 8 code
public class CartesianPlot extends Application {

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

    @Override
    public void start(final Stage stage) {
        Axes axes = new Axes(
                400, 300,
                -8, 8, 1,
                -6, 6, 1
        );

        Plot plot = new Plot(
                x -> .25 * (x + 4) * (x + 1) * (x - 2),
                -8, 8, 0.1,
                axes
        );

        StackPane layout = new StackPane(
                plot
        );
        layout.setPadding(new Insets(20));
        layout.setStyle("-fx-background-color: rgb(35, 39, 50);");

        stage.setTitle("y = \u00BC(x+4)(x+1)(x-2)");
        stage.setScene(new Scene(layout, Color.rgb(35, 39, 50)));
        stage.show();
    }

    class Axes extends Pane {
        private NumberAxis xAxis;
        private NumberAxis yAxis;

        public Axes(
                int width, int height,
                double xLow, double xHi, double xTickUnit,
                double yLow, double yHi, double yTickUnit
        ) {
            setMinSize(Pane.USE_PREF_SIZE, Pane.USE_PREF_SIZE);
            setPrefSize(width, height);
            setMaxSize(Pane.USE_PREF_SIZE, Pane.USE_PREF_SIZE);

            xAxis = new NumberAxis(xLow, xHi, xTickUnit);
            xAxis.setSide(Side.BOTTOM);
            xAxis.setMinorTickVisible(false);
            xAxis.setPrefWidth(width);
            xAxis.setLayoutY(height / 2);

            yAxis = new NumberAxis(yLow, yHi, yTickUnit);
            yAxis.setSide(Side.LEFT);
            yAxis.setMinorTickVisible(false);
            yAxis.setPrefHeight(height);
            yAxis.layoutXProperty().bind(
                Bindings.subtract(
                    (width / 2) + 1,
                    yAxis.widthProperty()
                )
            );

            getChildren().setAll(xAxis, yAxis);
        }

        public NumberAxis getXAxis() {
            return xAxis;
        }

        public NumberAxis getYAxis() {
            return yAxis;
        }
    }

    class Plot extends Pane {
        public Plot(
                Function<Double, Double> f,
                double xMin, double xMax, double xInc,
                Axes axes
        ) {
            Path path = new Path();
            path.setStroke(Color.ORANGE.deriveColor(0, 1, 1, 0.6));
            path.setStrokeWidth(2);

            path.setClip(
                    new Rectangle(
                            0, 0, 
                            axes.getPrefWidth(), 
                            axes.getPrefHeight()
                    )
            );

            double x = xMin;
            double y = f.apply(x);

            path.getElements().add(
                    new MoveTo(
                            mapX(x, axes), mapY(y, axes)
                    )
            );

            x += xInc;
            while (x < xMax) {
                y = f.apply(x);

                path.getElements().add(
                        new LineTo(
                                mapX(x, axes), mapY(y, axes)
                        )
                );

                x += xInc;
            }

            setMinSize(Pane.USE_PREF_SIZE, Pane.USE_PREF_SIZE);
            setPrefSize(axes.getPrefWidth(), axes.getPrefHeight());
            setMaxSize(Pane.USE_PREF_SIZE, Pane.USE_PREF_SIZE);

            getChildren().setAll(axes, path);
        }

        private double mapX(double x, Axes axes) {
            double tx = axes.getPrefWidth() / 2;
            double sx = axes.getPrefWidth() / 
               (axes.getXAxis().getUpperBound() - 
                axes.getXAxis().getLowerBound());

            return x * sx + tx;
        }

        private double mapY(double y, Axes axes) {
            double ty = axes.getPrefHeight() / 2;
            double sy = axes.getPrefHeight() / 
                (axes.getYAxis().getUpperBound() - 
                 axes.getYAxis().getLowerBound());

            return -y * sy + ty;
        }
    }
}
导入javafx.application.application;
导入javafx.beans.binding.Bindings;
导入javafx.geometry.*;
导入javafx.scene.scene;
导入javafx.scene.chart.NumberAxis;
导入javafx.scene.layout.*;
导入javafx.scene.paint.Color;
导入javafx.scene.shape.*;
导入javafx.stage.stage;
导入java.util.function.function;
//Java8代码
公共类CartesianPlot扩展了应用程序{
公共静态void main(字符串[]args){
发射(args);
}
@凌驾
公共作废开始(最后阶段){
轴=新轴(
400, 300,
-8, 8, 1,
-6, 6, 1
);
绘图=新绘图(
x->.25*(x+4)*(x+1)*(x-2),
-8, 8, 0.1,
斧头
);
StackPane布局=新建StackPane(
情节
);
布局。设置填充(新插图(20));
布局。设置样式(“-fx背景色:rgb(35,39,50);”;
舞台剧名(“y=\u00BC(x+4)(x+1)(x-2)”;
舞台场景(新场景(布局、颜色、rgb(35、39、50));
stage.show();
}
类扩展窗格{
私人号码xis xAxis;
私人号码xis yAxis;
公斧(
整数宽度,整数高度,
双xLow,双xHi,双xTickUnit,
双yLow,双yHi,双yTickUnit
) {
setMinSize(Pane.USE_PREF_SIZE,Pane.USE_PREF_SIZE);
setPrefSize(宽度、高度);
setMaxSize(Pane.USE_PREF_SIZE,Pane.USE_PREF_SIZE);
xAxis=新的数字axis(xLow、xHi、xTickUnit);
X轴设置侧(侧、底);
xAxis.setMinorTickVisible(false);
xAxis.setPrefWidth(宽度);
X轴设置布局(高度/2);
yAxis=新的数字轴(yLow、yHi、yTickUnit);
yAxis.setSide(左侧);
yAxis.setMinorTickVisible(false);
yAxis.setPrefHeight(高度);
yAxis.layoutXProperty().bind(
绑定。减法(
(宽度/2)+1,
yAxis.widthProperty()
)
);
getChildren().setAll(xAxis,yAxis);
}
公号axis getXAxis(){
返回xAxis;
}
公共号码axis getYAxis(){
返回yAxis;
}
}
类绘图扩展窗格{
公共地块(
功能f,
双xMin,双xMax,双xInc,
轴线
) {
路径路径=新路径();
设置行程(颜色.橙色.衍生颜色(0,1,1,0.6));
路径设置行程宽度(2);
path.setClip(
新矩形(
0, 0, 
axes.getPrefWidth(),
axes.getPrefHeight()
)
);
双x=xMin;
双y=f。应用(x);
path.getElements().add(
新动作(
mapX(x,轴),mapY(y,轴)
)
);
x+=xInc;
而(x

另一个用户使用上面的代码创建了一个示例,可以绘制用户键入的任意函数。使用以下命令解析函数:


谢谢jewelsea,但要这样绘制,我需要使用画布绘制笛卡尔平面和多边形。我建议使用场景图而不是画布来完成此任务,但每个人都有自己的画法。祝你的项目好运。你会如何使用场景图和内置的NumberAxis节点,而不是你发布的代码?我用
NumberAxis
LineChart
绘制图表。我不明白你的问题。我发布的代码确实使用了场景图和数字轴。