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度的方向+或逆时针移动 - (默认为向上移动)。
例如,如果用户输入:“hhh”,该行将向上移动30个像素。
我遇到的问题是我希望用户能够输入hhh + hhh并且它向上绘制一条30像素的线然后转向并向右绘制30多个像素。但是在我的程序中,当我输入hhh + hhh时,它只是向右一条直线60像素。
所以我的问题是:当它在指定的方向上看到+或 - 并绘制另一条线时,我怎么能让它画一条线。
答案 0 :(得分: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
}
您遇到的问题是您多次循环遍历代码,而不依赖于处理线条绘制代码。
虽然这可能无法解决您的问题,但可能会让您走上正轨。