Tabular IQ

Overview

The Bitmap class encapsulates a bitmap. A bitmap made with Bitmap(width, height) is a canvas of that many DIPs (device-independent pixels) at the display's scale: drawing on it through Graphics.fromBitmap comes out sized for the display, and Graphics.drawImage puts it on a control pixel for pixel. A bitmap loaded from a file keeps its own pixels, one per DIP. getWidth and getHeight report DIPs; saveFile writes the pixels.

Constructor

Bitmap(width : Integer, height : Integer, scale : Number)
Bitmap(xpm : String, type : Integer)
Bitmap(svg : String, type : Integer, size : Integer)

Arguments

width
The width of the created bitmap, in DIPs.
height
The height of the created bitmap, in DIPs.
scale
Optional pixels per DIP of the canvas; the display's scale when left out, 1 for one pixel per DIP.
xpm
The xpm data to use in creating the bitmap object.
type
The type of data being used as input.
svg
SVG markup to use in creating the bitmap object.
size
The default render size in DIPs for SVG input.

Properties

Bitmap.FormatBMP
A type flag representing the Windows BMP file format.
Bitmap.FormatGIF
A type flag representing the GIF file format.
Bitmap.FormatXPM
A type flag representing the X Pixmap file format.
Bitmap.FormatPNG
A type flag representing the PNG file format.
Bitmap.FormatJPG
A type flag representing the JPG file format.
Bitmap.FormatSVG
A type flag representing the SVG vector image format.

Methods

Bitmap.getHeight
Gets the height of the bitmap.
Bitmap.getWidth
Gets the width of the bitmap.
Bitmap.isOk
Indicates whether or not the Bitmap object represents a valid bitmap.
Bitmap.loadFile
Loads a file into a bitmap object.
Bitmap.saveFile
Saves bitmap object to a file
Bitmap.setHeight
Sets the height of the bitmap.
Bitmap.setWidth
Sets the width of the bitmap.

Example

// an offscreen canvas: a Bitmap made with a width and height is a surface
// of DIPs at the display's scale, a Graphics made from it draws in the
// same units a control is laid out in, and drawImage copies it onto a
// control pixel for pixel.  Drawing the badge once and copying it on
// every paint costs less than redrawing it, and it looks the same on a
// 100% display and a 250% one.

class BadgeControl extends Control {
    constructor(x_pos, y_pos, width, height) {
        super(x_pos, y_pos, width, height);

        // the canvas, 120 x 40 DIPs, drawn once; Bitmap(120, 40, 1) would
        // instead be a 120 x 40 pixel image whatever the display, the
        // right choice for an image written to a file with saveFile()
        this.badge = new Bitmap(120, 40);

        const gr = Graphics.fromBitmap(this.badge);
        gr.setPen(new Pen(new Color(0, 90, 200), 2));
        gr.setBrush(new Brush(new Color(225, 236, 250)));
        gr.drawRectangle(1, 1, 118, 38);
        gr.setFont(SystemFonts.Default.bold());
        gr.setTextForeground(new Color(0, 60, 140));
        gr.drawText("Tabular IQ", 14, 11);

        this.paint.connect(this, this.onPaintCalled);
    }

    onPaintCalled(sender, evt) {
        // a copy of the badge on each row; with the width and height left
        // out, the badge is drawn at its own size and lands pixel for pixel
        const gr = evt.graphics;
        for (let i = 0; i < 3; ++i) {
            gr.drawImage(this.badge, 10, 10 + i * 50);
        }
    }
}

// the form that hosts the control
class BadgeForm extends Form {
    constructor() {
        super("Bitmap Example", 0, 0, 260, 190);

        const client_size = this.getClientSize();
        this.add(new BadgeControl(0, 0, client_size.width, client_size.height));
    }
}

const f = new BadgeForm();
f.center();
f.show();
Application.run();

Bitmap.getHeight

function Bitmap.getHeight() : Integer

Returns

Returns the height of the bitmap.

Description

Returns the height of the bitmap in DIPs: its pixels over its scale, which is 1 for a bitmap loaded from a file.

Bitmap.getWidth

function Bitmap.getWidth() : Integer

Returns

Returns the width of the bitmap.

Description

Returns the width of the bitmap in DIPs: its pixels over its scale, which is 1 for a bitmap loaded from a file.

Bitmap.isOk

function Bitmap.isOk() : Boolean

Returns

Returns true if the Bitmap object represents a valid bitmap, and false otherwise.

Description

Returns true if the Bitmap object represents a valid bitmap. Returns false if the Bitmap object does not represent a valid bitmap.

Bitmap.loadFile

function Bitmap.loadFile(filename : String, type : Integer, size : Integer) : Boolean

Arguments

filename
The name of the file to load.
type
An optional parameter specifying the format to use when loading the data from the file.
size
An optional default render size in DIPs, used only when loading an SVG file.

Returns

Returns true if the bitmap is successfully loaded from the file and false otherwise.

Description

Loads a bitmap from a file, where the file is specified filename and the optional file format is given by type. The type is one of Bitmap.FormatBMP, Bitmap.FormatGIF, Bitmap.FormatXPM, Bitmap.FormatPNG, Bitmap.FormatJPG, or Bitmap.FormatSVG. Files ending in .svg are detected automatically when no type is given. An SVG load makes the bitmap vector-backed: it renders crisply at any display scale, and the optional size gives the default render size in DIPs (device-independent pixels), defaulting to 16. size is ignored for the raster formats. Returns true if the bitmap is successfully loaded from the file and false otherwise.

Bitmap.saveFile

function Bitmap.saveFile(filename : String, type : Integer) : Boolean

Arguments

filename
The name of the file to save.
type
An optional parameter specifying the format to use when saving the data to the file.

Returns

Returns true if the bitmap is successfully saved to the file and false otherwise.

Description

Saves a bitmap to a file, where the file is specified filename and the optional file format is given by type. The type is one of Bitmap.FormatBMP, Bitmap.FormatGIF, Bitmap.FormatXPM, Bitmap.FormatPNG, or Bitmap.FormatJPG. The file holds the bitmap's pixels: for a Bitmap(width, height) canvas that is the display's scale times its size in DIPs, so an image file of a fixed pixel size is made with Bitmap(width, height, 1). Returns true if the bitmap is successfully saved to the file and false otherwise.

Bitmap.setHeight

function Bitmap.setHeight(height : Integer)

Arguments

height
The height to which to set the bitmap.

Description

Sets the height of the bitmap, in DIPs. A canvas keeps its scale; its pixels are dropped.

Bitmap.setWidth

function Bitmap.setWidth(width : Integer)

Arguments

width
The width to which to set the bitmap.

Description

Sets the width of the bitmap, in DIPs. A canvas keeps its scale; its pixels are dropped.