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.
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;
}
When this app runs, it opens a window containing a custom view.
Inside that view:
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:
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.

Understanding how Draw() works is essential for:
The BView class provides a range of drawing primitives:
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:
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.
In the next KitLab entries, we’ll cover:
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!