Skip to content

Lay out the profile panel

In this lesson a layout places the menu buttons for you, then you build the profile panel on the right: three statistic cards and a list of recent rounds.

A dark panel titled Profile with three dark grey cards Games 3, Best 640 and Total 1370, and a list of three rounds under the heading Recent rounds

The result of this lesson: no position inside the column, the grid or the list is computed by hand.

The program of the previous lesson places each button by hand, 100 design pixels apart. Inserting a button means moving every button below it, and the right half of the window is empty.

Replace the three buttons with a FlexNode that holds them:

MenuUI.java
FlexNode
.vertical(160, 420, 360)
.margin(20D)
.body(flex -> {
RectNode.create(0, 0, 360, 80).color(Theme.ELEMENT).label(Text.create("Play", label)).attach(flex);
RectNode.create(0, 0, 360, 80).color(Theme.ELEMENT).label(Text.create("Settings", label)).attach(flex);
RectNode.create(0, 0, 360, 80).color(Theme.ELEMENT).label(Text.create("Quit", label)).attach(flex);
})
.attach(this);

Full file: MenuUI.java

  • FlexNode.vertical(160, 420, 360) is a column 360 design pixels wide at (160, 420); it grows to the height of its children.
  • margin(20D) leaves 20 design pixels between two children.
  • Each button is now created at (0, 0) and attached to flex: the column places them one under the other, in the order you attach them.

Run it. The menu looks the same as before, but you can insert a button anywhere by adding one line.

Add a darker color, SURFACE, to the palette of Theme, for the panel; its cards and its rows take the ELEMENT of the buttons:

Theme.java
package com.example.starfall;
import dev.joid.lib.color.Color;
import dev.joid.lib.font.impl.msdf.MsdfFont;
import dev.joid.lib.font.impl.msdf.MsdfFontLoader;
public final class Theme {
public static final MsdfFont FONT = MsdfFontLoader.load(
"fonts/Montserrat/Montserrat-Regular.ttf",
"fonts/Montserrat/Montserrat-Bold.ttf"
).join();
public static final Color TEXT = new Color(231, 233, 238).lock();
public static final Color ELEMENT = new Color(38, 43, 53).lock();
public static final Color SURFACE = new Color(23, 26, 33).lock();
private Theme() {}
}

The panel needs three more styles: a heading, a caption and a large value. Declare them at the top of init(), with the other styles:

final TextInfo heading = TextInfo.create(Theme.FONT, 40F).weight(FontWeight.BOLD).color(Theme.TEXT);
final TextInfo caption = TextInfo.create(Theme.FONT, 26F).color(Theme.TEXT);
final TextInfo value = TextInfo.create(Theme.FONT, 56F).weight(FontWeight.BOLD).color(Theme.TEXT);

Full file: MenuUI.java

The panel is a rectangle in SURFACE on the right half of the canvas. Add it after the column:

RectNode
.create(760, 160, 1000, 760)
.color(Theme.SURFACE)
.body(panel -> {
TextNode.create(48, 40).text(Text.create("Profile", heading)).attach(panel);
GridNode
.create(48, 120, 904, 150)
.margin(32D)
.body(grid -> {
RectNode
.create(0, 0, 280, 150)
.color(Theme.ELEMENT)
.body(card -> {
TextNode.create(24, 24).text(Text.create("Games", caption)).attach(card);
TextNode.create(24, 64).text(Text.create("3", value)).attach(card);
})
.attach(grid);
RectNode
.create(0, 0, 280, 150)
.color(Theme.ELEMENT)
.body(card -> {
TextNode.create(24, 24).text(Text.create("Best", caption)).attach(card);
TextNode.create(24, 64).text(Text.create("640", value)).attach(card);
})
.attach(grid);
RectNode
.create(0, 0, 280, 150)
.color(Theme.ELEMENT)
.body(card -> {
TextNode.create(24, 24).text(Text.create("Total", caption)).attach(card);
TextNode.create(24, 64).text(Text.create("1370", value)).attach(card);
})
.attach(grid);
})
.attach(panel);
TextNode.create(48, 320).text(Text.create("Recent rounds", heading)).attach(panel);
FlexNode
.vertical(48, 390, 904)
.margin(12D)
.body(flex -> {
final String[][] rounds = {{"Round 3", "412"}, {"Round 2", "640"}, {"Round 1", "318"}};
for (final String[] round : rounds) {
RectNode
.create(0, 0, 904, 72)
.color(Theme.ELEMENT)
.body(row -> {
TextNode
.create(24, row.dh(2D))
.text(Text.create(round[0], caption))
.anchorY(Align.CENTER)
.attach(row);
TextNode
.create(row.aw(-24D), row.dh(2D))
.text(Text.create(round[1], caption))
.anchorX(Align.END)
.anchorY(Align.CENTER)
.attach(row);
})
.attach(flex);
}
})
.attach(panel);
})
.attach(this);

Full file: MenuUI.java

  • body(...) creates the children inside their parent, which the lambda receives (Nodes and the tree).
  • A GridNode, GridNode.create(48, 120, 904, 150), places its children in rows, from left to right, and starts a new row when the next child does not fit in its 904 design pixels. Three cards of 280 with two gaps of 32 fill one row exactly.
  • The recent rounds are a second column: a FlexNode.vertical whose rows are 72 design pixels high, 12 apart. A plain Java loop creates the rows, one per entry of the array.
  • In each row, the points sit against the right edge: row.aw(-24D) is the width of the row minus 24, and Align.END, given to anchorX(...), puts the end of the text on that point. row.dh(2D) is half the height of the row, and anchorY(Align.CENTER) centers each text on it vertically (Position and size).

Run it. You should see the panel with its three cards and its three rounds.

The numbers are written in the code: Play does nothing and the panel never changes. Lesson 5 connects them to real data, after lesson 4 makes the buttons react.

The complete MenuUI.java
package com.example.starfall;
import dev.joid.lib.draw.text.Text;
import dev.joid.lib.font.FontWeight;
import dev.joid.lib.font.TextInfo;
import dev.joid.lib.ui.core.UI;
import dev.joid.lib.ui.node.impl.layout.FlexNode;
import dev.joid.lib.ui.node.impl.layout.GridNode;
import dev.joid.lib.ui.node.impl.shape.RectNode;
import dev.joid.lib.ui.node.impl.text.TextNode;
import dev.joid.lib.utils.align.Align;
public class MenuUI extends UI {
@Override
public void init() {
final TextInfo title = TextInfo.create(Theme.FONT, 120F).weight(FontWeight.BOLD).color(Theme.TEXT);
final TextInfo tagline = TextInfo.create(Theme.FONT, 36F).color(Theme.TEXT);
final TextInfo label = TextInfo.create(Theme.FONT, 36F).color(Theme.TEXT);
final TextInfo heading = TextInfo.create(Theme.FONT, 40F).weight(FontWeight.BOLD).color(Theme.TEXT);
final TextInfo caption = TextInfo.create(Theme.FONT, 26F).color(Theme.TEXT);
final TextInfo value = TextInfo.create(Theme.FONT, 56F).weight(FontWeight.BOLD).color(Theme.TEXT);
TextNode.create(160, 160).text(Text.create("Starfall", title)).attach(this);
TextNode.create(164, 310).text(Text.create("Catch the falling stars", tagline)).attach(this);
FlexNode
.vertical(160, 420, 360)
.margin(20D)
.body(flex -> {
RectNode.create(0, 0, 360, 80).color(Theme.ELEMENT).label(Text.create("Play", label)).attach(flex);
RectNode.create(0, 0, 360, 80).color(Theme.ELEMENT).label(Text.create("Settings", label)).attach(flex);
RectNode.create(0, 0, 360, 80).color(Theme.ELEMENT).label(Text.create("Quit", label)).attach(flex);
})
.attach(this);
RectNode
.create(760, 160, 1000, 760)
.color(Theme.SURFACE)
.body(panel -> {
TextNode.create(48, 40).text(Text.create("Profile", heading)).attach(panel);
GridNode
.create(48, 120, 904, 150)
.margin(32D)
.body(grid -> {
RectNode
.create(0, 0, 280, 150)
.color(Theme.ELEMENT)
.body(card -> {
TextNode.create(24, 24).text(Text.create("Games", caption)).attach(card);
TextNode.create(24, 64).text(Text.create("3", value)).attach(card);
})
.attach(grid);
RectNode
.create(0, 0, 280, 150)
.color(Theme.ELEMENT)
.body(card -> {
TextNode.create(24, 24).text(Text.create("Best", caption)).attach(card);
TextNode.create(24, 64).text(Text.create("640", value)).attach(card);
})
.attach(grid);
RectNode
.create(0, 0, 280, 150)
.color(Theme.ELEMENT)
.body(card -> {
TextNode.create(24, 24).text(Text.create("Total", caption)).attach(card);
TextNode.create(24, 64).text(Text.create("1370", value)).attach(card);
})
.attach(grid);
})
.attach(panel);
TextNode.create(48, 320).text(Text.create("Recent rounds", heading)).attach(panel);
FlexNode
.vertical(48, 390, 904)
.margin(12D)
.body(flex -> {
final String[][] rounds = {{"Round 3", "412"}, {"Round 2", "640"}, {"Round 1", "318"}};
for (final String[] round : rounds) {
RectNode
.create(0, 0, 904, 72)
.color(Theme.ELEMENT)
.body(row -> {
TextNode
.create(24, row.dh(2D))
.text(Text.create(round[0], caption))
.anchorY(Align.CENTER)
.attach(row);
TextNode
.create(row.aw(-24D), row.dh(2D))
.text(Text.create(round[1], caption))
.anchorX(Align.END)
.anchorY(Align.CENTER)
.attach(row);
})
.attach(flex);
}
})
.attach(panel);
})
.attach(this);
}
}

Add a Credits button between Settings and Quit, and space the buttons 40 design pixels apart.

Solution
FlexNode
.vertical(160, 420, 360)
.margin(40D)
.body(flex -> {
RectNode.create(0, 0, 360, 80).color(Theme.ELEMENT).label(Text.create("Play", label)).attach(flex);
RectNode.create(0, 0, 360, 80).color(Theme.ELEMENT).label(Text.create("Settings", label)).attach(flex);
RectNode.create(0, 0, 360, 80).color(Theme.ELEMENT).label(Text.create("Credits", label)).attach(flex);
RectNode.create(0, 0, 360, 80).color(Theme.ELEMENT).label(Text.create("Quit", label)).attach(flex);
})
.attach(this);

Full file: ChallengeUI.java

One setter changes every gap, and the new button takes its place by being attached third.

  • A layout node places its children, so they are created at (0, 0).
  • A flex layout stacks its children in a row or a column, with a margin between them.
  • A grid layout fills rows and wraps to the next one.
  • aw(...) and anchors place a node against the edge of its parent (Position and size).

Report a problem with this page