简体   繁体   English

Java程序中的对角线和直线图

[英]diagonal lines and straight line drawing in a java program

in the program below you can see that i've allowed the input of the user to give a direction such as n100 which will draw a line north and move it 100 spaces but How am I able to allow the sketch program to do diagonal lines as well as straight lines , I understand I am able to change the input to (0,2) to allow diagonal lines by using something like ne but then my program doesn't like when I use directions such as n, e, s, w. 在下面的程序中,您可以看到我已经允许用户输入一个方向,例如n100,它将向北画一条线并将其移动100个空格,但是我如何允许素描程序像以及直线 ,我知道我可以通过使用诸如ne之类的东西来将输入更改为(0,2)以允许对角线,但是当我使用诸如n,e,s,w之类的方向时,我的程序不喜欢。 What can I do to allow both lines? 我该怎么做才能允许两条线? this is the code below: 这是下面的代码:

在此处输入图片说明

boolean okToProcess = true;
    String message = "";
    int colourInt;

    String input = in.getText();
    String direction = input.substring(0, 1);
    String distance = input.substring(1);

    double distanceAsDouble = 0;



    if (direction.equals("n"))
        t.heading(0);
    else if (direction.equals("ne"))
        t.heading(45);
    else if (direction.equals("e"))
        t.heading(90);
    else if (direction.equals("se"))
        t.heading(135);
    else if (direction.equals("s"))
        t.heading(180);
    else if (direction.equals("sw"))
        t.heading(225);
    else if (direction.equals("w"))
        t.heading(270);
    else if (direction.equals("nw"))
        t.heading(315);
    else {
        okToProcess = false;
        message += "bad direction: " + direction + " ";
    }


    if (isNumeric(distance)) {
        distanceAsDouble = Double.parseDouble(distance);
    }
    else{
        okToProcess = false;
        message += "bad distance: " + distance;
    }

    if (okToProcess) {
        if (!EtchASketchClipped(t, distanceAsDouble)) {
            t.setLineWidth(3);

If you use 如果您使用

String direction = input.substring(0, 1);
String distance = input.substring(1);

you are storing and comparing only the first character of the string and eventually assign an invalid number to distance, as if the direction is diagonally, the second character is prepended to direction. 您将只存储和比较字符串的第一个字符,并最终为距离分配一个无效的数字,就好像方向是对角线一样,第二个字符将放在该方向的前面。 Use String.startsWith() to check for a given direction. 使用String.startsWith()检查给定方向。 Inside the if-statement, decide wether to start the distance at the second or third character. 在if语句中,决定是否以第二个或第三个字符开始距离。 You can also just use input as a value to check. 您也可以只使用输入作为值进行检查。

...
String distance ;
double distanceAsDouble = 0;

if (input.startsWith("n")) {
    t.heading(0);
    distance = input.substring(1);
} else if (input.startsWith("ne")) {
    t.heading(45);
     distance = input.substring(2);
} else if ...
String direction = input.replaceFirst("^(\\D*)(.*)$", "$1");
String distance = input.replaceFirst("^(\\D*)(.*)$", "$2").trim();

Where the regular expression means 正则表达式的含义

  • \\\\D matches non-digit \\\\D匹配非数字
  • \\\\d matches digit \\\\d匹配数字
  • postfix * for 0 or more 后缀* 0或更多
  • . for any char 对于任何字符
  • ^ begin ^开始
  • $ end $结束
  • () group numbered from 1 ()组从1开始编号
  • $1 group 1 $ 1组1

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

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