aboutsummaryrefslogtreecommitdiff
path: root/src/main/ui/BrowserCanvas.java
blob: 2a77442c6594967ca599a9ee9d99ad0ad51cd998 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package ui;

import model.html.ElementNode;
import model.html.TextNode;
import model.util.Node;

import javax.swing.*;
import java.awt.*;
import java.util.*;

public class BrowserCanvas extends JPanel {
    private ArrayList<Node> html;

    // MODIFIES: this
    // EFFECTS: constructs a BrowserCanvas object
    public BrowserCanvas(ArrayList<Node> html) {
        super();
        this.html = html;
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Point location = new Point(10, 20); // we need a mutable reference
        renderHtml(html, g, location);
    }

    // EFFECTS: naively renders our html file by printing text nodes
    private void renderHtml(ArrayList<Node> html, Graphics g, Point location) {
        for (Node node : html) {
            if (node instanceof TextNode) {
                if (!node.getData().isBlank()) {
                    g.drawString(node.getData(), location.x, location.y);
                    location.translate(0, 20);
                }
            } else {
                renderHtml(((ElementNode) node).getChildren(), g, location);
            }
        }
    }

}