我通过添加一个基本代码来了解它是如何工作的,开始执行第 3 项任务。但我无法摆脱这个问题。我只是添加了一个“if”,这样如果输入文本等于“hr”,那么海龟每次都会向右移动 2 个方格。但是当我运行代码时,就好像它只检查第一个字符。如果前两个字符是“hr”,那么它标记了一个点,但如果不是,它就不再检查输入。例如,如果我写:
re
Fd
hr
即使“hr”在那里,它也永远不会标记点。我该怎么做才能让 TurtleRenderer 在每次插入 \n 时读取该行,而不仅仅是在代码运行时读取?
我的代码:
package turtle;
public class BoardMaker {
private static int MAX = 100;
private boolean[][] board = new boolean[MAX][MAX];
int previousX = 0;
int previousY = 0;
public boolean[][] makeBoardFrom(String description) {
if(description.contentEquals("hr")){
previousX+=2;
board[previousX][previousY]=true;
}
return board;
}
public boolean[][] initialBoard() {
for(int i=0;i<MAX;i++)
{
for(int j=0;j<MAX;j++)
board[i][j]=false;
}
return board;
}
}
TurtleRenderer 类:
package turtle;
public class TurtleRenderer extends Panel implements DocumentListener {
private static final long serialVersionUID = 1;
static final Dimension WINDOW_SIZE = new Dimension(1150, 1150);
boolean [][] board;
final BoardMaker boardMaker;
public TurtleRenderer() {
boardMaker = new BoardMaker();
board = boardMaker.initialBoard();
}
static public void main(String args[]) throws Exception {
JFrame frame = new JFrame("Display image");
JPanel panel = new JPanel();
TurtleRenderer image = new TurtleRenderer();
image.setPreferredSize(WINDOW_SIZE);
JScrollPane textArea = makeTextArea(image);
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
panel.add(image);
buildRightPanel(panel, textArea);
frame.setSize(WINDOW_SIZE);
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}});
frame.getContentPane().add(panel);
frame.setVisible(true);
}
static void buildRightPanel(JPanel panel,JComponent textArea) {
JLabel label = new JLabel("Your program:");
label.setPreferredSize(new Dimension(150, 20));
JPanel right = new JPanel();
textArea.setPreferredSize(new Dimension(150,500));
right.setLayout(new BoxLayout(right, BoxLayout.Y_AXIS));
right.add(label);
right.add(textArea);
panel.add(right);
}
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
g2d.setColor(Color.white);
g.fillRect(0, 0, WINDOW_SIZE.width, WINDOW_SIZE.width);
if(board == null)
return;
g2d.setColor(Color.red);
for(int i=0;i<board.length;i++) {
for(int j=0;j<board.length;j++) {
if(board[i][j])
g2d.fillRect(9*i+1, 9*j+1, 6, 6);
}
}
}
static JScrollPane makeTextArea(TurtleRenderer image) {
JTextArea textArea = new JTextArea();
textArea.getDocument().addDocumentListener(image);
textArea.setVisible(true);
JScrollPane areaScrollPane = new JScrollPane(textArea);
areaScrollPane.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
areaScrollPane.setBorder(BorderFactory.createLineBorder(Color.black));
return areaScrollPane;
}
@Override
public void insertUpdate(DocumentEvent e) {
changed(e);
}
@Override
public void removeUpdate(DocumentEvent e) {
changed(e);
}
@Override
public void changedUpdate(DocumentEvent e) {
changed(e);
}
void changed(DocumentEvent de) {
String description;
Document document = de.getDocument();
try {
description = document.getText(0, document.getLength());
} catch (BadLocationException e) {
throw new RuntimeException(e);
}
try {
board = boardMaker.makeBoardFrom(description);
} catch(ParserException pe) {
board = null;
}
this.repaint();
}
}