我当前正在创建一个程序,您在其中创建一个星座,一个人输入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;


可能还有其他方法可以做到,但这是我脑海中浮现的第一个方法;)

08-03 12:41