Java 用画布画多条线 publicstaticvoidmain(字符串[]args){ 图片myPic=新图片(600600); Graphics canvas=myPic.getOffScreengGraphics(); canvas.setColor(Color.WHITE); canvas.fillRect(0,060600); canvas.setColor(Color.BLACK); int-x1; int y1; int-x2; int y2; int标题; x1=300; y1=300; x2=300; y2=300; 航向=0; 字符串键盘=JOptionPane.showInputDialog(null, “输入您的输入”); 如果(键盘!=null){ if(keyboard.isEmpty()| | keyboard.contains(“”){ JOptionPane.showMessageDialog( 无效的 “请输入输入”, “错误”, JOptionPane.ERROR\u消息); main(args); } int counth=0; int countg=0; int countPlus=0; int count减数=0; int countK=0; int countR=0; int countG=0; int countB=0; int countC=0; int countO=0; 对于(int i=0;i

Java 用画布画多条线 publicstaticvoidmain(字符串[]args){ 图片myPic=新图片(600600); Graphics canvas=myPic.getOffScreengGraphics(); canvas.setColor(Color.WHITE); canvas.fillRect(0,060600); canvas.setColor(Color.BLACK); int-x1; int y1; int-x2; int y2; int标题; x1=300; y1=300; x2=300; y2=300; 航向=0; 字符串键盘=JOptionPane.showInputDialog(null, “输入您的输入”); 如果(键盘!=null){ if(keyboard.isEmpty()| | keyboard.contains(“”){ JOptionPane.showMessageDialog( 无效的 “请输入输入”, “错误”, JOptionPane.ERROR\u消息); main(args); } int counth=0; int countg=0; int countPlus=0; int count减数=0; int countK=0; int countR=0; int countG=0; int countB=0; int countC=0; int countO=0; 对于(int i=0;i,java,graphics,drawing,awt,2d,Java,Graphics,Drawing,Awt,2d,我正试图根据用户输入画一些线 输入为h和f,用于向前移动10个像素(两者的作用相同)和+或-为了改变方向,直线将顺时针移动90度,用于+或逆时针移动-(默认为向上移动) 例如,如果用户输入:“hhh”,该行将向上移动30个像素 我遇到的问题是,我希望用户能够输入hhh+hhh,然后它向上画一条30像素的线,然后转向右侧再画30个像素。然而,在我的程序中,当我输入hhh+hhh时,它只在右边60像素处形成一条直线 所以我的问题是:当它在指定的方向上看到+或-并画另一条线时,如何让它画一个线转弯 代

我正试图根据用户输入画一些线

输入为h和f,用于向前移动10个像素(两者的作用相同)和+或-为了改变方向,直线将顺时针移动90度,用于+或逆时针移动-(默认为向上移动)

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

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


所以我的问题是:当它在指定的方向上看到+或-并画另一条线时,如何让它画一个线转弯

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

删除+和-

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);
    }


}
for(int i=0;i3)
航向=0;
if(keyboard.charAt(i)='h'| | keyboard.charAt(i)='f')
//剪掉标题代码的其余部分
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
 }