基于X和Y坐标绘制线条

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (25)

我目前正在创建一个程序,你创建一个星座,一个人输入X和Y坐标,以在画布上得到一个或多个星星,我的主要问题是如何让线连接到用户输入的每个点,因此制作一个星座。我尝试了一段时间和一个for循环通过单独的尝试,但它没有工作,我最终感到困惑。我只需要有人告诉我该怎么做或为什么它有效。所有的帮助表示赞赏,谢谢你,代码将在下面+图像上,当我运行它时,我画了一行,以显示它应该是什么样子(https://imgur.com/18VbFPO

import javafx.application.Application;
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.shape.ArcType;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import java.awt.*;
import java.lang.Math;
import java.util.Scanner;

import static javafx.application.Application.launch;

public class constellation extends Application {
    @Override
    public void start(Stage Stage) throws Exception
    {
        Group root = new Group();
        Scene Scene = new Scene(root);
        Canvas canvas=  new Canvas(500,500);

        root.getChildren().add(canvas);
        Stage.setScene(Scene);

        GraphicsContext gc = canvas.getGraphicsContext2D();
        gc.setFill(Color.BLACK);
        gc.fillRect(0,0,500,500);
        int max = 500;
        int min = 1;
        int maxx = 5;
        int minn = 1;
        int range = max - min + 1;
        int rangee = maxx - minn + 1;
        for (int i = 0 ; i <= 250; i++)
        {

            int randnum = (int)(Math.random() * range) + min;
            int randnum2 = (int)(Math.random() * range) + min;
            int randnum3 = (int)(Math.random() * rangee) + minn;
            int randnum4 = (int)(Math.random() * rangee) + minn;
            gc.setFill(Color.WHITE);
            gc.fillOval(randnum,randnum2,randnum3,randnum4);
            Stage.show();
        }
        Scanner sc = new Scanner(System.in);

        System.out.println("Welcome to the Constellation Simulator(Press any key to continue)");
        String looper = "";
        while (true) {
            double count = 0;
            double x ;
            double y ;
            System.out.println("Hello please enter a X and Y coordinate(s)[enter 'STOP' when you are done with inputting stars]");
            looper = sc.nextLine();
            if(looper.equalsIgnoreCase("stop")){break;}
            x = Double.parseDouble(looper);
            y = Double.parseDouble(sc.nextLine());
            if ((x >500)|| (y > 500)){
                System.out.println("Sorry Invalid input restart the program and re enter your coordinates (500 MAX)");
                System.exit(0);
            }
            if ((x < 0)||(y < 0)){
                System.out.println("Sorry Invalid input, you cannot enter  value(s) less than 0");
                System.exit(0);
            }

            Stage.setTitle("Constellation Simulator");
            gc.setFill(Color.YELLOW);
            gc.fillRect(x,y,10,10);
            gc.setStroke(Color.RED);
            gc.strokeLine(x,y,x,y);

        }
        System.out.println("Please enter your constellation name");
        String constname = sc.nextLine();
        gc.setFill(Color.PAPAYAWHIP);
        gc.setFont(new Font("Papyrus",50));
        gc.fillText(constname,300,450);
        gc.setFill(Color.INDIANRED);
        gc.setFont(new Font("Papyrus",15));
        gc.fillText("-By Alexei Ougriniouk",300,470);
        Stage.show();


    }
    public static void main(String[] args) {
        launch(args);
    }
}
提问于
用户回答回答于

您需要2个点来绘制一条线,以便在第一个条目后无法开始绘制。

double startX = -1;
double startY = -1;

while( true ) {

    // ...Your code...

    if( startX > -1 || startY > -1 ) {
       Stage.setTitle("Constellation Simulator");  // Should be outside the while loop
       gc.setFill(Color.YELLOW);  // Should be outside the while loop
       gc.fillRect(x,y,10,10);
       gc.setStroke(Color.RED);  // Should be outside the while loop
       gc.strokeLine(startX,startY,x,y);
    }
}

// Save the current position
startX = x;
startY = y;

可能有其他方法可以做到这一点,但这是第一个摆脱我头脑的方法;)

扫码关注云+社区

领取腾讯云代金券