在绘图计算器中实现放大和缩小

时间:2014-02-07 16:43:31

标签: java plot zoom scale calculator

我正在制作一个绘制计算器,用于追踪某些功能的Graphes,但我正在努力

实施缩放选项。

我想要的是获得与此链接Plotting Calculator

相同的结果

我使用Javafx和Canvas绘制图形。

实际上,我不知道如何继续,但如果你愿意,我可以在这里发布我的代码!

提前感谢所有人!

编辑:这是我用于绘制X²函数的代码!

    package application;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class Square extends Application {

    double pixels=800;
    double xc;
    double yc;
    int i=0;



     Canvas can = new Canvas(800, 800);
     GraphicsContext gc = can.getGraphicsContext2D();


    @Override
    public void start(Stage primaryStage) {
        try {
            BorderPane root = new BorderPane();
            root.setCenter(can);
            Button btn = new Button("Draw image");
            Button btn2 = new Button("Zoom image");
            Button btn3 = new Button("Zoom out image");

            root.setTop(btn);
            root.setBottom(btn2);
            root.setLeft(btn3);

            btn3.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent arg0) {
                    i--;
                    if(i<=0)
                        i=1;
                    can.setScaleX(i);
                    can.setScaleY(i);
                }
            });
            btn2.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent arg0) {
                    i++;
                    can.setScaleX(i);
                    can.setScaleY(i);
                }
            });

            btn.setOnAction(new EventHandler<ActionEvent>() {

                @Override
                public void handle(ActionEvent arg0) {
                                   //Use this commands to trace the x axis and y axis and launching the plotting method
                    gc.setStroke(Color.RED);
                    gc.strokeLine(0, 400, 800, 400);
                    gc.strokeLine(400, 0, 400, 800);
                    traceImage();
                }
            });


            Scene scene = new Scene(root,800,800);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

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

    //This the for loop for tracing the graph, i use r and yc to define the first points coordinates and xc and r+1 to define the second point coordinates, these for the line to be traced
    public void traceImage(){

        xc = 0;
        gc.setStroke(Color.GREEN);
        for (double r =-50; r < 50; r++) {
            yc = -Math.pow(r, 2);
            xc = -Math.pow((r+1),2);

            gc.strokeLine((r+400), (yc+400), ((r+1)+400), (xc+400));

        }
    }

}

0 个答案:

没有答案