0

In my Application if the brush size is selected it changes the brush size but it also changes the size of the brush inside the canvas how to make it so that the brush size inside the canvas doesn't change.

This is my code for the EasyOne.Java - This contains contain the brushsizes:

import static com.example.artbit.display2.colorList1;
import static com.example.artbit.display2.currentbrush1;
import static com.example.artbit.display2.pathlist1;

public class EasyOne extends AppCompatActivity {
public static Path path1 = new Path();

public static Paint paintbrush1 = new Paint();
public static String brushsize1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_easy_one);

This is for the brushes

public void SmallBrush1(View view){
    paintbrush1.setStrokeWidth(10f);
    paintbrush1.setColor(Color.BLACK);

    currentColor1(paintbrush1.getColor());
}

public static void BigBrush1(View view){
   /* brushsize1 = "1";

    paintbrush1.setColor(Color.BLACK);
    currentColor1(paintbrush1.getColor());*/


    paintbrush1.setStrokeWidth(50f);
    currentColor1(paintbrush1.getColor());
}

public void Brush1(View view) {

    MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.selecttools);
    mediaPlayer.start();

    paintbrush1.setStrokeWidth(20f);
    paintbrush1.setColor(Color.BLACK);
    currentColor1(paintbrush1.getColor());
}

public void Clear1(View view){
    MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.selecttools);
    mediaPlayer.start();

    pathlist1.clear();
    path1.reset();
    colorList1.clear();

}




public static void currentColor1 (int c){
    currentbrush1 = c;
    path1 = new Path();
}

public static void currentsizebrush1(int c){
    currentbrush1 = c;
    path1 = new Path();

}

}

and this is the code for display which is the canvas this contains the pathlist and arrays for colors

import static com.example.artbit.EasyOne.paintbrush1;
import static com.example.artbit.EasyOne.path1;
import static com.example.artbit.EasyOne.brushsize1;
import android.graphics.Paint;
import android.graphics.Path;

public class display2 extends View {

public static MediaPlayer player;
    public static ArrayList<Path> pathlist1 = new ArrayList<>();
    public static ArrayList<Path> pathlist2 = new ArrayList<>();
    public static ArrayList<Integer> brushList1 = new ArrayList<>();
    public static ArrayList<Integer> colorList1 = new ArrayList<>();


    public ViewGroup.LayoutParams params;

    public static int currentbrush1 = Color.BLACK;

    public display2(Context context) {
        super(context);
        init(context);
    }

    public display2(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public display2(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context){
        
            paintbrush1.setAntiAlias(true);
            paintbrush1.setColor(Color.BLACK);
            paintbrush1.setStyle(Paint.Style.STROKE);
            paintbrush1.setStrokeCap(Paint.Cap.ROUND);
            paintbrush1.setStrokeJoin(Paint.Join.ROUND);
            paintbrush1.setStrokeWidth(10f);
        




        params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        float x = event.getX();
        float y = event.getY();

        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                path1.moveTo(x,y);


                invalidate();
                return true;

            case MotionEvent.ACTION_MOVE:
                path1.lineTo(x,y);


                pathlist1.add(path1);


                colorList1.add(currentbrush1);
                brushList1.add(currentbrush1);

                invalidate();
                return true;
                default:
                return false;
        }
    }

    @Override
    protected void onDraw(android.graphics.Canvas canvas) {
        for (int i=0; i<pathlist1.size(); i++){
            paintbrush1.setColor(colorList1.get(i));
            paintbrush1.setStrokeWidth(brushList1.get(i));
            canvas.drawPath(pathlist1.get(i),paintbrush1);
            invalidate();
        }

    }
}

1 Answers1

1

I think I figured out the problem, the problem is in your onTouchEvent at

brushList1.add(currentbrush1);

You are adding to the ArrayList of brush's size the Color of the Path which is Black by default is -16777216 so when in draw you set the strokeWidth to a negative value it uses the Paint current value. So create a static int strokeWidth inside display2 and add to all your brush methods strokeWidth = //width number; like so

public void SmallBrush1(View view){
    paintbrush1.setStrokeWidth(10f);
    strokeWidth = 10; //ADD THIS
    paintbrush1.setColor(Color.BLACK);
    
    currentColor1(paintbrush1.getColor());
}

Do for all your brush methods

So change this

brushList1.add(currentbrush1);

to

brushList1.add(strokeWidth);

this should fix your problem

Hope that helps

Mirco0
  • 306
  • 1
  • 2
  • 8