0
public static void main(String[] args) {
    Picture myPic = new Picture(600, 600);
    Graphics canvas = myPic.getOffScreenGraphics();

    canvas.setColor(Color.WHITE);
    canvas.fillRect(0, 0, 600, 600);

    canvas.setColor(Color.BLACK);
    int x1;
    int y1;
    int x2;
    int y2;
    int heading;

    x1 = 300;
    y1 = 300;
    x2 = 300;
    y2 = 300;

    heading = 0;



    String keyboard = JOptionPane.showInputDialog(null,
            "Enter your input");

    if (keyboard != null) {
        if (keyboard.isEmpty() || keyboard.contains(" ")) {
            JOptionPane.showMessageDialog(
                    null,
                    "Please enter an input",
                    "Error",
                    JOptionPane.ERROR_MESSAGE);
            main(args);
        }


        int counth = 0;
        int countg = 0;
        int countPlus = 0;
        int countMinus = 0;
        int countK = 0;
        int countR = 0;
        int countG = 0;
        int countB = 0;
        int countC = 0;
        int countO = 0;


        for (int i = 0; i < keyboard.length(); i++) {
            if (keyboard.charAt(i) == '+') {
                if (heading == 0) {
                    heading = 1;
                } else if (heading == 1) {
                    heading = 2;
                } else if (heading == 2) {
                    heading = 3;
                } else if (heading == 3) {
                    heading = 0;
                }
            }
        }
        countPlus++;

        for (int i = 0; i < keyboard.length(); i++) {
            if (keyboard.charAt(i) == '-') {
                if (heading == 0) {
                    heading = 3;
                } else if (heading == 1) {
                    heading = 0;
                } else if (heading == 2) {
                    heading = 1;
                } else if (heading == 3) {
                    heading = 2;
                }
            }
        }
        countMinus++;


        for (int i = 0; i < keyboard.length(); i++) {
            if (keyboard.charAt(i) == 'h' || keyboard.charAt(i) == 'f') {
                if (heading == 0) {
                    counth++;
                    int k = 10 * (counth);
                    canvas.drawLine(x1, y1, x2, y2 + k);

                } else if (heading == 1) {
                    counth++;
                    int k = 10 * (counth);
                    canvas.drawLine(x1, y1, x2 + k, y2);
                } else if (heading == 2) {
                    counth++;
                    int k = 10 * (counth);
                    canvas.drawLine(x1, y1, x2, y2 - k);
                } else if (heading == 3) {
                    counth++;
                    int k = 10 * (counth);
                    canvas.drawLine(x1, y1, x2 - k, y2);
                } else if (heading > 3 || heading < 0) {
                    JOptionPane.showMessageDialog(
                            null,
                            "Your heading is greater than 3 or less than 0",
                            "Error",
                            JOptionPane.ERROR_MESSAGE);
                    main(args);
                }
            }

        }



        for (int i = 0; i < keyboard.length(); i++) {
            if (keyboard.charAt(i) == 'K') {
                canvas.setColor(Color.BLACK);
            }
        }
        countK++;

        for (int i = 0; i < keyboard.length(); i++) {
            if (keyboard.charAt(i) == 'R') {
                canvas.setColor(Color.RED);
            }
        }
        countR++;

        for (int i = 0; i < keyboard.length(); i++) {
            if (keyboard.charAt(i) == 'G') {
                canvas.setColor(Color.GREEN);
            }
        }
        countG++;

        for (int i = 0; i < keyboard.length(); i++) {
            if (keyboard.charAt(i) == 'B') {
                canvas.setColor(Color.BLUE);
            }
        }
        countB++;

        for (int i = 0; i < keyboard.length(); i++) {
            if (keyboard.charAt(i) == 'C') {
                canvas.setColor(Color.CYAN);
            }
        }
        countC++;

        for (int i = 0; i < keyboard.length(); i++) {
            if (keyboard.charAt(i) == 'O') {
                canvas.setColor(Color.BLACK);
            }
        }
        countO++;


        System.out.println("h = " + counth);
        System.out.println("g = " + countg);
        System.out.println("+ = " + countPlus);
        System.out.println("- = " + countMinus);
        System.out.println("K = " + countK);
        System.out.println("R = " + countR);
        System.out.println("G = " + countG);
        System.out.println("B = " + countB);
        System.out.println("C = " + countC);
        System.out.println("O = " + countO);



        myPic.repaint();
    } else {
        System.exit(0);
    }


}

我正在尝试根据用户输入绘制一些线条。

输入是 h 和 f 用于向前移动 10 个像素(两者都做同样的事情)和 + 或 - 来改变线将移动 90 度的方向,无论是顺时针移动 90 度,还是逆时针移动 -(默认是向上移动)。

例如,如果用户输入:“hhh”,则该行将向上移动 30 个像素。

我遇到的问题是我希望用户能够输入 hhh+hhh 并向上绘制 30 个像素的线,然后转向并在右侧绘制 30 个像素。然而,在我的程序中,当我输入 hhh+hhh 时,它只会使一条直线向右 60 像素。

所以我的问题是:当它在指定方向看到 + 或 - 并画另一条线时,我怎样才能让它画一条线。

4

1 回答 1

0

代码完全未经测试,但我建议你:

删除 + 和 - 的两个循环。

for(int i =0; i < keyboard.length(); i++)
  {
 if(keyboard.charAt(i) == '+')
      heading++;
 if(keyboard.charAt(i) == '-')
      heading--;

if (heading < 0)
    heading = 3;
if (heading > 3)
    heading = 0;

 if(keyboard.charAt(i) == 'h' || keyboard.charAt(i) == 'f') 
 //Snip rest of heading code here
 }

您遇到的问题是您多次循环代码,而与处理线条图代码无关。

虽然这可能无法解决您的问题,但它可能会让您走上正轨。

于 2013-02-22T00:49:00.947 回答