1

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:

re
Fd
hr

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?

My 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<MAX;i++)
        {
            for(int j=0;j<MAX;j++)
                board[i][j]=false;
        }
        return board;       
    }
}

The TurtleRenderer class:

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();
    }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373

3 Answers3

1

Your problem is that you're currently testing if the whole text held by the JTextArea holds "hr". This may be true if hr is the only text in the JTextArea, but once more text is added, this will always be false. What you need to check is if the last line is "hr".

Since this is homework, I won't post a solution, but a pseudo-code logic solution for your DocumentListener could be:

try
   get the text String from the document
   get the last char from this String
   if this last Char == carriage return which is (char)10
      split the text into lines using the carriage return as the split's delimiter
      get the last line held by this array and check it
      if it is hr do something
   end if last char == carriage return
end try
catch for BadLocationException 
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
0

From the Javadocs,

public boolean contentEquals(CharSequence cs)

Compares this string to the specified CharSequence. 
The result is true if and only if this String represents the same sequence of char values as the specified sequence.

public boolean contains(CharSequence s)

Returns true if and only if this string contains the specified sequence of char values.

Thus String.contentEquals will function more of a type of String.equals method. There are some differences though.

As with this problem you would require String.contains method to check whether the text contains the String "hr"


One more advice with regards to code efficiency :

You don't have to perform any action in the changedUpdate(DocumentEvent e) method within the DocumentListener. This method is only called when an attribute or a set of attributes has changed, i.e. the style of the document has changed which is not possible in a JTextArea as it does not support styled text.

I hope I have understood your problem correctly.

Community
  • 1
  • 1
Extreme Coders
  • 3,441
  • 2
  • 39
  • 55
0

First, as in a previous comment, the method makeBoardFrom will every time receive the entire program. If is up to you to split that program into individual commands, and then execute each command. You should not attempt to change the TurtleRenderer class to behave differently.

Second, if you want to move the turtle to the left by two squares you have to mark both squares as visited, not just the destination square. Right now in your solution, by only using previousX+=2; you only mark the destination square as visited.

Third, in the initialBoard method you also have to actually mark the initial square of the turtle with true. In your case that will be the square at position (0, 0).

Community
  • 1
  • 1