diff options
author | j-james | 2022-12-22 01:36:49 +0000 |
---|---|---|
committer | j-james | 2022-12-22 01:36:49 +0000 |
commit | 0e693fd4814572712a4192cb4f176e71f7a344f7 (patch) | |
tree | 9fb7daba882a025d368775aed9d518f565a40db2 /src/main/ui | |
parent | 35741efd699e67382367411fcb991e2366a6774f (diff) |
Implement the basics of a recursive layout renderer
Diffstat (limited to 'src/main/ui')
-rw-r--r-- | src/main/ui/BrowserCanvas.java | 32 | ||||
-rw-r--r-- | src/main/ui/BrowserWindow.java | 1 |
2 files changed, 31 insertions, 2 deletions
diff --git a/src/main/ui/BrowserCanvas.java b/src/main/ui/BrowserCanvas.java index 1502c3d..a58f6e1 100644 --- a/src/main/ui/BrowserCanvas.java +++ b/src/main/ui/BrowserCanvas.java @@ -3,6 +3,8 @@ package ui; import model.html.ElementNode; import model.html.TextNode; import model.html.Node; +import model.layout.DocumentLayout; +import model.layout.Layout; import javax.swing.*; import java.awt.*; @@ -10,12 +12,22 @@ import java.util.*; public class BrowserCanvas extends JPanel { private ArrayList<Node> html; + private DocumentLayout currentLayout; // MODIFIES: this // EFFECTS: constructs a BrowserCanvas object public BrowserCanvas(ArrayList<Node> html) { super(); this.html = html; + this.currentLayout = Layout.constructTree(html); + printTree(this.currentLayout.getChildren()); + } + + private void printTree(ArrayList<Layout> tree) { + for (Layout node : tree) { + System.out.println(System.identityHashCode(node.getLocation())); + printTree((node).getChildren()); + } } // EFFECTS: paints a component onto a canvas @@ -23,11 +35,11 @@ public class BrowserCanvas extends JPanel { public void paintComponent(Graphics g) { super.paintComponent(g); Point location = new Point(10, 20); // we need a mutable reference - renderHtml(html, g, location); + renderHtml(this.currentLayout.getChildren(), g, location); } // EFFECTS: naively renders our html file by printing text nodes - private void renderHtml(ArrayList<Node> html, Graphics g, Point location) { + /*private void renderHtml(ArrayList<Node> html, Graphics g, Point location) { for (Node node : html) { if (node instanceof TextNode) { if (node.getData().length() > 5) { @@ -38,6 +50,22 @@ public class BrowserCanvas extends JPanel { renderHtml(((ElementNode) node).getChildren(), g, location); } } + }*/ + + private void renderHtml(ArrayList<Layout> tree, Graphics g, Point location) { + for (Layout layout : tree) { +// System.out.println(layout.getLocation()); + g.drawRect(layout.getLocation().x, layout.getLocation().y, layout.getDimension().width, layout.getDimension().height); + if (layout.getAssociatedNode() instanceof TextNode) { + if (layout.getAssociatedNode().getData().length() > 5) { +// System.out.println(location); + g.drawString(layout.getAssociatedNode().getData(), layout.getLocation().x, layout.getLocation().y); + g.drawString("X", 10, 20); + } + } else { + renderHtml(layout.getChildren(), g, location); + } + } } } diff --git a/src/main/ui/BrowserWindow.java b/src/main/ui/BrowserWindow.java index 640fb1a..2375fc1 100644 --- a/src/main/ui/BrowserWindow.java +++ b/src/main/ui/BrowserWindow.java @@ -43,6 +43,7 @@ public class BrowserWindow extends JFrame { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(WIDTH, HEIGHT); render("/home/apropos/Projects/website/j-james/index.html"); +// render("data/example.hctml"); // browserBar.addTab("/home/apropos/Projects/website/j-james/index.html"); setVisible(true); setClosingBehavior(); |