嘿,我对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中的那些项目之下。
发布于 2015-10-06 00:48:52
您的红色圆(颜色为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;
}
}https://stackoverflow.com/questions/32958310
复制相似问题