Jurgen Feuchter
Jurgen Feuchter

Reputation: 604

Using SWT and canvas, Moving line drawn with button

I come to you in time of need once again. hehehe Ive done a little SWT which makes lines, circles, squares and triangles. I do this by taking the start point and the end point to draw all of these figures. once drawn I want to move it in x and y axis. Ill put here the core of where im having trouble. Im maing the lines by having a mouse listener inside the canvas, and hearing when i clicks down and lets go the click. //This is the listener

MasX.addListener(SWT.Selection, new Listener(){

        public void handleEvent(Event event) {
            SX += 1;
            EX += 1;
            canvas.redraw();
            //gc.drawLine(SX, SY, EX, EY);
            //crearLinea(SX,SY,EX,EY,gc);

        }

    });

//This is the line drawing method

public void crearLinea(int SX, int SY, int EX, int EY, GC gc){

    int x1 = SX;
    int x2 = EX;
    int y1 = SY;
    int y2 = EY;
    System.out.println(SX+" "+SY+" "+EX+" "+EY);
    int cont = 0;
    float dx, dy, m, y, x;
    if (x1>x2){
        int ax = x2;
        int ay = y2;
        x2 = x1;
        x1 = ax;
        y2 = y1;
        y1 = ay;
    }
    dx = x2 - x1;
    dy = y2 - y1;
    m = dy/dx;
    if (m>=-1&&m<=1){
        y = y1;
        //System.out.println(m+" "+x1+" "+y1+"....."+x2+" "+y2);
        for (x = x1 ; x <= x2;x=x+5){
            //if (x>=dot&&x<=dot+10||x>=dot*2&&x<=dot*2+10||x>=dot*3&&x<=dot*3+10){
                gc.drawLine((int)x, (int)Math.round(y), (int)x, (int)Math.round(y));
                y+=m*5;
            //}
        }   
    }
    if(m>1){
        x = x1;
        //System.out.println(m+" "+x1+" "+y1+"....."+x2+" "+y2);
        for (y = y1 ; y <= y2;y=y+3){
            if (cont<=5){
            //System.out.println(cont);
            gc.drawLine((int)Math.round(x), (int)y, (int)Math.round(x), (int)y);
            cont++;
            }
            if(cont>=10){
            //System.out.println(cont);
            cont=0;
            }
            if(cont>5){
            //System.out.println(cont);
            cont++;
            }
            x+=(1/m)*3; 
        }
    }
    if(m<-1){
        x = x1;
        //System.out.println(m+" "+x1+" "+y1+"....."+x2+" "+y2);
        for (y = y1 ; y >= y2;y--){
            if(y%10==0||y%8==0||y%12==0){
            gc.drawLine((int)Math.round(x), (int)y, (int)Math.round(x), (int)y);
            }
            x-=(1/m);   
        }
    }

}

The error it gives me is Null pointer exception and points to the gc.drawline()

Hope someone can help me :D

Upvotes: 0

Views: 758

Answers (2)

Jurgen Feuchter
Jurgen Feuchter

Reputation: 604

Like Greg told me to, I used the method canvas.redraw() which calles upon the canvas.addPaintListener(); method which then gives me the event im looking for to create the drawing I want. Here is the code to call it from the button:

MasX.addListener(SWT.Selection, new Listener(){

        public void handleEvent(Event event) {
            SX += 10;
            EX += 10;
            System.out.println(SX+" "+SY+" "+EX+" "+EY);
            canvas.redraw();
            System.out.println("Aqui 2");
            //gc.drawLine(SX, SY, EX, EY);
            //crearLinea(SX,SY,EX,EY,gc);

        }

    });

And the core to call all the other methods which make the lines squares etc.

canvas.addPaintListener(new PaintListener() {

        @Override
        public void paintControl(PaintEvent event) {
            if(btnLinea.getSelection()){
                gc = event.gc;

                crearLinea(SX,SY,EX,EY,gc);

            }
            if(btnCirculo.getSelection()){
                gc = event.gc;

                crearCirculo(SX,SY,EX,EY,gc);

            }
            if(btnCuadrado.getSelection()){
                gc = event.gc;

                crearCuadro(SX,SY,EX,EY,gc);

            }
            if(btnTriangulo.getSelection()){
                gc = event.gc;

                crearTriangulo(SX,SY,EX,EY,gc);

            }
        }
    });

Hope it helps anyone :D

Upvotes: 0

greg-449
greg-449

Reputation: 111142

The Event.gc is only set for SWT.Paint events, for the SWT.Selection listener you are using it will be null.

You can call one of the Control.redraw methods in the selection listener to request that part or all of the control be redrawn, this will generate a paint event.

Upvotes: 1

Related Questions