Code example for Canvas

Methods: concat, getMatrix

0
        mRh.add((RenderingHints) hints);
    } 
 
    public float[] getMatrix() { 
        float[] f = new float[9];
        mC.getMatrix().getValues(f);
        return f;
    } 
 
    /** 
     *  
     * @return a Matrix in Android format 
     */ 
    public float[] getInverseMatrix() { 
        AffineTransform af = new AffineTransform(createAWTMatrix(getMatrix()));
        try { 
            af = af.createInverse();
        } catch (NoninvertibleTransformException e) {
        } 
        return createMatrix(af);
    } 
 
    private Path getPath(Shape s) {
        Path path = new Path();
        PathIterator pi = s.getPathIterator(null);
        while (pi.isDone() == false) {
            getCurrentSegment(pi, path);
            pi.next();
        } 
        return path;
    } 
 
    private void getCurrentSegment(PathIterator pi, Path path) {
        float[] coordinates = new float[6];
        int type = pi.currentSegment(coordinates);
        switch (type) {
        case PathIterator.SEG_MOVETO: 
            path.moveTo(coordinates[0], coordinates[1]);
            break; 
        case PathIterator.SEG_LINETO: 
            path.lineTo(coordinates[0], coordinates[1]);
            break; 
        case PathIterator.SEG_QUADTO: 
            path.quadTo(coordinates[0], coordinates[1], coordinates[2],
                    coordinates[3]);
            break; 
        case PathIterator.SEG_CUBICTO: 
            path.cubicTo(coordinates[0], coordinates[1], coordinates[2],
                    coordinates[3], coordinates[4], coordinates[5]);
            break; 
        case PathIterator.SEG_CLOSE: 
            path.close();
            break; 
        default: 
            break; 
        } 
    } 
     
    private Shape createShape(int[] arr) {
        Shape s = new GeneralPath();
        for(int i = 0; i < arr.length; i++) {
            int type = arr[i];    
            switch (type) {
            case -1: 
                //MOVETO 
                ((GeneralPath)s).moveTo(arr[++i], arr[++i]);
                break; 
            case -2: 
                //LINETO 
                ((GeneralPath)s).lineTo(arr[++i], arr[++i]);
                break; 
            case -3: 
                //QUADTO 
                ((GeneralPath)s).quadTo(arr[++i], arr[++i], arr[++i],
                        arr[++i]);
                break; 
            case -4: 
                //CUBICTO 
                ((GeneralPath)s).curveTo(arr[++i], arr[++i], arr[++i],
                        arr[++i], arr[++i], arr[++i]);
                break; 
            case -5: 
                //CLOSE 
                return s;
            default: 
                break; 
            } 
        } 
        return s;
    } 
    /* 
    public int[] getPixels() { 
        return mC.getPixels(); 
    }*/ 
 
    public static float getRadian(float degree) {
        return (float) ((Math.PI / 180) * degree);
    } 
     
    private Shape getShape() {
        return null; 
    } 
 
    public static float getDegree(float radian) {
        return (float) ((180 / Math.PI) * radian);
    } 
 
    /* 
     * Degree in radian 
     */ 
    public static float getEllipsisX(float degree, float princAxis) {
        return (float) Math.cos(degree) * princAxis;
    } 
 
    public static float getEllipsisY(float degree, float conAxis) {
        return (float) Math.sin(degree) * conAxis;
    } 
 
    @Override 
    public void clip(Shape s) {
        mC.clipPath(getPath(s));
    } 
 
    public void setCanvas(Canvas c) {
        mC = c;
    } 
 
    @Override 
    public void draw(Shape s) {
        if (mP == null) {
            mP = new Paint();
        } 
        Paint.Style tmp = mP.getStyle();
        mP.setStyle(Paint.Style.STROKE);
        mC.drawPath(getPath(s), mP);
        mP.setStyle(tmp);
    } 
/* 
    private ArrayList getSegments(Shape s) { 
        ArrayList arr = new ArrayList(); 
        PathIterator pi = s.getPathIterator(null); 
        while (pi.isDone() == false) { 
            getCurrentSegment(pi, arr); 
            pi.next(); 
        } 
        return arr; 
    } 
 
    private void getCurrentSegment(PathIterator pi, ArrayList arr) { 
        float[] coordinates = new float[6]; 
        int type = pi.currentSegment(coordinates); 
        switch (type) { 
        case PathIterator.SEG_MOVETO: 
            arr.add(new Integer(-1)); 
            break; 
        case PathIterator.SEG_LINETO: 
            arr.add(new Integer(-2)); 
            break; 
        case PathIterator.SEG_QUADTO: 
            arr.add(new Integer(-3)); 
            break; 
        case PathIterator.SEG_CUBICTO: 
            arr.add(new Integer(-4)); 
            break; 
        case PathIterator.SEG_CLOSE: 
            arr.add(new Integer(-5)); 
            break; 
        default: 
            break; 
        } 
    } 
*/ 
    /* 
     * Convenience method, not standard AWT 
     */ 
    public void draw(Path s) {
        if (mP == null) {
            mP = new Paint();
        } 
        Paint.Style tmp = mP.getStyle();
        mP.setStyle(Paint.Style.STROKE);
        s.transform(mM);
        mC.drawPath(s, mP);
        mP.setStyle(tmp);
    } 
 
    @Override 
    public void drawGlyphVector(GlyphVector g, float x, float y) {
        // TODO draw at x, y 
        // draw(g.getOutline()); 
        /* 
        Matrix matrix = new Matrix(); 
        matrix.setTranslate(x, y); 
        Path pth = getPath(g.getOutline()); 
        pth.transform(matrix); 
        draw(pth); 
        */ 
        Path path = new Path();
        char[] c = ((AndroidGlyphVector)g).getGlyphs();
        mP.getTextPath(c, 0, c.length, x, y, path);
        mC.drawPath(path, mP);
    } 
 
    @Override 
    public void drawRenderableImage(RenderableImage img, AffineTransform xform) {
        throw new RuntimeException("Not implemented!");
    } 
 
    @Override 
    public void drawRenderedImage(RenderedImage img, AffineTransform xform) {
        throw new RuntimeException("Not implemented!");
    } 
 
    @Override 
    public void drawString(AttributedCharacterIterator iterator, float x,
            float y) {
        throw new RuntimeException("AttributedCharacterIterator not supported!");
 
    } 
 
    @Override 
    public void drawString(AttributedCharacterIterator iterator, int x, int y) {
        throw new RuntimeException("AttributedCharacterIterator not supported!");
 
    } 
 
    @Override 
    public void drawString(String s, float x, float y) {
            if (mP == null) {
                mP = new Paint();
            } 
            Paint.Style tmp = mP.getStyle();
 
            mP.setStyle(Paint.Style.FILL);
            Path pth = new Path();
            mP.getTextPath(s, 0, s.length(), x, y, pth);
            mC.drawPath(pth, mP);
            mP.setStyle(tmp);
    } 
 
    @Override 
    public void drawString(String str, int x, int y) {
            if (mP == null) {
                mP = new Paint();
            } 
            Paint.Style tmp = mP.getStyle();
            mP.setStrokeWidth(0);
 
            mC.drawText(str.toCharArray(), 0, str.toCharArray().length, x, y,
                    mP);
            mP.setStyle(tmp);
    } 
 
    @Override 
    public void fill(Shape s) {
            if (mP == null) {
                mP = new Paint();
            } 
            Paint.Style tmp = mP.getStyle();
            mP.setStyle(Paint.Style.FILL);
            mC.drawPath(getPath(s), mP);
            mP.setStyle(tmp);
    } 
 
    @Override 
    public Color getBackground() {
        return mBc;
    } 
 
    @Override 
    public Composite getComposite() { 
        throw new RuntimeException("Composite not implemented!");
    } 
 
    @Override 
    public GraphicsConfiguration getDeviceConfiguration() { 
        return new AndroidGraphicsConfiguration(); 
    } 
 
    @Override 
    public FontRenderContext getFontRenderContext() { 
        return new FontRenderContext(getTransform(), mP.isAntiAlias(), true);
    } 
 
    @Override 
    public java.awt.Paint getPaint() {
        throw new RuntimeException("AWT Paint not implemented in Android!");
    } 
 
    public static Canvas getAndroidCanvas() {
        return mC;
    } 
     
    public static Paint getAndroidPaint() {
        return mP;
    } 
 
    @Override 
    public RenderingHints getRenderingHints() { 
        return mRh;
    } 
 
    @Override 
    public Stroke getStroke() { 
        if (mP != null) {
            return new BasicStroke(mP.getStrokeWidth(), mP.getStrokeCap()
                    .ordinal(), mP.getStrokeJoin().ordinal());
        } 
        return null; 
    } 
 
    @Override 
    public AffineTransform getTransform() { 
        return new AffineTransform(createAWTMatrix(getMatrix())); 
    } 
 
    @Override 
    public boolean hit(Rectangle rect, Shape s, boolean onStroke) {
        // ???AWT TODO check if on stroke 
        return s.intersects(rect.getX(), rect.getY(), rect.getWidth(), rect
                .getHeight()); 
    } 
 
    @Override 
    public void rotate(double theta) {
        mM.preRotate((float) AndroidGraphics2D
                .getDegree((float) (RAD_360 - theta)));
        mC.concat(mM);
    } 
 
    @Override 
    public void rotate(double theta, double x, double y) {
        mM.preRotate((float) AndroidGraphics2D.getDegree((float) theta),
                (float) x, (float) y);
        mC.concat(mM);
    } 
 
    @Override 
    public void scale(double sx, double sy) {
        mM.setScale((float) sx, (float) sy);
        mC.concat(mM);
    } 
 
    @Override 
    public void setBackground(Color color) {
        mBc = color;
        mC.clipRect(new Rect(0, 0, mC.getWidth(), mC.getHeight()));
        // TODO don't limit to current clip 
        mC.drawARGB(color.getAlpha(), color.getRed(), color.getGreen(), color
                .getBlue()); 
    } 
 
    @Override 
    public void setComposite(Composite comp) {
        throw new RuntimeException("Composite not implemented!");
    } 
 
    public void setSpaint(Paint paint) {
        mP = paint;
    } 
 
    @Override 
    public void setPaint(java.awt.Paint paint) {
        setColor((Color)paint);
    } 
 
    @Override 
    public Object getRenderingHint(RenderingHints.Key key) {
        if (mRh == null) {
            return null; 
        } 
        return mRh.get(key);
    } 
 
    @Override 
    public void setRenderingHint(RenderingHints.Key hintKey, Object hintValue) {
        if (mRh == null) {
            mRh = new RenderingHints(hintKey, hintValue);
        } else { 
            mRh.put(hintKey, hintValue);
        } 
        applyHints(); 
    } 
 
    @Override 
    public void setRenderingHints(Map<?, ?> hints) {
        mRh = (RenderingHints) hints;
        applyHints(); 
    } 
 
    private void applyHints() { 
        Object o;
 
        // TODO do something like this: 
        /* 
         * Set s = mRh.keySet(); Iterator it = s.iterator(); while(it.hasNext()) { 
         * o = it.next(); } 
         */ 
 
        // ///////////////////////////////////////////////////////////////////// 
        // not supported in skia 
        /* 
         * o = mRh.get(RenderingHints.KEY_ALPHA_INTERPOLATION); if 
         * (o.equals(RenderingHints.VALUE_ALPHA_INTERPOLATION_DEFAULT)) { } else 
         * if (o.equals(RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY)) { } 
         * else if (o.equals(RenderingHints.VALUE_ALPHA_INTERPOLATION_SPEED)) { } 
         *  
         * o = mRh.get(RenderingHints.KEY_COLOR_RENDERING); if 
         * (o.equals(RenderingHints.VALUE_COLOR_RENDER_DEFAULT)) { } else if 
         * (o.equals(RenderingHints.VALUE_COLOR_RENDER_QUALITY)) { } else if 
         * (o.equals(RenderingHints.VALUE_COLOR_RENDER_SPEED)) { } 
         *  
         * o = mRh.get(RenderingHints.KEY_DITHERING); if 
         * (o.equals(RenderingHints.VALUE_DITHER_DEFAULT)) { } else if 
         * (o.equals(RenderingHints.VALUE_DITHER_DISABLE)) { } else if 
         * (o.equals(RenderingHints.VALUE_DITHER_ENABLE)) { } 
         *  
         * o = mRh.get(RenderingHints.KEY_FRACTIONALMETRICS); if 
         * (o.equals(RenderingHints.VALUE_FRACTIONALMETRICS_DEFAULT)) { } else 
         * if (o.equals(RenderingHints.VALUE_FRACTIONALMETRICS_OFF)) { } else if 
         * (o.equals(RenderingHints.VALUE_FRACTIONALMETRICS_ON)) { } 
         *  
         * o = mRh.get(RenderingHints.KEY_INTERPOLATION); if 
         * (o.equals(RenderingHints.VALUE_INTERPOLATION_BICUBIC)) { } else if 
         * (o.equals(RenderingHints.VALUE_INTERPOLATION_BILINEAR)) { } else if 
         * (o .equals(RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR)) { } 
         *  
         * o = mRh.get(RenderingHints.KEY_RENDERING); if 
         * (o.equals(RenderingHints.VALUE_RENDER_DEFAULT)) { } else if 
         * (o.equals(RenderingHints.VALUE_RENDER_QUALITY)) { } else if 
         * (o.equals(RenderingHints.VALUE_RENDER_SPEED)) { } 
         *  
         * o = mRh.get(RenderingHints.KEY_STROKE_CONTROL); if 
         * (o.equals(RenderingHints.VALUE_STROKE_DEFAULT)) { } else if 
         * (o.equals(RenderingHints.VALUE_STROKE_NORMALIZE)) { } else if 
         * (o.equals(RenderingHints.VALUE_STROKE_PURE)) { } 
         */ 
 
        o = mRh.get(RenderingHints.KEY_ANTIALIASING);
        if (o != null) {
            if (o.equals(RenderingHints.VALUE_ANTIALIAS_DEFAULT)) {
                mP.setAntiAlias(false);
            } else if (o.equals(RenderingHints.VALUE_ANTIALIAS_OFF)) {
                mP.setAntiAlias(false);
            } else if (o.equals(RenderingHints.VALUE_ANTIALIAS_ON)) {
                mP.setAntiAlias(true);
            } 
        } 
 
        o = mRh.get(RenderingHints.KEY_TEXT_ANTIALIASING);
        if (o != null) {
            if (o.equals(RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT)) {
                mP.setAntiAlias(false);
            } else if (o.equals(RenderingHints.VALUE_TEXT_ANTIALIAS_OFF)) {
                mP.setAntiAlias(false);
            } else if (o.equals(RenderingHints.VALUE_TEXT_ANTIALIAS_ON)) {
                mP.setAntiAlias(true);
            } 
        } 
    } 
 
    @Override 
    public void setStroke(Stroke s) {
        if (mP == null) {
            mP = new Paint();
        } 
        BasicStroke bs = (BasicStroke) s;
        mP.setStyle(Paint.Style.STROKE);
        mP.setStrokeWidth(bs.getLineWidth());
 
        int cap = bs.getEndCap();
        if (cap == 0) {
            mP.setStrokeCap(Paint.Cap.BUTT);
        } else if (cap == 1) {
            mP.setStrokeCap(Paint.Cap.ROUND);
        } else if (cap == 2) {
            mP.setStrokeCap(Paint.Cap.SQUARE);
        } 
 
        int join = bs.getLineJoin();
        if (join == 0) {
            mP.setStrokeJoin(Paint.Join.MITER);
        } else if (join == 1) {
            mP.setStrokeJoin(Paint.Join.ROUND);
        } else if (join == 2) {
            mP.setStrokeJoin(Paint.Join.BEVEL);
        } 
    } 
 
    public static float[] createMatrix(AffineTransform Tx) {
        double[] at = new double[9];
        Tx.getMatrix(at);
        float[] f = new float[at.length];
        f[0] = (float) at[0];
        f[1] = (float) at[2];
        f[2] = (float) at[4];
        f[3] = (float) at[1];
        f[4] = (float) at[3];
        f[5] = (float) at[5];
        f[6] = 0;
        f[7] = 0;
        f[8] = 1;
        return f;
    } 
 
    private float[] createAWTMatrix(float[] matrix) {
        float[] at = new float[9];
        at[0] = matrix[0];
        at[1] = matrix[3];
        at[2] = matrix[1];
        at[3] = matrix[4];
        at[4] = matrix[2];
        at[5] = matrix[5];
        at[6] = 0;
        at[7] = 0;
        at[8] = 1;
        return at;
    } 
 
    public static Matrix createMatrixObj(AffineTransform Tx) {
        Matrix m = new Matrix();
        m.reset();
        m.setValues(createMatrix(Tx));
        return m;
    } 
 
    @Override 
    public void setTransform(AffineTransform Tx) {
        mM.reset();
        /* 
         * if(Tx.isIdentity()) { mM = new Matrix(); } 
         */ 
        mM.setValues(createMatrix(Tx));
        Matrix m = new Matrix();
        m.setValues(getInverseMatrix());
        mC.concat(m);
        mC.concat(mM);
    } 
 
    @Override 
    public void shear(double shx, double shy) {
        mM.setSkew((float) shx, (float) shy);