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.
// 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(); Returns the height of the bitmap.
Returns the height of the bitmap in DIPs: its pixels over its scale, which is 1 for a bitmap loaded from a file.
Returns the width of the bitmap.
Returns the width of the bitmap in DIPs: its pixels over its scale, which is 1 for a bitmap loaded from a file.
Returns true if the Bitmap object represents a valid bitmap, and false otherwise.
Returns true if the Bitmap object represents a valid bitmap. Returns false if the Bitmap object does not represent a valid bitmap.
Returns true if the bitmap is successfully loaded from the file and false otherwise.
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.
Returns true if the bitmap is successfully saved to the file and false otherwise.
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.
Sets the height of the bitmap, in DIPs. A canvas keeps its scale; its pixels are dropped.
Sets the width of the bitmap, in DIPs. A canvas keeps its scale; its pixels are dropped.