如何在JavaFX中从一个角点到另一个角点绘制线条?
我想用JavaFX做drawLine0,0,getWidth,getHeight。我不只是想把数字放在我的队伍里。以下是JFrame和JavaFX的代码如何在JavaFX中从一个角点到另一个角点绘制线条?,java,javafx,line,Java,Javafx,Line,我想用JavaFX做drawLine0,0,getWidth,getHeight。我不只是想把数字放在我的队伍里。以下是JFrame和JavaFX的代码 package project; import java.awt.Color; import java.awt.Graphics; import javax.swing.JFrame; import javax.swing.JPanel; public class main extends JPanel{ public static
package project;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class main extends JPanel{
public static void main(String args[])
{
JFrame myWindow = new JFrame("Isometric Grid");
main myPanel = new main(); //myPanel is just a name
myWindow.setSize(400,400);
myWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myWindow.add(myPanel);
myWindow.setVisible(true);
}
//this method will override main and create a custom graphic
public void paintComponent(Graphics myPen) //change
{
super.paintComponent(myPen);
this.setBackground(Color.BLACK);
myPen.setColor(Color.blue);
myPen.drawLine(0, 0, getWidth(), getHeight());
}
}
我不想这样做:
package JavaFXApplication14;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
public class JavaFXApplication14 extends Application {
@Override
public void start(Stage primaryStage) {
Group root = new Group();
Line ln = new Line(0,0,480,480);
ln.setStrokeWidth(1);
root.getChildren().add(ln);
Scene scene = new Scene(root, 480, 480, Color.SKYBLUE);
primaryStage.setTitle("Stuff");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
我的其他问题是如何做getHeight;在JavaFX中的for循环中,类似于:
for(int y = 0; y < getHeight(); y = y + 100)
除了场景的宽度和高度外,您可以执行相同的操作。无法使用该组,因为它会调整自身大小以包含该行。另一个技巧是绑定到宽度和高度,以便调整舞台的大小。因此,在场景被声明后,添加以下两行
Scene scene = new Scene(root, 480, 480, Color.SKYBLUE);
ln.endXProperty().bind(scene.widthProperty());
ln.endYProperty().bind(scene.heightProperty());
如果不想绑定它,只需ln.setEndXscene.getWidth
对于矩形
for(int y = 0; y < scene.getHeight(); y += 20) {
//Rectangle(double x, double y, double width, double height)
Rectangle rect = new Rectangle(20, y, 10, 10);
//default is black fill but you can chamge it;
rect.setFill(Color.RED);
//or in css, same as this
rect.setStyle("-fx-fill: purple;");
//then add it to the Group
root.getChildren().add(rect);
}
您不想使用的代码有什么问题?他在JavaFX案例中有480480个硬编码,但可能会对动态更改窗口大小作出反应。@ArturBiesiadowski:啊,OP使用line有两个截然不同的目的:它是您的代码。我刚刚加了两行。现场……等等。。这一行是你的,在这之后再加上下两行。我就这么做了,谢谢。我还有一个问题。你会如何在这里使用这个概念:forint y=0;y<身高;y=y+100?你的意思是画不止一条线?你想把它们绑在1/100的高度吗?我的意思是像forint y=0;y<身高;y=y+20{myPen.drawRect20,y,10,10;}我会在场景高度属性中添加一个侦听器,并在侦听器中添加矩形。你应该尝试一下,用一些代码问一个新问题。scene.heightProperty.addListenerobs,oldHeight,newHeight->{//使用newHeight做点什么};
for(int y = 0; y < scene.getHeight(); y += 20) {
//Rectangle(double x, double y, double width, double height)
Rectangle rect = new Rectangle(20, y, 10, 10);
//default is black fill but you can chamge it;
rect.setFill(Color.RED);
//or in css, same as this
rect.setStyle("-fx-fill: purple;");
//then add it to the Group
root.getChildren().add(rect);
}