low level graphics in Blackberry

172 Views Asked by At

hi everyone I am new to blackebrry. I want to use low level graphics for drawing a bitmapfield. On this bitmapfield i need to take three action-

  1. pick the bitmapfield,
  2. drag it in the screen in any direction and
  3. finally release it at any point on the screen.

how can I do this. if 'Canvas' can be a solution... can any one help me with some sample code for how to use 'Canvas'.

Also is there any other method for making a draggable bitmapfield..??

Please help

1

There are 1 best solutions below

2
On

Firstly, you can create a bitmap and get a graphics object from it to do custom drawing on it.

Bitmap myBitmap = new Bitmap(widht, height);
Graphics graphics = Graphics.create(myBitmap);

// now you can draw anything on graphics supported by API

[Edited]

I have added a code snippet for drag an image on a screen. You can check this code if it works on your side.

class MyScreen extends MainScreen {
    private final Bitmap myBm;
    private int wBm;
    private int hBm;
    private int xBm;
    private int yBm;
    private boolean dragStarted;

    public MyScreen() {
        myBm = Bitmap.getBitmapResource("img/myBitmap.png");
        if (myBm != null) {
            wBm = myBm.getWidth();
            hBm = myBm.getHeight();
        }
    }

    protected void paint(Graphics graphics) {
        // need to adjust z-index of the items added on screen.
        super.paint(graphics);
        if (myBm != null) {
            graphics.drawBitmap(xBm, yBm, wBm, hBm, myBm, 0, 0);
        }
    }

    protected boolean touchEvent(TouchEvent event) {
        int x = event.getX(1);
        int y = event.getY(1);
        int eventCode = event.getEvent();

        if (dragStarted == false & (eventCode == TouchEvent.DOWN)) {
            if (isDragActivated(x, y)) {
                // need to adjust offset.
                dragStarted = true;
                updateBitmapLocation(x, y);
            }
        }

        if (eventCode == TouchEvent.MOVE && dragStarted) {
            updateBitmapLocation(x, y);
        }

        if (eventCode == TouchEvent.UP && dragStarted) {
            dragStarted = false;
            updateBitmapLocation(x, y);
        }

        // need to decide what to return
        return true;
    }

    private void updateBitmapLocation(int xTouch, int yTouch) {
        if (xTouch != xBm && yTouch != yBm) {
            xBm = xTouch;
            yBm = yTouch;
            invalidate();
        }
    }

    private boolean isDragActivated(int x, int y) {
        return (x >= xBm && x <= xBm + wBm && y >= yBm && y <= yBm + hBm);
    }
}