需要协助使用箭头键绘制线段 [英] 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();
}
推荐答案
我终于明白了,谢谢你不仅仅把答案交给我,但我最终踩到了你的代码并将其分解,看看它是如何工作的...结果,我能够想出一个解决方案......
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);
}
}
这篇关于需要协助使用箭头键绘制线段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文