Skip to content
stefvanschie edited this page Feb 27, 2022 · 2 revisions

Languages: Dutch (Nederlands)

Drawing

Example code for a drawing gui. Players can select a color from the palette on the bottom and draw on the canvas above by clicking with the selected color on each slot.

Code

List<Material> palette = new ArrayList<>();

palette.add(Material.WHITE_STAINED_GLASS_PANE);
palette.add(Material.RED_STAINED_GLASS_PANE);
palette.add(Material.LIGHT_BLUE_STAINED_GLASS_PANE);
palette.add(Material.LIME_STAINED_GLASS_PANE);
palette.add(Material.YELLOW_STAINED_GLASS_PANE);
palette.add(Material.MAGENTA_STAINED_GLASS_PANE);
palette.add(Material.ORANGE_STAINED_GLASS_PANE);
palette.add(Material.PINK_STAINED_GLASS_PANE);
palette.add(Material.BLACK_STAINED_GLASS_PANE);

ChestGui gui = new ChestGui(6, "Drawing");

gui.setOnGlobalClick(event -> event.setCancelled(true));

OutlinePane background = new OutlinePane(0, 0, 9, 6, Pane.Priority.LOWEST);
background.addItem(new GuiItem(new ItemStack(Material.WHITE_STAINED_GLASS_PANE)));
background.setRepeat(true);

StaticPane canvas = new StaticPane(0, 0, 9, 6);

canvas.setOnClick(event -> {
    ItemStack cursor = event.getCursor();

    if (cursor == null || cursor.getType() == Material.AIR) {
        return;
    }

    int slot = event.getSlot();
    int length = canvas.getLength();

    canvas.addItem(new GuiItem(cursor), slot % length, slot / length);

    gui.update();
});

OutlinePane selection = new OutlinePane(0, 6, 9, 1);

for (Material color : palette) {
    selection.addItem(new GuiItem(new ItemStack(color), event -> event.getWhoClicked().setItemOnCursor(new ItemStack(color))));
}

gui.addPane(background);
gui.addPane(canvas);
gui.addPane(selection);

XML

<chestgui title="Drawing" rows="6">
  <outlinepane x="0" y="0" length ="9" height="6" repeat="true" priority="lowest">
    <item id="white_stained_glass_pane" />
  </outlinepane>
  <staticpane x="0" y="0" length ="9" height="6" onClick="canvasClick" />
  <outlinepane x="0" y="6" length="9" height="1">
    <item id="white_stained_glass_pane" onClick="paletteClick" />
    <item id="red_stained_glass_pane" onClick="paletteClick" />
    <item id="light_blue_stained_glass_pane" onClick="paletteClick" />
    <item id="lime_stained_glass_pane" onClick="paletteClick" />
    <item id="yellow_stained_glass_pane" onClick="paletteClick" />
    <item id="magenta_stained_glass_pane" onClick="paletteClick" />
    <item id="orange_stained_glass_pane" onClick="paletteClick" />
    <item id="pink_stained_glass_pane" onClick="paletteClick" />
    <item id="black_stained_glass_pane" onClick="paletteClick" />
  </outlinepane>
</chestgui>
Clone this wiki locally