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

Example

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

Example

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

Exceptions

Example

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

Exceptions

Example

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

Exceptions

Example

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

Exceptions

Example

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
    

(bitmap) <bitmap sourceBitmap>.copy

Creates a new bitmap that is an exact copy of the bitmap given as parameter.

Parameters

Example

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

Return value

Exceptions

Example

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

Example

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

Exceptions

Example

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

Exceptions

Example

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

Example

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

Example

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