gpt4 book ai didi

java - 根据 X 和 Y 坐标绘制线条

转载 作者:行者123 更新时间:2023-12-02 05:20:36 26 4
gpt4 key购买 nike

我目前正在创建一个程序,您可以在其中创建一个星座,然后一个人输入 X 和 Y 坐标以在 Canvas 上获得一颗或多颗星星,我的主要问题是如何让线路连接到用户的每个点输入,从而形成一个星座。我尝试通过单独的尝试进行一段时间和一个 for 循环,但它没有成功,我最终感到困惑。我只需要有人告诉我如何做或者为什么它有效。感谢所有帮助,谢谢,代码也将在下面+图像,显示我运行它时的样子,我画了一条线来显示它应该是什么样子(/image/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;

可能还有其他方法可以做到这一点,但这是我想到的第一个方法;)

关于java - 根据 X 和 Y 坐标绘制线条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56264498/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com