Class Bitmap
Class Bitmap represents an image with an alpha channel. Bitmaps can
be loaded from files, from a byte[] or you can create a bitmap with
a specified size.
As the bitmap class implements Canvas interface you can use
the drawing operations on bitmaps.
Supported Image Formats
The bitmap class supports loading images from PNG and JPG files.
Other image file formats are platform-dependent.
Implemented Interfaces
canvas
Constructors
Methods
main
system.run application new MyApplication
class MyApplication extends Application
Bitmap bmp1 = new Bitmap size 10, 10
Bitmap bmp2 = new Bitmap from file "example.png"
Bitmap bmp3 = null
Application
bmp3 = new Bitmap from bmp2 source 3, 3 size 25, 25
bmp3.scale to size 15, 15
bmp1.draw line from 0, 0 to 10, 10 color Color.red
draw to <canvas g>
g.draw bitmap bmp1 to 0, 0
g.draw bitmap bmp2 to 0, bmp1.get size.get height
g.draw bitmap bmp3 to 0, 100 alpha 128
(bitmap) new bitmap size <size bitmapSize> [color <color fillColor = Color.black>]
Constructs a bitmap with the specified size. The bitmap is filled with the given color.
If the width or height is zero the bitmap will be empty.
Parameters
- bitmapSize - Size of the bitmap in pixels.
- fillColor - The bitmap is filled with this color.
main
// Create a bitmap with size (10, 40)
Bitmap bmp = new Bitmap size 10, 40
// Create an empty bitmap
Bitmap bmp2 = new Bitmap size 0, 0
(bitmap) new bitmap from <bitmap sourceBitmap> source <rectangle sourceRegion>
Creates a new bitmap from pixel data of another bitmap.
The content will be copied from the sourceBitmap's specified region.
The size of the new bitmap is the same as the size of the region.
Parameters
- sourceBitmap - Source bitmap.
- sourceRegion - Region on the source bitmap that contains the pixel values for the new bitmap.
Exceptions
- InvalidArgumentException - If the sourceRegion is outside of the sourceBitmap's bounds.
main
// Create a bitmap with size (10, 40)
Bitmap bmp1 = new Bitmap size 10, 40
// Create bitmap with size (10, 10), starting point is upper left corner of source bitmap.
Bitmap bmp2 = new Bitmap from bmp1 source 0,0 size 10,10
(bitmap) new bitmap from file <string path>
Loads a bitmap from file.
Parameters
- path - Path of the bitmap.
Exceptions
- InvalidArgumentException - If the path is empty or the file is not an image.
- IOException - If the image file cannot be read.
main
// Load bitmap from file
Bitmap bmp = new Bitmap from file "bitmap.png"
(bitmap) new bitmap from <byte[] bitmapData>
Loads a bitmap from byte[] containing image data.
Parameters
- bitmapData - A byte array that contains image data.
Exceptions
- InvalidArgumentException - If the image data is empty or if loading of the Bitmap fails.
main
FileReader fr = new FileReader "bitmap.png"
Byte[] byteArray = fr.read fr.get size bytes
// Load bitmap from byte data.
Bitmap bmp = new Bitmap from byteArray
fr.close
(bitmap) new bitmap from <byte[] bitmapData> range <range bitmapDataRange>
Loads a bitmap from sub-section of a byte[] containing image data.
The sub-section is defined by bitmapDataRange.
Parameters
- bitmapData -
A byte array that contains image data at the specified range.
- bitmapDataRange -
Range of bitmapData that contains the image's data.
Exceptions
- InvalidRangeException - If the specified range is outside the bitmapData's bounds.
- InvalidArgumentException - If the range is empty or if loading of the Bitmap fails.
main
FileReader fr = new FileReader "bitmap.png"
Byte[] byteArray = fr.read fr.get size bytes
// Load bitmap from byte datas first 1000 bytes.
Bitmap bmp = new Bitmap from byteArray range 0 size 1000
fr.close
Creates a new bitmap that is an exact copy of the bitmap given as parameter.
Parameters
- sourceBitmap - The bitmap that will be copied.
main
// Create a bitmap with size (10, 40)
Bitmap bmp1 = new Bitmap size 10, 40
Bitmap bmp2 = bmp1.copy
(color) get pixel <position pixelPosition>
Returns the color value at the given pixel.
Parameters
- pixelPosition - Position of the pixel.
Return value
- Color value on the given pixel.
Exceptions
- InvalidArgumentException - If the pixelPosition is outside of the bitmap's bounds.
main
// Create a bitmap with size (10, 40)
Bitmap bmp = new Bitmap from file "bitmap.png"
// Get color of first pixel
Color c = bmp.get pixel 0,0
(size) get size
Returns the size of the bitmap.
Return value
- Size of the bitmap in pixels.
main
// Create a bitmap with size (10, 40)
Bitmap bmp = new Bitmap from file "bitmap.png"
// Get size of the bitmap
Size s = bmp.get size
print "Size of the bitmap is " & s.get width & "x" & s.get height
set pixel <position pixelPosition> to <color newColor>
Set a new value for a pixel.
Parameters
- pixelPosition - Position of the pixel.
- newColor - Color value for the pixel.
Exceptions
- InvalidArgumentException - If the pixelPosition is outside of the bitmap's bounds.
main
// Create a bitmap with size (10, 40)
Bitmap bmp = new Bitmap from file "bitmap.png"
// Set color of first pixel to black
bmp.set pixel 0,0 to color.black
scale to size <size newSize>
Scales an bitmap to a new size. This method is the same as scale to <size> quality low.
Parameters
- newSize - New size for the bitmap.
Exceptions
- InvalidArgumentException - If the pixelPosition is outside of the bitmap's bounds.
main
// Create a bitmap with size (10, 40)
Bitmap bmp = new Bitmap from file "bitmap.png"
// Scale bitmap to 10x10
bmp.scale to size 10,10
scale to size <size newSize> quality low
Scales an bitmap to a new size. This method uses a faster scaling algorithm.
Parameters
- newSize - New size for the bitmap.
main
// Create a bitmap with size (10, 40)
Bitmap bmp = new Bitmap from file "bitmap.png"
// Scale bitmap to 10x10
bmp.scale to size 10,10 quality low
scale to size <size newSize> quality high
Scales an bitmap to a new size. This method uses slower but better quality scaling algorithm.
Parameters
- newSize - New size for the bitmap.
main
// Create a bitmap with size (10, 40)
Bitmap bmp = new Bitmap from file "bitmap.png"
// Scale bitmap to 10x10 with high quality
bmp.scale to size 10,10 quality high