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度順時針移動+或逆時針移動 - (默認向上移動) 。
例如,如果用戶輸入:「hhh」,該線將向上移動30個像素。
我遇到的問題是我希望用戶能夠輸入hhh + hhh,並且它向上繪製一個30像素的線,然後轉動並繪製30個像素,但是向右。但是在我的程序中,當我輸入hhh + hhh時,它只是向右一個60像素的直線。
所以我的問題是:如何在指定的方向上看到+或 - 並繪製另一條線時,如何繪製線。
你錯過了一些代碼嗎?你從來沒有宣佈「標題」變量,除非我是瞎子 – ChrisK 2013-02-22 00:36:20
以及我沒有包括我的完整代碼,但現在粘貼它。 - 完成 – Mert 2013-02-22 00:38:23
'... main(args); ...'這是一個非常糟糕的主意,請使用某種類型的循環 – MadProgrammer 2013-02-22 01:03:43