user3132352
user3132352

Reputation: 403

JAVA Draw line with swt not deleting previous lines

I have an application where onMouseDown starts drawing a line and the line is drawn when onMouseUp. The problem I have is that when I move my mouse the previous lines stays. How can I do it to work normally and when the line is drawn, the previous are deleted? To get it more clear I'm posting a screanshots: 1

2

And here is my code:

public class MainClass {

    private static Point fp;

    private static Point lp;

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Display d = new Display();
        Shell shell = new Shell(d);

        shell.setLayout(new FillLayout());
        Canvas c = new Canvas(shell, SWT.NONE);
        c.setSize(100, 100);
        c.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));

        shell.open();

        createPaintListener(c);
        createMouseListener(c);
        createMoveListener(c);
        while(!shell.isDisposed()) {
            if(!d.readAndDispatch()) {
                d.sleep();

            }
        }

        d.dispose();
    }

    private static void createMoveListener(final Canvas c) {
        // TODO Auto-generated method stub
        c.addMouseMoveListener(new MouseMoveListener() {

            @Override
            public void mouseMove(MouseEvent e) {
                // TODO Auto-generated method stub
                if (fp != null) {
                    GC gc = new GC(c);
                    if(lp != null) {
                        gc.setXORMode(true);
                        gc.drawLine(fp.x, fp.y, lp.x, lp.y);

                        lp = new Point(e.x, e.y);
                        gc.drawLine(fp.x, fp.y, lp.x, lp.y);
                    }else {
                        lp = new Point(e.x, e.y);
                    }

                    gc.dispose();
                }
            }
        });
    }

    private static void createMouseListener(final Canvas c) {
        c.addMouseListener(new MouseListener() {

            @Override
            public void mouseDoubleClick(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseDown(MouseEvent e) {
                // TODO Auto-generated method stub
                if (fp == null) {
                    fp = new Point(e.x, e.y);
                } else {

                }
            }

            @Override
            public void mouseUp(MouseEvent e) {
                // TODO Auto-generated method stub
                GC gc = new GC(c);
                gc.drawLine(fp.x, fp.y, e.x, e.y);
                gc.dispose();
                fp = null;
            }

        });

    }

    private static void createPaintListener(Canvas c) {
        c.addPaintListener(new PaintListener() {

            @Override
            public void paintControl(PaintEvent e) {
                // TODO Auto-generated method stub
            }
        });
    }

}

Upvotes: 0

Views: 1160

Answers (1)

Helper Guest
Helper Guest

Reputation: 11

Remove all the drawing code from the mouse listeners. The drawing should only occur inside paint(). Even though you keep newing up a GC() in the mouse listeners, internally, they all point to the same memory buffer that gets output to the screen, so you're drawing a new line every time the mouse moves, on top of the old buffer. In paint(), you're getting the an empty buffer to start with.

private static void createPaintListener(Canvas c) {
    c.addPaintListener(new PaintListener() {

        @Override
        public void paintControl(PaintEvent e) {
            e.gc.drawLine(fp.x, fp.y, lp.x, lp.y);
        }
    });
}

Upvotes: 1

Related Questions