Use of constant in Canvas.drawLine()

Go To StackoverFlow.com

0

I have this code. Only the first drawLine gets drawn and the rest 2 are not. Can anyone explain why the other two drawlines do not work in the present case? They work if I replace "factor" with "1/2" in the drawLine() statements. Thanks

public class RenderView extends View {

Paint paint;
private float factor = 1/2;

public RenderView(Context context) {
    // TODO Auto-generated constructor stub
    super(context);
    paint = new Paint(Paint.ANTI_ALIAS_FLAG);
}

@Override
protected void onDraw(Canvas canvas) {

    int screenWidth = canvas.getWidth();
    int screenHeight = canvas.getHeight();

    paint.setColor(Color.RED);


    canvas.drawLine(0, 0, screenWidth, screenHeight, paint);

    canvas.drawLine(factor*screenWidth, 0, factor*screenWidth, screenHeight, paint);


    canvas.drawLine(0, factor*screenHeight, screenWidth, factor*screenHeight, paint);

    invalidate();       
}

}

2012-04-05 15:55
by Souvik Basu


1

1/2 is 0 (by integer division). Try 1f/2 or just 0.5f.

2012-04-05 15:57
by Che Jami


1

I think the problem is that 1/2 is integer division, and so 1/2 = 0. Try 0.5f instead.

2012-04-05 15:58
by JRaymond
Ads