Blackberry[CodeNameone]- Drawing on screen

113 views Asked by At

I am beginner in codenameone. I have task assigned that draws anything[not rectangles or any shapes] on screen. Anything means It could be anything with fingers. Like android has gesturelayout where you can draw anything on surface.

I have seen some forums that said I should derive the container and override paint method. That never led me to anything. Even the tutorial guide just goes through steps.I won't some working examples or any link where i can find some suitable material.

1

There are 1 answers

0
Shai Almog On

Did you read the developer guide: http://www.codenameone.com/developer-guide.html

JavaDocs: https://codenameone.googlecode.com/svn/trunk/CodenameOne/javadoc/index.html

You should derive component and override paint, notice this code is really bad since it doesn't eliminate duplicates or do anything clever:

class Draw extends Component {
    private ArrayList<Point> points = new ArrayList<Point>();

    public Draw() {
       setFocusable(true);
    }

    public void pointerPressed(int x, int y) {
        points.add(new Point(x, y, 0xff0000));
    }

    public void pointerDragged(int x, int y) {
        points.add(new Point(x, y, 0xff0000));
    }

    public void pointerReleased(int x, int y) {
        points.add(new Point(x, y, 0xff0000));
    }

    public void paint(Graphics g) {
       Point lastPoint = null;
       for(Point p : points) {
           if(lastPoint != null) {
               g.setColor(p.color);
               g.drawLine(lastPoint.x, lastPoint.y, p.x, p.y);
           }
           lastPoint = p;
       }
    }
}

class Point {
    int x;
    int y;
    int color;

    public Point(int x, int y, int color) {
       this.x = x; this.y = y; this.color = color;
    }
}