嘿所有我是java和javafx的新手试图学习它们。我目前正在尝试用户输入两个圆的值(圆心和半径)。它应该显示圆圈,并显示它们正在做什么,例如它们是相等的,一个包含在另一个中等等。
为此,我使用了vbox,我尝试使用窗格绘制圆圈,并将窗格实现到vbox中。这是我的代码:
public class Shapes extends Application {
@Override
public void start(Stage stage) {
createTextField();
VBox root = new VBox();
root.setSpacing(10);
Pane pane = new Pane();
Color blue = new Color(1,0,0,1);
Color red = new Color(0,0,1,1);
Circle circle1 = createCircle(50, 50, 50, red);
Circle circle2 = createCircle(50, 50, 200, blue);
pane.getChildren().addAll(circle1, circle2);
root.getChildren().addAll(label0, label1, label2, tField, label3, label4, pane);
Scene scene = new Scene(root, 1000 , 1000);
stage.setTitle("Spatial Relations Demo by");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
Label label0, label1, label2, label3, label4;
TextField tField;
public void createTextField()
{
label0 = new Label();
label0.setText("Spatial Relations Demo");
label1 = new Label();
label1.setText(" ");
label1.setTextAlignment(TextAlignment.CENTER);
label2 = new Label();
label2.setText("Input Circles: x1 y1 r1 x2 y2 r2 ");
label2.setTextAlignment(TextAlignment.CENTER);
label3 = new Label();
label3.setTextAlignment(TextAlignment.CENTER);
label4 = new Label();
label4.setTextAlignment(TextAlignment.CENTER);
tField = new TextField();
tField.setOnAction(new TextFieldHandler());
}
public class TextFieldHandler implements EventHandler<ActionEvent> {
public void handle( ActionEvent e)
{
String str = tField.getText();
int x1, y1, r1, x2, y2, r2;
Scanner scanner = new Scanner(str);
x1 = scanner.nextInt();
y1 = scanner.nextInt();
r1 = scanner.nextInt();
x2 = scanner.nextInt();
y2 = scanner.nextInt();
r2 = scanner.nextInt();
tField.setText( "" );
String str1 = str.format("Input is: " + x1 + " " + y1 + " " + r1 + " " + x2 + " " + y2 + " " + r2);
label3.setText(str1);
double d = sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
if ((x1==x2) & (y1==y2) & (r1==r2))
label4.setText( " The circles are equal.");
else if (d >= (r1+r2))
label4.setText( " The circle interiors are disjoint.");
else if (d <= (r2-r1))
label4.setText( " Circle1 is inside Circle2.");
else if (d <= (r1-r2))
label4.setText( " Circle2 is inside Circle1.");
else
label4.setText( " The circles overlap.");
}
}
public Circle createCircle(int x, int y, int r, Color color)
{
Circle circle = new Circle();
circle.setRadius(r);
circle.setCenterX(x);
circle.setCenterY(y);
circle.setStroke(color);
circle.setFill(null);
return circle;
}
}
截至目前,我刚刚实施了一些测试数字来绘制圆圈,但我仍在尝试弄清楚如何将用户输入的数字作为我的圆圈参数。我不知道如何让窗格正常工作。任何提示或建议将不胜感激,谢谢!我试图发布一个图像来显示我的输出,但我想我没有足够的声誉。我的圈子重叠了所有内容,而不是停留在标签和文本字段下方。我似乎无法弄清楚如何将它们保持在vbox中的那些项目之下。
答案 0 :(得分:0)
你的红色圆圈(你称之为blue
;)的红色圆圈的边界在x和y方向都延伸到负坐标范围(它有中心(50,50)和半径200 )。因此,它超出了窗格的范围。
如果要确保窗格不在其边界外绘制,可以设置绑定到其大小的剪辑:
Rectangle clip = new Rectangle(0, 0, 0, 0);
clip.widthProperty().bind(pane.widthProperty());
clip.heightProperty().bind(pane.heightProperty());
pane.setClip(clip);
要更新圈子,您只需要将它们设为实例变量,而不是start
方法的本地变量,并更新它们的中心和半径:
import java.util.Scanner;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;
public class Shapes extends Application {
private Label label0, label1, label2, label3, label4;
private TextField tField;
private Circle circle1 ;
private Circle circle2 ;
@Override
public void start(Stage stage) {
createTextField();
VBox root = new VBox();
root.setSpacing(10);
Pane pane = new Pane();
Color blue = new Color(1, 0, 0, 1);
Color red = new Color(0, 0, 1, 1);
circle1 = createCircle(50, 50, 50, red);
circle2 = createCircle(50, 50, 200, blue);
pane.getChildren().addAll(circle1, circle2);
// If desired:
// Rectangle clip = new Rectangle(0, 0, 0, 0);
// clip.widthProperty().bind(pane.widthProperty());
// clip.heightProperty().bind(pane.heightProperty());
// pane.setClip(clip);
root.getChildren().addAll(label0, label1, label2, tField, label3,
label4, pane);
Scene scene = new Scene(root, 1000, 1000);
stage.setTitle("Spatial Relations Demo by");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
public void createTextField() {
label0 = new Label();
label0.setText("Spatial Relations Demo");
label1 = new Label();
label1.setText(" ");
label1.setTextAlignment(TextAlignment.CENTER);
label2 = new Label();
label2.setText("Input Circles: x1 y1 r1 x2 y2 r2 ");
label2.setTextAlignment(TextAlignment.CENTER);
label3 = new Label();
label3.setTextAlignment(TextAlignment.CENTER);
label4 = new Label();
label4.setTextAlignment(TextAlignment.CENTER);
tField = new TextField();
tField.setOnAction(new TextFieldHandler());
}
public class TextFieldHandler implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent e) {
String str = tField.getText();
int x1, y1, r1, x2, y2, r2;
Scanner scanner = new Scanner(str);
x1 = scanner.nextInt();
y1 = scanner.nextInt();
r1 = scanner.nextInt();
x2 = scanner.nextInt();
y2 = scanner.nextInt();
r2 = scanner.nextInt();
circle1.setCenterX(x1);
circle1.setCenterY(y1);
circle1.setRadius(r1);
circle2.setCenterX(x2);
circle2.setCenterY(y2);
circle2.setRadius(r2);
tField.setText("");
String str1 = String.format("Input is: " + x1 + " " + y1 + " " + r1
+ " " + x2 + " " + y2 + " " + r2);
label3.setText(str1);
double d = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
if ((x1 == x2) & (y1 == y2) & (r1 == r2))
label4.setText(" The circles are equal.");
else if (d >= (r1 + r2))
label4.setText(" The circle interiors are disjoint.");
else if (d <= (r2 - r1))
label4.setText(" Circle1 is inside Circle2.");
else if (d <= (r1 - r2))
label4.setText(" Circle2 is inside Circle1.");
else
label4.setText(" The circles overlap.");
scanner.close();
}
}
public Circle createCircle(int x, int y, int r, Color color) {
Circle circle = new Circle();
circle.setRadius(r);
circle.setCenterX(x);
circle.setCenterY(y);
circle.setStroke(color);
circle.setFill(null);
return circle;
}
}