Interface Kit: drawing shapes in a view

Interface Kit: drawing shapes in a view

  • mdo  DigitalBox
  •   KitLab
  •   September 29, 2025

In this KitLab article, we’ll explore how to draw custom graphics in a Haiku application using the Draw() method provided by the BView class.

Whether you're building a user interface or a visual art application, Draw() is your entry point into Haiku's 2D rendering system.

We'll walk through a minimal example - drawing a centered yellow circle with a black outline - to a more complex example with M.C. Escher-Inspired Patterns.

How drawing works in Haiku

To draw anything on screen in Haiku, you subclass BView and override its Draw() method.

Draw() is automatically called by the system whenever the view needs repainting—such as during window exposure, resizing, or programmatic invalidation using Invalidate().

Full Source Code: DrawApp.cpp

#include <Application.h>
#include <Window.h>
#include <View.h>

class CircleView : public BView
{
public:
    CircleView(BRect frame)
        :
        BView(frame, "circle_view", B_FOLLOW_NONE, B_WILL_DRAW)
    {
        SetViewColor(255, 255, 255);
    }

    void Draw(BRect) override
    {
        BRect bounds = Bounds();

        // Compute circle center and radius based on view size
        BPoint center((bounds.left + bounds.right) / 2.0f,
            (bounds.top + bounds.bottom) / 2.0f);
        float radius = std::min(bounds.Width(), bounds.Height()) / 4.0f;

        // Filled yellow circle
        SetHighColor(255, 255, 200);
        FillEllipse(BRect(center.x - radius, center.y - radius,
            center.x + radius, center.y + radius));

        // Outline
        SetHighColor(0, 0, 0);
        StrokeEllipse(BRect(center.x - radius, center.y - radius,
            center.x + radius, center.y + radius));
    }
};

class CircleWindow : public BWindow
{
public:
    CircleWindow(BRect f)
        :
        BWindow(f, "Simple Yellow Circle", B_TITLED_WINDOW, B_QUIT_ON_WINDOW_CLOSE)
    {
        AddChild(new CircleView(Bounds()));
    }

    bool QuitRequested() override
    {
        be_app->PostMessage(B_QUIT_REQUESTED);
        return true;
    }
};

class DrawApp : public BApplication
{
public:
    DrawApp()
        :
        BApplication("application/x-vnd.example-draw")
    {
        BRect r(100, 100, 400, 400);
        CircleWindow* w = new CircleWindow(r);
        w->Show();
    }
};

int main()
{
    DrawApp app;
    app.Run();
    return 0;
}

What It Does

When this app runs, it opens a window containing a custom view.

Inside that view:

  • It calculates the center of the view and a radius based on the window size.
  • It draws a yellow filled circle using FillEllipse().
  • Then it draws a black outline around it using StrokeEllipse()

The Draw() method ensures that the shape is redrawn any time Haiku decides it needs to repaint the view — for instance, if the window is resized or uncovered.

In the Draw() method:

  • Bounds() gives the current size of the view.
  • Center() computes the center point.
  • A radius is calculated as one-quarter the smaller dimension.
  • A filled yellow circle is drawn with FillEllipse().
  • A black border is drawn with StrokeEllipse().

Compile and Run

If you’re using Haiku, open Terminal and compile the code with:

g++ -o DrawApp DrawApp.cpp -lbe
./DrawApp

You’ll see a simple window with a centered yellow circle on a white background.

Why This Matters

Understanding how Draw() works is essential for:

  • Creating custom UI widgets
  • Building graphical tools or visualizers
  • Designing interactive artwork or simple games

The BView class provides a range of drawing primitives:

  • StrokeLine(), StrokeRect(), StrokePolygon()…
  • FillRect(), FillEllipse()…
  • Plus text drawing, colors, clipping, and more.

Escher-inspired tiling with polygons

The Draw() method doesn’t just let you draw simple shapes like circles or rectangles — it’s also your gateway to creating complex visual patterns. 

In fact, many of M.C. Escher’s most iconic artworks can be reimagined with nothing more than geometry, color, and repetition inside Draw().

Here’s a practical example: an Escher-inspired tiling, using a manually designed polygon that roughly resembles a stylized creature or interlocking tile.

We fill the screen by arranging this shape in a grid and moving the pattern across rows and columns.

Example: Escher-Like Pattern

#include <Application.h>
#include <Window.h>
#include <View.h>
#include <Polygon.h>
#include <math.h>

class EscherView : public BView
{
public:
    EscherView(BRect frame)
        :
        BView(frame, "escher_view", B_FOLLOW_ALL, B_WILL_DRAW)
    {
        SetViewColor(255, 255, 255);
    }

    void Draw(BRect) override
    {
        float size = 10;
        float dx = size * 8 * 2;
        float dy = size * 8;

        // Compute grid size based on window bounds
        int rows = (int)(Bounds().Height() / dy) + 2;
        int cols = (int)(Bounds().Width() / dx) + 2;

        for (int row = 0; row < rows; ++row) {
            for (int col = 0; col < cols; ++col) {
                int pattern = (row + col) % 4;                  // Cycle through four colors
                float x = col * dx + ((row % 2) * dx / 2);      // Offset every other row for tiling effect
                float y = row * dy;
                DrawPattern(BPoint(x, y), size, pattern);
            }
        }
    }

    void DrawPattern(BPoint center, float size, int pattern)
    {
        // Original 8x8 polygon pattern
        BPoint pts[19] = {
            { size * -4.0f, size * 4.0f },     { size * -1.0f, size * 4.0f },
            { size * -1.0f, size * 6.0f },     { size * 1.5f, size * 6.0f },
            { size * 1.5f, size * 4.0f },      { size * 4.0f, size * 4.0f },
            { size * 4.0f, size * 3.5f },      { size * 6.0f, size * 0.5f },
            { size * 4.0f, size * -1.0f },     { size * 4.0f, size * -4.0f },
            { size * 1.5f, size * -4.0f },     { size * 1.5f, size * -2.0f },
            { size * -1.0f, size * -2.0f },    { size * -1.0f, size * -4.0f },
            { size * -4.0f, size * -4.0f },    { size * -4.0f, size * -1.0f },
            { size * -2.0f, size * 1.0f },     { size * -4.0f, size * 4.0f },
            { size * -4.0f, size * 4.0f }
        };

        // Offset polygon by center position
        for (auto& p : pts) {
            p.x += center.x;
            p.y += center.y;
        }

        rgb_color colors[4] = {
            { 180, 60, 100, 255 },
            { 255, 220, 100, 255 },
            { 60, 120, 180, 255 },
            { 200, 100, 50, 255 }
        };

        SetHighColor(colors[pattern % 4]);
        FillPolygon(pts, 19);
        SetHighColor(0, 0, 0);
        StrokePolygon(pts, 19, true);
    }
};

class EscherWindow : public BWindow
{
public:
    EscherWindow(BRect f)
        :
        BWindow(f, "Escher-Style Tiling", B_TITLED_WINDOW, B_QUIT_ON_WINDOW_CLOSE)
    {
        AddChild(new EscherView(Bounds()));
    }

    bool QuitRequested() override
    {
        be_app->PostMessage(B_QUIT_REQUESTED);
        return true;
    }
};

class PatternApp : public BApplication
{
public:
    PatternApp()
        :
        BApplication("application/x-vnd.example-pattern")
    {
        BRect r(100, 100, 800, 600);
        EscherWindow* w = new EscherWindow(r);
        w->Show();
    }
};

int main()
{
    PatternApp app;
    app.Run();
    return 0;
}

The core idea here is:

  • Use a predefined shape (in this case, 19 points polygon).
  • Position it in a grid that alternates each row to create a tiled effect.
  • Cycle through a palette of colors for visual variety.
  • Use FillPolygon() and StrokePolygon() to render the shape with borders.

Compile and Run

Open Terminal and compile the code with:

g++ -o PatternApp PatternApp.cpp -lbe
./PatternApp

You’ll see a nice window with an MC Escher tiling:

Pretty fun, right?

And it only took a few lines of code.

Try this

  • Change the polygon points to invent your own tile shape.
  • Add a mouse click to randomize colors or re-tile.
  • Use StrokeLine() to draw a grid around the tiles.

What’s Next?

In the next KitLab entries, we’ll cover:

  • The usage of layouts to create nice GUI
  • The handling drag and drop messages in a view

Conclusion

This example showed how simple and efficient it is to create custom visuals in Haiku using Draw().

From here, you can build much more — from interface widgets to generative art.

Keep exploring!


Powered by Bludit - Hosted by Planet Hoster
© 2025 Haiku Insider