Overview | Package | Class | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## waba.fx
Class Image

    java.lang.Object
      |
      +--waba.fx.Image

public final class Image
extends Object
implements ISurface

Image is a rectangular image.

You can draw into an image and copy an image to a surface using a Graphics object. Before a image is drawed, its palette is set.

See Also:
  `Graphics`

Field Summary
`protected int` | `height`
`protected int[]` | `palRGBs`
`protected int` | `width`

Constructor Summary
`Image(byte[] fullBmpDescription)` Parses an image from the given byte array.
`Image(int width, int height)` Creates an image of the specified width and height.
`Image(String path)` Loads and constructs an image from a file.

Method Summary
`void` | `applyPalette()` apply the palette used by this bitmap to the display
`void` | `changeColors(Color from, Color to)` Changes all the pixels of the image from one color to the other.
`int` | `createBmp(DataStream ds)` saves this image as a Windows .bmp file format, with the same bpp of the screen using the current palette of this image, to the given DataStream.
`void` | `free()` Sets the image width and height to 0 and frees any systems resources associated with the image.
`Graphics` | `getGraphics()` Returns a new Graphics instance that can be used to drawing in this image.
`int` | `getHeight()` Returns the height of the image.
`Palette` | `getPalette()` returns the palette associated with this image.
`Color` | `getTransparentColor()` Gets the transparent color of this image.
`int` | `getWidth()` Returns the width of the image.
`int` | `saveTo(Catalog cat, String name)` saves this image as a Windows .bmp file format to the given Catalog.
`void` | `setPalette(Palette pal)` substitutes the original palette that came with this image with the given one.
`void` | `setTransparentColor(Color c)` Sets the transparent color of this image.

Methods inherited from class java.lang.Object
`equals, getClass, hashCode, notify, toString, wait, wait`

Field Detail

### width

    protected int width

### height

    protected int height

### palRGBs

    protected int[] palRGBs

Constructor Detail

### Image

    public Image(int width,
                 int height)

  Creates an image of the specified width and height. The image has a color depth (number of bitplanes) and color map that matches the default drawing surface. Here is an example of use:

       Image img = new Image(100,100);
       Graphics g = img.getGraphics();
       g.setBackColor(Color.WHITE);
       g.fillRect(25,25,50,50);
       ...
       Graphics screenG = createGraphics();
       screenG.drawImage(img,CENTER,CENTER);

### Image

    public Image(String path)

  Loads and constructs an image from a file. The path given is the path to the image file. The file must be in 2, 16 or 256 color uncompressed BMP bitmap format. If the image cannot be loaded, the image constructed will have a width and height of 0.

### Image

    public Image(byte[] fullBmpDescription)

  Parses an image from the given byte array. Note that the byte array must specify the full bitmap, with headers, palette and the data. The bitmap can be in compressed rle4 or rle8. Here is a code example:

       // create the image and fill it with something
       Image img = new Image(160,160);
       Graphics g = img.getGraphics();
       for (int i =0; i < 16; i++)
       {
          g.setBackColor(new Color(10*i,10*i,10*i));
          g.fillRect(i*10,0,10,160);
       }
       // save the bmp in a byte stream
       ByteArrayStream bas = new ByteArrayStream(4096);
       DataStream ds = new DataStream(bas);
       int totalBytesWritten = img.createBmp(ds);
       // parse the saved bmp
       Image im = new Image(bas.getBuffer()); // Caution! the buffer may be greater than totalBytesWritten, but when parsing theres no problem.
       if (im.getWidth() > 0) // successfully parsed?
       {
          createGraphics().drawImage(im,CENTER,CENTER);
          Vm.sleep(2000);
       }

Method Detail

### free

    public void free()

  Sets the image width and height to 0 and frees any systems resources associated with the image. The image will be no longer valid and will be GC'd.

### getHeight

    public int getHeight()

  Returns the height of the image. You can check if the image is ok comparing this with zero.

### getWidth

    public int getWidth()

  Returns the width of the image. You can check if the image is ok comparing this with zero.

### getGraphics

    public Graphics getGraphics()

  Returns a new Graphics instance that can be used to drawing in this image.

### setTransparentColor

    public void setTransparentColor(Color c)

  Sets the transparent color of this image. Used in many controls. Note that you can set it to null to make the image use the background color. Important!: If your image doesnt use the web safe palette, you must call `myImage.applyPalette` before calling setTransparentColor (or before the first time the image is drawn), otherwise the color may not be found in the active palette.

### getTransparentColor

    public Color getTransparentColor()

  Gets the transparent color of this image.

### changeColors

    public void changeColors(Color from,
                             Color to)

  Changes all the pixels of the image from one color to the other. Note that the current value of the transparent color is not changed. When you load a mono bmp (consumes less memory), it is stored internally as grayScale (or color). Using this routine, you can change the colors to any other you want.

### getPalette

    public Palette getPalette()

  returns the palette associated with this image. If this method return null, the default SystemPalette is used.

### setPalette

    public void setPalette(Palette pal)

  substitutes the original palette that came with this image with the given one.

### saveTo

    public int saveTo(Catalog cat,
                      String name)

  saves this image as a Windows .bmp file format to the given Catalog. Important note: the catalog can save multiple images, but the record must be in the Waba warp record format (the image name comes before the data) and must be sorted. This method finds the exact place where to insert the bmp and put it there. If you want to create a bmp to be transfered by a stream to serial or socket then you must use the method createBmp instead. Important: when creating the catalog, the type must be 'SWAX' (SuperWAba eXtension library); if not, the library will not be found by the VM. The catalog name can not be the same name of the image. EG: you can have a catalog named "images" and containing lots of images, one with each name. if a record with this name already exists, it will be replaced. The name is always converted to lowercase and the method makes sure that .bmp is appended to it. To get the list of images in a Catalog, just do a readString at the beginning of each record.

  Here is an usage example:

       // create the image and paint over it
       Image img = new Image(100,100);
       Graphics g = img.getGraphics();
       g.setBackColor(new Color(100,150,200));
       g.fillRect(25,25,50,50);
       g.setForeColor(new Color(255,255,255));
       g.drawCircle(50,50,20);
       // create the catalog to save the image. You must change CRTR to match your apps creator ID
       Catalog cat = new Catalog("images.CRTR.SWAX", Catalog.CREATE);
       img.saveTo(cat, "boxcircle.bmp");
       cat.close();
       // load the previously created image
       if (Vm.attachLibrary("images.CRTR")) // must add the creator so it can be used in desktop. The type is always SWAX
       {
          Image im = new Image("boxcircle.bmp");
          if (im.getWidth() > 0) // successfully loaded?
             createGraphics().drawImage(im,CENTER,CENTER);
       }

  Returns:
    the numbers of bytes writted.
  See Also:
    `createBmp(waba.io.DataStream)`

### createBmp

    public int createBmp(DataStream ds)

  saves this image as a Windows .bmp file format, with the same bpp of the screen using the current palette of this image, to the given DataStream. If you're sending the bmp through a stream but not saving to a catalog, you can use this method. If you're goind to save it to a catalog, then you must use the saveTo method. Note: if you're saving an image over a previously loaded one, the palette will be preserved only if the device is color. Otherwise, the colors will be converted to the current System palette or to the device's palette.
  Returns:
    the number of bytes writted.
  See Also:
    `saveTo(waba.io.Catalog, java.lang.String)`

### applyPalette

    public void applyPalette()

  apply the palette used by this bitmap to the display

Overview | Package | Class | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD