基于X和Y坐标的图线

  • 本文关键字:坐标 基于 java graphics
  • 更新时间 :
  • 英文 :


我当前正在创建一个程序,您可以在其中创建一个星座,并且一个人输入x和y坐标以在画布上获得恒星或星星,我的主要问题是如何获得行要连接到用户输入的每个点,因此要建立一个星座。我尝试通过单独的尝试进行一段时间,但没有奏效,但我最终感到困惑。我只需要有人告诉我如何做或为什么它可以工作。请感谢所有帮助,谢谢您,也将在下面的 图像下方添加代码,以了解其运行时的外观,我绘制了线条以显示其应该是如何的(https://i.stack.imgur。com/f47ab.jpg(

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;

可能还有其他方法可以做到这一点,但这是第一个从我的脑海中弹出的方法;(

相关内容

  • 没有找到相关文章

最新更新