Totalcross - Whiteboard starts stroke with point difference initially pressed

Asked

Viewed 79 times

3

I’m using the component Whiteboard, for signatures, however when making a dash, it draws with a difference from the starting point where I pressed the screen, making a vertical risk of approximately 30 points, so do the stroke pressed on the screen, How do I solve this???

  • The class Whiteboard is open, goes together in the SDK. Feel free to derive it or create a new class based on it. Just a minute for a more complete answer

3 answers

2

The class Whiteboard works with the concept of several small traits. At each moment, it captures the point where the touch is and draws a line compared to the previous point.

The magic of this concept of small strokes on the screen is in the method drawTo within the Whiteboard; given a new point, knowing the previous point, draw the line:

private void drawTo(Graphics g, int pex, int pey)
{
    g.drawLine(oldX,oldY,pex,pey); // guich@580_34: draw directly on screen
    if (thick)
    {
        g.drawLine(oldX+1,oldY+1,pex+1,pey+1);
        g.drawLine(oldX-1,oldY-1,pex-1,pey-1);
        g.drawLine(oldX+1,oldY+1,pex-1,pey-1);
        g.drawLine(oldX-1,oldY-1,pex+1,pey+1);
    }
}

Note: if connected to make a thick drawing, it will make an additional set of parallel lines, as seen in if (thick).

In the case of the first touch (event of type PEN_DOWN), he ends up drawing a zero pixel segment, what turns a dot on the screen:

public onEvent(Event event)
{
    PenEvent pe;
    switch (event.type)
    {
    [...]
    case PenEvent.PEN_DOWN:
        pe = (PenEvent)event;
        oldX = pe.x;
        oldY = pe.y;
        drawTo(gImg, pe.x,pe.y); // after
        if (gScr != null) drawTo(gScr,pe.x,pe.y);
        getParentWindow().setGrabPenEvents(this); // guich@tc100: redirect all pen events to here, bypassing other processings
        if (Settings.isOpenGL) te = addTimer(100);
        break;
    [...]
} [fim do onEvent]

While dragging touch (event of type PEN_DRAG), for each drag catch the Whiteboard will make a dash from the previous touch to the current touch:

public onEvent(Event event)
{
    PenEvent pe;
    switch (event.type)
    {
    [...]
    case PenEvent.PEN_DRAG:
        pe = (PenEvent)event;
        drawTo(gImg, pe.x,pe.y); // before
        if (gScr != null) drawTo(gScr,pe.x,pe.y);
        oldX = pe.x;
        oldY = pe.y;
        if (!Settings.isOpenGL) Window.safeUpdateScreen(); // important at desktop!
        break;
    [...]
} [fim do onEvent]

In this case, the sensitivity from which the system picks up the click point (pe.x and pe.y) depends on your finger and also on the device. So far and the tests performed, the accuracy has been well satisfactory.

2

public onEvent(Event event) 
{
    PenEvent pe; 
    switch (event.type) 
    { 
    [...] 
    case PenEvent.PEN_DOWN: 
        pe = (PenEvent)event; 
        oldX = pe.x; 
        oldY = pe.y;
        drawTo(gImg, pe.x,pe.y); // after 
        if (gScr != null) drawTo(gScr,pe.x,pe.y);
      //  getParentWindow().setGrabPenEvents(this); // guich@tc100: redirect all //pen events to here, bypassing other processings
        if (Settings.isOpenGL) te = addTimer(100);
        break;
    [...]
} [fim do onEvent]

Includes the class in the project and after debugging, I commented the line with the command

getParentWindow().setGrabPenEvents(this);

and worked correctly as expected.

What should I have done so that it was not necessary to comment on this line???

  • Was it only java/debugging level? Or confirmed on the device as well?

  • Only the level of debugging, on the device did not work, loses the function of making the trace.

  • Hmmm... So you’re saying that in the original, the detection of the initial touch was problematic, but when you removed the setGrapPenEvents in thesis corrects the initial touch, but cannot detect drag?

  • Starts the trace from the right point, but in a moment it loses the scratching function, assuming the touch function for selection.

  • It must be a side effect of not having setGrabPenEvents... I’ll try to give you some simulations, after the experiment I’ll be back

  • Includes an answer with an adjustment to meet my need. Analyzing the code I pasted will better understand what is occurring.

Show 1 more comment

0


     case PenEvent.PEN_DOWN:
        pe = (PenEvent)event;
        oldX = pe.x;
        oldY = pe.y;
        yIni = pe.y;       <=======
        drawTo(gImg, pe.x,pe.y); // after
        if (gScr != null) drawTo(gScr,pe.x,pe.y);
        getParentWindow().setGrabPenEvents(this); // guich@tc100: redirect all pen events to here, bypassing other processings
        if (Settings.isOpenGL) te = addTimer(100);
        break;
     case PenEvent.PEN_DRAG:
        pe = (PenEvent)event;
        if (yIni>0) {                
            yDif = yIni -pe.y;         
            yIni = 0;                 
        }
        drawTo(gImg, pe.x,pe.y + yDif); // before
        if (gScr != null) drawTo(gScr, pe.x, pe.y + yDif);  
        oldX = pe.x;
        oldY = pe.y + yDif;
        if (!Settings.isOpenGL) Window.safeUpdateScreen(); // important at desktop!
        break;

I adjusted the routine this way, calculated the difference of pe. y of the PEN_DOWN event with pe. y of the PEN_DRAG event and applied it to pe. y to trace the line.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.