Forgot the code.

On Tuesday, October 11, 2016 at 10:47:47 AM UTC+10, Bryan Buchanan wrote:
>
> I've adapted the code from the SignatureComponent (see Form2.java), which 
> works fine. I've also tried some native Android code - 
>
>
> http://www.mysamplecode.com/2011/11/android-capture-signature-using-canvas.html
>
> They both work pretty much the same, but the native Android code is 
> noticeably more responsive (on a Nexus 7).  The SignatureComponent code 
> looks pretty simple, so I guess there's maybe nothing to be done to make it 
> work a little bit smoother ?
>

-- 
You received this message because you are subscribed to the Google Groups 
"CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to codenameone-discussions+unsubscr...@googlegroups.com.
Visit this group at https://groups.google.com/group/codenameone-discussions.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/codenameone-discussions/31e7f0f9-3b47-44e6-bf46-852adfe64617%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.webbtide.tpd.timeclock;

import com.codename1.ui.Button;
import com.codename1.ui.Component;
import com.codename1.ui.Container;
import com.codename1.ui.Display;
import com.codename1.ui.Graphics;
import com.codename1.ui.Image;
import com.codename1.ui.Stroke;
import com.codename1.ui.events.ActionEvent;
import com.codename1.ui.events.ActionListener;
import com.codename1.ui.geom.GeneralPath;
import com.codename1.ui.layouts.BorderLayout;
import com.codename1.ui.layouts.GridLayout;
import com.codename1.util.StringUtil;
import java.util.Calendar;
import java.util.List;
import java.util.TimeZone;

/**
 * @author bryanb
 */
public class Form2 extends BJBForm {

    SignaturePanel canvas;
    Image signatureImage;

    public Form2() {
    }

    @Override
    public Form2 init() {

        setTitle("Enter Signature");
        setLayout(new BorderLayout());
        setScrollableX(false);
        setScrollableY(false);

        canvas = new SignaturePanel();
        canvas.setUIID("BorderContainer");

        Container c = new Container(new GridLayout(1, 2));

        Button clear = new BorderButton("Clear");
        clear.addActionListener((ActionListener) (ActionEvent evt) -> {
            canvas.clear();
        });
        c.add(clear);

        Button save = new BorderButton("Save");
        save.addActionListener((ActionListener) (ActionEvent evt) -> {

            /**
             * save stuff and talk
             */
            String employee = CommonContext.INSTANCE.getEmployee();
            List<String> l = StringUtil.tokenize(employee, " ");
            String name = l.get(0);

            Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
            int hour = calendar.get(Calendar.HOUR_OF_DAY);

            String talk;
            if (hour > 11) {
                talk = "Good Afternoon " + name + ", enjoy your evening";
            } else {
                talk = "Good Morning " + name + ", have a good day";
            }
            if (CommonContext.INSTANCE.getTts() != null && CommonContext.INSTANCE.getTts().isSupported()) {
                CommonContext.INSTANCE.getTts().say(talk);
            }

            signatureImage = canvas.getImage();
            /**
             * get the image from the component
             */
            /*
                Image buffer = Image.createImage(canvas.getWidth(), canvas.getHeight());
                Graphics g = buffer.getGraphics();

                g.translate(-canvas.getAbsoluteX(), -canvas.getAbsoluteY());
                canvas.paintComponent(g);
                g.translate(canvas.getAbsoluteX(), canvas.getAbsoluteY());

                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                ImageIO.getImageIO().save(buffer, stream, ImageIO.FORMAT_PNG, 1);
                byte[] byteArray = stream.toByteArray();

                String base64 = Base64.encodeNoNewline(byteArray);
             */
            /**
             * return to first form
             */
            CommonContext.INSTANCE.getForm1().show();

        });
        c.add(save);

        addComponent(BorderLayout.SOUTH, c);
        addComponent(BorderLayout.CENTER, canvas);

        setBackCommand(CommonContext.INSTANCE.getForm1());

        return this;
    }

    @Override

    protected void onShowCompleted() {
        super.onShowCompleted();

    }

    public class BorderButton extends Button {

        int h = Display.getInstance().convertToPixels(8, false);

        public BorderButton() {
            super("");
            setUIID("BorderButton");
            setPreferredH(getPreferredH() < h ? h : getPreferredH());
        }

        public BorderButton(String s) {
            super(s);
            setUIID("BorderButton");
            setPreferredH(getPreferredH() < h ? h : getPreferredH());
        }
    }

    class SignaturePanel extends Component {

        private final GeneralPath path = new GeneralPath();
        private final Stroke stroke = new Stroke();
//        private final Rectangle signatureRect = new Rectangle();
//        private final Font xFont;

        public SignaturePanel() {
            stroke.setLineWidth(Math.max(1, Display.getInstance().convertToPixels(1, true) / 2));
            getAllStyles().setBgColor(0xffffff);
            getAllStyles().setBgTransparency(255);
//            xFont = Font.createSystemFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE);
        }

        /**
         * Overridden to try to make this component as sensitive as possible to
         * drag events. If we don't do this, it requires a longer drag before
         * the "drag" events will kick in.
         *
         * @param x
         * @param y
         * @return
         */
        @Override
        protected int getDragRegionStatus(int x, int y) {
            return Component.DRAG_REGION_LIKELY_DRAG_XY;
        }

        /**
         *
         * @param g
         */
        @Override
        public void paint(Graphics g) {
            super.paint(g);

//            g.setColor(0x666666);
//            calcSignatureRect(signatureRect);
//            g.drawRect(signatureRect.getX(), signatureRect.getY(), signatureRect.getWidth(), signatureRect.getHeight());
//            g.drawString("X", signatureRect.getX() + Display.getInstance().convertToPixels(1, true), signatureRect.getY() + signatureRect.getHeight() / 2);
            paintSignature(g);
        }

        /**
         * Paints just the signature portion of the panel. This is is reused to
         * also create the image of the signature.
         *
         * @param g
         */
        private void paintSignature(Graphics g) {
            g.setColor(0x0);
            boolean oldAA = g.isAntiAliased();
            g.setAntiAliased(true);
            g.drawShape(path, stroke);
            g.setAntiAliased(oldAA);
        }

        /**
         * Calculates a rectangle (in parent component space) used for the drawn
         * "rectangle" inside which the user should draw their signature. It
         * tries to create a 16x9 rectangle that fits inside the component with
         * a bit of padding (3mm on each edge).
         *
         * @param r Output variable.
         */
        /*
        private void calcSignatureRect(Rectangle r) {
            int w = getWidth() - Display.getInstance().convertToPixels(6, true);
            int h = (int) (w * 9.0 / 16.0);
            if (h > getHeight()) {
                h = getHeight() - Display.getInstance().convertToPixels(6, false);
                w = (int) (h * 16.0 / 9.0);
            }
            r.setX(getX() + (getWidth() - w) / 2);
            r.setY(getY() + (getHeight() - h) / 2);
            r.setWidth(w);
            r.setHeight(h);
        }
        */
        /*
        @Override
        protected Dimension calcPreferredSize() {
            Display d = Display.getInstance();
            return new Dimension(d.convertToPixels(100, true), d.convertToPixels(60, false));
        }
        */
        @Override
        public void pointerPressed(int x, int y) {
            path.moveTo(x(x), y(y));
            repaint();
        }

        @Override
        public void pointerDragged(int x, int y) {
            path.lineTo(x(x), y(y));
            repaint();
        }

        @Override
        public void pointerReleased(int x, int y) {
            repaint();
        }

        /**
         * Converts an x coordinate from screen space, to parent component
         * space.
         *
         * @param x
         * @return
         */
        private int x(int x) {
            return x - getParent().getAbsoluteX();
        }

        /**
         * Converts a y coordinate from screen space to parent component space.
         *
         * @param y
         * @return
         */
        private int y(int y) {
            return y - getParent().getAbsoluteY();
        }

        /**
         * Gets the currently drawn signature as an image. This only includes
         * the areas inside the {@link #signatureRect}
         *
         * @return
         */
        public Image getImage() {
//            calcSignatureRect(signatureRect);

//            Image img = Image.createImage(signatureRect.getWidth(), signatureRect.getHeight(), 0xffffff);
            Image img = Image.createImage(getWidth(), getHeight(), 0xffffff);
            Graphics g = img.getGraphics();
//            g.translate(-signatureRect.getX(), -signatureRect.getY());
            g.translate(-getX(), -getY());
            paintSignature(g);
            return img;
        }

        /**
         * Resets the signature as a blank path.
         */
        public void clear() {
            path.reset();
            repaint();
        }
    }
}

Reply via email to