blob: 534fe91a37275c8d9129aeff47377db27ee70fb1 (
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
|
package ui;
import model.html.*;
import model.layout.*;
import javax.swing.*;
import java.awt.*;
import java.util.*;
public class BrowserCanvas extends JPanel {
private final DocumentLayout currentLayout;
public BrowserCanvas(ArrayList<Node> html) {
super();
this.currentLayout = Layout.constructTree(html);
this.setBackground(Color.BLACK);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
g.setColor(Color.WHITE);
renderHtml(this.currentLayout.children, g);
}
private void renderHtml(ArrayList<Layout> tree, Graphics g) {
for (Layout layout : tree) {
g.setColor(new Color((int)(Math.random() * 0x1000000)));
g.drawRect(layout.location.x, layout.location.y, layout.dimension.width, layout.dimension.height);
g.setColor(Color.WHITE);
if (layout.associatedNode instanceof TextNode t) {
g.drawString(t.text(), layout.location.x, layout.location.y + layout.dimension.height - 5);
} else {
renderHtml(layout.children, g);
}
}
}
}
|