简体   繁体   English

需要帮助使用箭头键绘制线段

[英]Need assistance drawing line segments using arrow keys

Now all I get are the dots, but with no lines connecting them.... Should I be re-creating the original "line" that is initialized before my switch? 现在我得到的只是点,但是没有线连接它们。...我应该重新创建在切换之前初始化的原始“线”吗? Or should I not be creating new lines at all but rather re-modifying the original line? 还是我根本不应该创建新行,而是重新修改原始行?

public class DrawLineWithArrowKeys extends Application {
    double bX = 150;
    double bY = 150;
    double eX = 150;
    double eY = 150;
    double segment = 20;
@Override
public void start(Stage primaryStage) {
Pane myPane = new Pane();

Line line = new Line(bX,bY,eX,eY);
line.setStrokeWidth(2);
line.setStroke(Color.RED);
myPane.getChildren().add(line);

line.setOnKeyPressed(e -> {
 if (e.getCode().isArrowKey())
 {
    switch (e.getCode()) {

    case DOWN:
        bY += segment;
        eY = bY;
        Line line2 = new Line(bX,bY,eX,eY);
        line2.setStrokeWidth(2);
        line2.setStroke(Color.GREEN);
        myPane.getChildren().add(line2);
        break;

    case UP:
        bY -= segment;
        eY = bY;
        Line line3 = new Line(bX,bY,eX,eY);
        line3.setStrokeWidth(2);
        line3.setStroke(Color.GREEN);
        myPane.getChildren().add(line3);
        break;

    case LEFT: 
        bX -= segment;
        eX = bX;
        Line line4 = new Line(bX,bY, eX, eY);
        line4.setStrokeWidth(2);
        line4.setStroke(Color.GREEN);
        myPane.getChildren().add(line4);
        break;

    case RIGHT: 
        bX += segment;
        eX = bX;
        Line line5 = new Line(bX,bY,eX,eY);
        line5.setStrokeWidth(2);
        line5.setStroke(Color.GREEN);
        myPane.getChildren().add(line5);
        break;

        default: 



        break;
    }
 }
});


Scene scene = new Scene(myPane, 300, 300);
primaryStage.setTitle("Draw Line with Arrow Keys");
primaryStage.setScene(scene);
primaryStage.show();
line.requestFocus();

} }

You can use Path with its PathElement s: 您可以将Path及其PathElement一起使用:

private int bX = 75, bY = 75;
private int step = 20;


@Override
public void start( Stage primaryStage )
{

    Pane myPane = new Pane();
    Path path = new Path( new MoveTo( bX, bY ) ); // move to starting point
    path.setStrokeWidth( 1 );
    path.setStroke( Color.GREEN );

    myPane.getChildren().add( path );

    path.setOnKeyPressed( e ->
    {
        if ( e.getCode().isArrowKey() )
        {
            switch (e.getCode())
            {
                case DOWN:
                    bY += step;
                    break;

                case UP:
                    bY -= step;
                    break;

                case LEFT:
                    bX -= step;
                    break;

                case RIGHT:
                    bX += step;
                    break;
            }
            path.getElements().add( new LineTo( bX, bY ) );
        }
    } );

    Scene scene = new Scene( myPane, 150, 150 );
    primaryStage.setTitle( "Draw Line with Arrow Keys" );
    primaryStage.setScene( scene );
    primaryStage.show();
    path.requestFocus();
}

I am not a fan of providing ready code for homework like questions. 我不喜欢为问题之类的作业提供现成的代码。 But at least it seems you have tried to implement. 但至少看来您已尝试实现。

I finally figured it out, thank you for not just handing the answer to me, but I did end up stepping through your code and breaking it down to see how it worked... As a result, I was able to come up with a solution to the assignment... 我终于弄清楚了,谢谢您不仅向我提供了答案,而且最终我确实逐步浏览了您的代码并将其分解以查看其工作原理...结果,我得以提出一个解决方案。解决方案...

package skowronek15;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;



public class DrawLineWithArrowKeys extends Application {
double bX = 150; //start x value denote for beginning-X
double bY = 150; //start y value denote for beginning-y
double eX = 150; //start x value denote for end-x
double eY = 150; //start y value denote for end-y
double bX2, bY2; //start of next line
double segment = 20; //length of segment


@Override
public void start(Stage primaryStage) {
    Pane myPane = new Pane(); //create a pane


    Line line = new Line(bX,bY,eX,eY); //create the starting or initial line
    line.setStrokeWidth(2); //set width
    line.setStroke(Color.RED); //set color
    myPane.getChildren().add(line); //add line to pane

    line.setOnKeyPressed(e -> { //switch statement to determine what happens when
                                // each key is pressed
     if (e.getCode().isArrowKey())
     {
        switch (e.getCode()) {

        case DOWN:
            bY2 = bY ; //start of next line, bY2 is equal to the start of originating
                       // y-coordinate bY
            bY += segment;//increment start of first line
            eY = bY; // end y (eY) coordinate equal to beginning y coordinate (bY) after 
                    // it's incremented;


            Line line2 = new Line(bX,bY2,eX,eY); //create a new line to be drawn;
                                //beginning y coordinate is now bY2;
            line2.setStrokeWidth(2);
            line2.setStroke(Color.GREEN);
            myPane.getChildren().add(line2);
            break;

        case UP:
            bY2 = bY;
            bY -= segment;
            eY = bY;

            Line line3 = new Line(bX,bY2,eX,eY);
            line3.setStrokeWidth(2);
            line3.setStroke(Color.GREEN);
            myPane.getChildren().add(line3);
            break;

        case LEFT:
            bX2 = bX;
            bX -= segment;
            eX = bX;

            Line line4 = new Line(bX2,bY, eX, eY);
            line4.setStrokeWidth(2);
            line4.setStroke(Color.GREEN);
            myPane.getChildren().add(line4);
            break;

        case RIGHT: 
            bX2 = bX;
            bX += segment;
            eX = bX;
            Line line5 = new Line(bX2,bY,eX,eY);
            line5.setStrokeWidth(2);
            line5.setStroke(Color.GREEN);
            myPane.getChildren().add(line5);
            line5.getStrokeLineJoin();
            break;

            default:


            break;
        }
     }
    });


    Scene scene = new Scene(myPane, 300, 300);
    primaryStage.setTitle("Draw Line with Arrow Keys");
    primaryStage.setScene(scene);
    primaryStage.show();
    line.requestFocus();



}






public static void main(String[] args) {
    launch(args);
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM