在codenameone中从一点到另一点画直线

时间:2019-01-16 16:14:25

标签: codenameone

我试图在用户拖动手指时从一个点到另一点画一条直线。因此,我可以划清界限。但是,如果我尝试添加下一条画线,则第一条画线将消失。

Override
    public void pointerDragged(int x, int y) {
        super.pointerDragged(x, y);
        if (MainMenuScreen.inSentFolder()) {
            return;
        }

        //adjust by taking off absolute x and y
        if (photoImage == null) {
            x = x - getX();

            y = y - getY();

            //adjust by viewerOffsets
            x = x - viewerXOffset;
            y = y - viewerYOffset;
        }
        else {

            x = x - imageDrawnFromX;
            y = y - imageDrawnFromY;
        }
     if(shapeType == ShapeTypes.LINE) {
     // it holds the x/y co-ordinates value as user drags from one point to another.
                    model.addLine(lastXPos,  lastYPos,  x, y, brushCol, brushGirth, brushShapeSize, imageToUse == null? -1 : imageToUse.getWidth(), imageToUse == null ? -1 : imageToUse.getHeight(), ShapeTypes.LINE,true);

                }

            }

            invalidate();       
        }
    }

    @Override
    public void paint(Graphics g) {
    super.paint(g);
        for (int i = 0; i < model.getInfo().size(); i++) {
            Line currentLine = (Line) model.getInfo().elementAt(i);
            g.setColor(currentLine.col);
            boolean drawme=false;

                        if(currentLine.guideLineOnly && (i == 
                                graffitiModel.getInfo().size() - 1))
                            drawme = true;

                        if(!currentLine.guideLineOnly)
                            drawme = true;

                        if(drawme) {
                          p.moveTo(currentLine.startX + imageDrawnFromX, currentLine.startY + imageDrawnFromY);
                          p.lineTo(currentLine.endX + imageDrawnFromX, currentLine.endY + imageDrawnFromY);
                        }

                            g.drawShape(p, stroke);
    }

请帮助我,因为我能够绘制,所以当我尝试绘制第二行时它就消失了。 谢谢

0 个答案:

没有答案