Problem: Canvas onDraw Android
-
Hello! I have been trying to draw different rectangles on the canvas after several times of button click. It should display different colored rectangle and the rectangle should remain on canvas after every button click. The rectangles should be able to move around the canvas. I have 4 buttons on my main.xml file. After invoking the first button, a blue colored rectangle shld appear on the canvas. If the first button is invoked the second time, another blue colored rectangle shld appear as well however the code i have right now only appear one blue rectangle regardless the number of times the button is invoked. Here are my codes:
public class DrawRectangle extends View {
public DrawRectangle(Context context){
super(context);
}
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);Rect ourRect = new Rect(); ourRect.set(0, 0, canvas.getWidth()/4, canvas.getHeight()/4); Paint paintColor = new Paint(); // make the entire canvas white paintColor.setColor(Color.WHITE); canvas.drawPaint(paintColor); if(MainActivity.isBlue){ paintColor.setColor(Color.BLUE); } paintColor.setStyle(Paint.Style.FILL); //Draw to actual canvas canvas.drawRect(ourRect, paintColor); canvas.save(); if(MainActivity.isRed){ paintColor.setColor(Color.RED); } paintColor.setStyle(Paint.Style.FILL); //Draw to actual canvas canvas.drawRect(ourRect, paintColor); canvas.save(); if(MainActivity.isYellow){ paintColor.setColor(Color.YELLOW); } paintColor.setStyle(Paint.Style.FILL); canvas.save(); //Draw to actual canvas canvas.drawRect(ourRect, paintColor); if(MainActivity.isGreen){ paintColor.setColor(Color.GREEN); } paintColor.setStyle(Paint.Style.FILL); //Draw to actual canvas canvas.drawRect(ourRect, paintColor); canvas.save(); } }
and this is my code for my first button:
public static boolean isBlue, isRed, isYellow, isGreen;
DrawRectangle dv;bluebutton.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View arg0) { // TODO Auto-generated method stub isBlue = true; isRed = false; isGreen = false; isYellow = false; dv.inv