CanvasGraphicsItem

CanvasGraphicsItem is a floating drawable component that is connected to its parent Canvas. Every CanvasGraphicsItem can have only one parent at a time. Parent must be set before any other operation is done with the item.

When created, the CanvasGraphicsItem is not visible and must be set visible with setVisible().

The CanvasGraphicsItem has the same drawing capabilities as Canvas. The paint() method is declared abstract, and so the application must provide an implementation in its subclass. The whole CanvasGraphicsItem is transparent, so every other item on Canvas (TextEditor and CanvasGraphicsItem) and everything painted on Canvas can be seen. Graphics does not support transparency, so everything drawn by it is opaque. To draw something with different level of transparency, the application must use DirectGraphics instead.

CanvasGraphicsItem can be used for drawing moving transparent and opaque objects multiple times, or for scroll bars, frames, buttons and so on. The main use case of the CanvasGraphicsItem is drawing content on top of the editor, such as pop ups, option lists and mouse pointers.

CanvasGraphicsItem example

TheCanvasGraphicsItem does not support touch input. All pointer events pressed on top of it are passed either to TextEditor, if it is at the pointer event location, or to parent object, Canvas. It is up to the MIDlet to implement CanvasGraphicsItem touch interaction.

The following example shows how to create and show CanvasGraphicsItem on Canvas with a partly transparent rectangle and an opaque circle with the paint() method.

// CanvasGraphicsItemExample.java\ import com.nokia.mid.ui.CanvasGraphicsItem

// ...

    // Create a class which overwrites the CanvasGraphicsItem
    public class CanvasGItem extends CanvasGraphicsItem {
    
        // Constructor
        CanvasGItem(int width, int height) {
            super(width, height);
	 widthCanvas = width;
	 heightCanvas = height; 
        }

        /*
         * Only method that needs to be implemented by 
         * CanvasGraphicsItem's subclass.
         */
        protected void paint(Graphics gc) {
        
            // Draws red rectangle 
            graphics.setColor(0xFF0000);
            graphics.fillRect(10, 10, this.getWidth() - 10, 
                this.getHeight() - 10);

            // Draws transparent green polygon using DirectGraphics
            DirectGraphics dg = DirectUtils.getDirectGraphics(graphics);
            dg.fillPolygon(
                new int[]{0, widthCanvas / 2 + 20, 
                    widthCanvas, widthCanvas / 2 - 20}, 0, 
                new int[]{heightCanvas / 2 - 30, 0, heightCanvas / 2 + 30,
                    heightCanvas}, 0,
                4,
                0x7700FF00
                );
        }

// CanvasExample.java

    //..

    // Creates the CanvasGraphicsItem in the Canvas with
    // size 200/200, position 50/50 and makes it visible.
    CanvasGItem item = new CanvasGItem(200, 200);
    item.setParent(this);
    item.setPosition(50, 50);
    item.setVisibility(true);

    // ...

// CanvasGraphicsItemExample.java\ import com.nokia.mid.ui.CanvasGraphicsItem