Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are If statements on the same level both executed in an Android/Java Program?

Currently, I am going over the code given to us for an Android program called PaintPot, which allows the users to finger paint on their Android devices.

This code handles the events that will happen when the screen is tapped, buttons are clicked, etc.

// Here is the event dispatcher for our app.  We need to Override the method for the Form
// superclass
@Override
public boolean dispatchEvent(Component component, String id, String eventName,
                             Object[] args) {

    //if the canvas is touched by a tapping finger
    if (component.equals(myCanvas) && eventName.equals("Touched")) {
        canvasTouced(((Float) args[0]).intValue(), ((Float) args[1]).intValue());
        return true;

        //if the canvas is touched by a dragging finger, paint the line this way
    } else if (component.equals(myCanvas) && eventName.equals("Dragged")) {
        drawLine(((Float) args[2]).intValue(),
                ((Float) args[3]).intValue(),
                ((Float) args[4]).intValue(),
                ((Float) args[5]).intValue());
        return true;

        //if the canvas is touched while the blue button is selected
    } else if (component.equals(btnBlue) && eventName.equals("Click")) {
        myCanvas.PaintColor(COLOR_BLUE);
        return true;

        //if the canvas is touched while the green button is selected
    } else if (component.equals(btnGreen) && eventName.equals("Click")) {
        myCanvas.PaintColor(COLOR_GREEN);
        return true;

        //if the canvas is touched while the red button is selected
    } else if (component.equals(btnRed) && eventName.equals("Click")) {
        myCanvas.PaintColor(COLOR_RED);
        return true;

        //if the wipe button is selected
    } else if (component.equals(btnWipe) && eventName.equals("Click")) {
        myCanvas.Clear();
        return true;
    }


    return false;
}

The code works fine and does what I want it to "as-is." But what I don't really understand is that the if statement for if the user taps or drags his or her finger across the canvas is on "the same level" or within the same if statement if the color buttons are selected. Shouldn't it be when the user taps or drags his finger across the screen, the same code that determines if a line or a dot that is made should also ask for what color the line or dot should be, rather than just size of the dot, depending on what color button is selected?

For a more complete reference, here is the entire code, if anyone is curious:

import android.graphics.Color;
import com.google.devtools.simple.runtime.components.android.Button;
import com.google.devtools.simple.runtime.components.Component;
import com.google.devtools.simple.runtime.components.HandlesEventDispatching;
import com.google.devtools.simple.runtime.components.android.Canvas;
import com.google.devtools.simple.runtime.components.android.Form;
import com.google.devtools.simple.runtime.components.android.HorizontalArrangement;
import com.google.devtools.simple.runtime.components.android.Label;
import com.google.devtools.simple.runtime.events.EventDispatcher;


import java.util.Random;




public class PaintPotActivity extends Form implements HandlesEventDispatching {


private Canvas myCanvas; //creates a canvas object
private Label lblStatus; //creates a label that discusses the status of the program
private Button btnRed; //creates a button for red paint
private Button btnBlue; // "" for blue paint
private Button btnGreen; // "" for green paint


private Button btnWipe; //creates a button that wipes the screen clean
private Button btnDotSize; // creates a button that changes the dot size




// Variable (field) used to for displaying number of touches
int numTouches; //declares an integer that lists out the number of touches a user made


// The equivalent to a "main" method for App Inventor apps is the $define method.
void $define() { 


    //We are going to place the color buttons in a HorizontalArrangement
    HorizontalArrangement hr = new HorizontalArrangement(this);
    btnRed = new Button(hr); 
    btnBlue = new Button(hr);
    btnGreen = new Button(hr);


    //set their color
    btnRed.BackgroundColor(Color.RED);
    btnBlue.BackgroundColor(Color.BLUE);
    btnGreen.BackgroundColor(Color.GREEN);

    //set the button text
    btnRed.Text("Red");
    btnBlue.Text("Blue");
    btnGreen.Text("Green");


    //canvas into its own HorizontalArrangement
    hr = new HorizontalArrangement(this);
    myCanvas = new Canvas(hr);
    myCanvas.Width(400);
    myCanvas.Height(400);
    myCanvas.LineWidth(10);


    //Wipe and a label into its own HorizontalArrangement
    hr = new HorizontalArrangement(this);
    btnWipe = new Button(hr);
    btnWipe.Text("Wipe");


    lblStatus = new Label(hr);
    lblStatus.Text("  touchX/touchY:");


    // Register for events.  By the second argument can be any string.    The third argument must
    // exactly match the name of the event that you want to handle for that component.  When the event
    // happens, dispatchEvent will be called with these arguments.
    EventDispatcher.registerEventForDelegation(this, "JavaBridge", "Touched");
    EventDispatcher.registerEventForDelegation(this, "JavaBridge", "Click");
    EventDispatcher.registerEventForDelegation(this, "JavaBridge", "Dragged");
}


// Here is the event dispatcher for our app.  We need to Override the method for the Form
// superclass
@Override
public boolean dispatchEvent(Component component, String id, String eventName,
                             Object[] args) {

    //if the canvas is touched by a tapping finger
    if (component.equals(myCanvas) && eventName.equals("Touched")) {
        canvasTouced(((Float) args[0]).intValue(), ((Float) args[1]).intValue());
        return true;

        //if the canvas is touched by a dragging finger, paint the line this way
    } else if (component.equals(myCanvas) && eventName.equals("Dragged")) {
        drawLine(((Float) args[2]).intValue(),
                ((Float) args[3]).intValue(),
                ((Float) args[4]).intValue(),
                ((Float) args[5]).intValue());
        return true;

        //if the canvas is touched while the blue button is selected
    } else if (component.equals(btnBlue) && eventName.equals("Click")) {
        myCanvas.PaintColor(COLOR_BLUE);
        return true;

        //if the canvas is touched while the green button is selected
    } else if (component.equals(btnGreen) && eventName.equals("Click")) {
        myCanvas.PaintColor(COLOR_GREEN);
        return true;

        //if the canvas is touched while the red button is selected
    } else if (component.equals(btnRed) && eventName.equals("Click")) {
        myCanvas.PaintColor(COLOR_RED);
        return true;

        //if the wipe button is selected
    } else if (component.equals(btnWipe) && eventName.equals("Click")) {
        myCanvas.Clear();
        return true;
    }


    return false;
}




/**
 * This method will get the touched touchX, touchY coordinates and will then create a circle
 * of random radius (between 1 to 33) with the color that was selected (RED, BLUE or GREEN).
 * It will also display the touched touchX,touchY coordinates.
 * @param x current x
 * @param y current y
 */
private void canvasTouced(int x, int y) {


    myCanvas.DrawCircle(x, y, new Random().nextInt(33));
    lblStatus.Text("  touchX/touchY:" + x + "/" + y + " touches: " + ++numTouches);


}


/**
 * Method to draw line
 * @param prevX last touch x
 * @param prevY last touch y
 * @param touchX current x
 * @param touchY current y
 */
private void drawLine(int prevX, int prevY, int touchX, int touchY) {
    myCanvas.DrawLine(prevX, prevY, touchX, touchY);
 }


}
like image 476
user1768884 Avatar asked Dec 04 '25 13:12

user1768884


2 Answers

dispatchEvent is called as the application fires events. Separate events get thrown for each button click and interaction with the app. Each of the if statements is determining how different types of events get handled.

Shouldn't it be when the user taps or drags his finger across the screen, the same code that determines if a line or a dot that is made should also ask for what color the line or dot should be, rather than just size of the dot, depending on what color button is selected

In this case, state between events is being saved in the myCanvas - clicking btnBlue sets the paint color to blue on the canvas (myCanvas.PaintColor(COLOR_BLUE);) then waits for another event to happen. If the user then drags across the canvas, a line is drawn (ultimately myCanvas.DrawLine(prevX, prevY, touchX, touchY);). Since myCanvas remembers the state, it draws the line in blue.

like image 181
Nate Avatar answered Dec 07 '25 04:12

Nate


if statement for if the user taps or drags his or her finger across the canvas is on the same level

Well, I may be wrong but if by the same level you mean some kind of equal weightage to user's action then answer is yes.

Shouldn't it be when the user taps or drags his finger across the screen, the same code that determines if a line or a dot that is made should also ask for what color the line or dot should be, rather than just size of the dot, depending on what color button is selected

Yes, it should. For this, you will have to program accordingly.

like image 28
Nitish Avatar answered Dec 07 '25 03:12

Nitish



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!