mwt
Class Skin

java.lang.Object
  extended by mwt.Skin
Direct Known Subclasses:
ImageUtil

public class Skin
extends java.lang.Object

A skin is used to paint regions of different sizes using the same look and feel.

A skin can be created giving images (an "image skin"), or giving colors ("colour rectangle skin").

Image Skin

To create an image skin, you must provide an array of 9 images.
The next example shows how they will be used:

Each image of the given array maps in the next way:
 7 0 1       0, 2, 4, 5: sides 
 6 8 2       1, 3, 5, 7: borders
 5 4 3       8: fill
    

"Middle" sides (2 and 6), are repeated in y axis until they fill the desired area.
"Center" sides (0 and 4), are repeated in x axis until they fill the desired area.
The "fill" image is repeated in both axis.
Border images are blitted as they are.

If images are repeated too many times, the application may run slower.
To improve this, you can resize the images when the skin is created. However, this requires more memory.
Another drawback is that if the images are resized, they may lose the alpha channel info.

The next screens illustrate how an image skin is stretched.

Colour Rectangle Skin

A colour rectangle skin is created by giving an array of integers.

The skin will draw a rectangle using this colour data which must contain at least 1 value.
The first index value is the background fill, next values are frames from outside to inside.

Example:
0xFFE220
0x000000
0x5999FF
0x000000
0xD9C01C
0xFFCCCF
0xA34184
0xFF66CF
0xFF66CF
0xFFFFFF
0x000000
0xA1C632
0xCFFF40

Extending

Classes that extend a Skin must override the following methods:

The contructor must call the default constructor Skin().

Example:
 class MySkin extends Skin {
   int newAttribute = ...;
   
   public MySkin(int newAttribute) {
     super(); // this is not necessary
     this.newAttribute = newAttribute;
   }
   
   protected void copy(MySkin s) {
     super.copy(s);
     s.newAttribute = this.newAttribute;
   }
   
   private MySkin() {}
   public Skin clone() {
     MySkin s = new MySkin();
     copy(s);
     return s;
   }
   
   protected void paint(Graphics g, int width, int height) {
     // new implementation
   }
 }
 

Caching

To improve performance, there's a mechanism to create a cache if the component calling is double buffered.
The default implementation has an important drawback; alpha values are ignored.
You can extend this class and implement a better cache mechanism overriding the createBuffer(int, int) method.


Constructor Summary
Skin()
          Creates a "dummy" skin, paint calls are ignored.
Skin(javax.microedition.lcdui.Image[] images, int newSize)
          Creates an image skin.
Skin(int[] colors)
          Creates a "colour rectangle" skin.
 
Method Summary
 Skin clone()
          Gets a clone of this skin.
protected  void copy(Skin skin)
          Copies this skin into the given skin.
protected  javax.microedition.lcdui.Image createBuffer(int width, int height)
          Creates an image that will be used as a buffer.
 void paint(Component c, javax.microedition.lcdui.Graphics g)
          Paints a component into the given graphics object.
protected  void paint(javax.microedition.lcdui.Graphics g, int width, int height)
          Paints this skin into the given graphics object with the given width and height.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Skin

public Skin()
Creates a "dummy" skin, paint calls are ignored.


Skin

public Skin(int[] colors)
Creates a "colour rectangle" skin.

See Also:
Colour Rectangle Skin

Skin

public Skin(javax.microedition.lcdui.Image[] images,
            int newSize)
Creates an image skin.

Parameters:
images - The array with 9 images in the correspoding order
newSize - The new size, or 0 to skip resizing
See Also:
Image Skin
Method Detail

clone

public Skin clone()
Gets a clone of this skin.

Overrides:
clone in class java.lang.Object
See Also:
Extending a Skin

copy

protected void copy(Skin skin)
Copies this skin into the given skin.

See Also:
Extending a Skin

paint

public final void paint(Component c,
                        javax.microedition.lcdui.Graphics g)
Paints a component into the given graphics object.
If the component is double buffered, this method may call createBuffer(int, int).


createBuffer

protected javax.microedition.lcdui.Image createBuffer(int width,
                                                      int height)
Creates an image that will be used as a buffer.
This method must return an image for the given component size.
An implementation can return null, in such case, no cache is created.
The default implementation is:
        Image buf = Image.createImage(c.getWidth(),c.getHeight());
        this.paint(buf.getGraphics(),c.getWidth(),c.getHeight());
        return buf;
 
MWT handles memory management automatically.
References to this image will be garbage collected when the given component is garbage collected, or when its double buffer property is set to false.


paint

protected void paint(javax.microedition.lcdui.Graphics g,
                     int width,
                     int height)
Paints this skin into the given graphics object with the given width and height.



Copyright © 2007 MWT-Team. All Rights Reserved.