简体   繁体   English

基于X和Y坐标绘制线条

[英]Drawing line(s) based on X and Y coordinates

I am currently creating a program where you create a constellation and a person inputs X and Y coordinates to get a star or stars on the canvas, my main question is how do I get the line to connect to every single point the user inputs, hence making a constellation. 我目前正在创建一个程序,你创建一个星座,一个人输入X和Y坐标,以在画布上得到一个或多个星星,我的主要问题是如何让线连接到用户输入的每个点,因此制作一个星座。 I tried making a while and a for loop through separate attempts but it didnt work out and I ended up being confused. 我尝试了一段时间和一个for循环通过单独的尝试,但它没有工作,我最终感到困惑。 I just need someone to tell me how to do it or why it works. 我只需要有人告诉我该怎么做或为什么它有效。 All help is appreciated please and thank you , also code will be down below + image on what it looks like when i run it, i drew the line to show how its supposed to look like( https://imgur.com/18VbFPO ) 所有的帮助表示赞赏,谢谢你,代码将在下面+图像上,当我运行它时,我画了一行,以显示它应该是什么样子( 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);
    }
}

You need 2 points to draw a line so you cannot start drawing after the first entry. 您需要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;

There are probably other ways to do this, but that's the first one that poped out of my head ;) 可能有其他方法可以做到这一点,但这是第一个摆脱我头脑的方法;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM