Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I make the DocumentListener compare after every line and not only once the program is run?
    primarykey
    data
    text
    <p>I started doing the assignment number #3 by just adding a basic code to understand how it works. But I can’t get out of this problem. I just added an “if” so that if the input text is equal to “hr”, then the turtle would move 2 squares to the right every time. But when I run the code, it is as if it only checks the first characters. If the first two characters are “hr” then it marks a point, but if not, it never again checks the input. So for example if I write:</p> <pre><code>re Fd hr </code></pre> <p>It never marks the point even though “hr” is there. What can I do so that the TurtleRenderer reads the line every time a \n is inserted and not only once the code is run?</p> <p>My code:</p> <pre><code>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&lt;MAX;i++) { for(int j=0;j&lt;MAX;j++) board[i][j]=false; } return board; } } </code></pre> <p>The TurtleRenderer class:</p> <pre><code>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&lt;board.length;i++) { for(int j=0;j&lt;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(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload