在Android/Java程序中,如果语句在同一级别上如何执行?

3

目前,我正在检查一个名为PaintPot的Android程序的代码,它允许用户在其Android设备上进行手指绘画。

该代码处理屏幕被触摸、按钮被点击等事件的发生。

// 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;
}

代码本身能够正常工作并且按照我的期望执行。但是我不太明白的是,如果用户点击或者拖动手指在画布上时的if语句与选择颜色按钮的if语句处于"同一级别"或者说在同一个if语句中。难道不应该当用户点击或者拖动手指在屏幕上时,决定线条或点的代码也应该询问线条或点应该是什么颜色,而不仅仅是点的大小,取决于选中了哪个颜色按钮吗?
以下是完整的参考代码,如果有人感兴趣:
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);
 }


}
3个回答

3

dispatchEvent 方法在应用程序触发事件时被调用。每个按钮点击和与应用程序的交互都会产生不同的事件。每个 if 语句都确定如何处理不同类型的事件。

如果用户点击屏幕或在屏幕上拖动手指,那么不同颜色的线条或点应该询问应该是哪种颜色,而不仅仅是点的大小,这取决于选中了哪个颜色按钮。

在这种情况下,事件之间的状态保存在 myCanvas 中 - 点击 btnBlue 将画笔颜色设置为蓝色 (myCanvas.PaintColor(COLOR_BLUE);) 然后等待另一个事件发生。如果用户在画布上拖动,则会绘制一条线 (最终是 myCanvas.DrawLine(prevX, prevY, touchX, touchY);)。由于 myCanvas 记住了状态,所以它以蓝色绘制线条。


1

如果用户在画布上轻触或拖动手指,则if语句应该是相同的级别

嗯,我可能错了,但是如果你所说的“相同级别”是指对用户操作的某种平等权重,那么答案是肯定的。

当用户在屏幕上轻触或拖动手指时,不仅应该确定所绘制的线条或点的大小,还应该询问线条或点的颜色,具体取决于所选颜色按钮,这个代码也应该请求颜色

是的,应该这样做。为此,您需要进行相应的编程。


代码可以正常运行,我只是想知道为什么要这样做。 - user1768884

1
这是事件处理的常见结构。将“同级”IF视为用户可以选择的选项可能有所帮助。当您在同一个对话框中有两个按钮时,通常不会嵌套它们,因为您无法知道哪个按钮将被单击。
方法名称“dispatchEvent”是一个很大的提示,它只是接收事件并选择适当的代码路径。通常,每个IF都会调用一个函数或方法,否则分派方法可能会变得非常庞大。
如果您有足够大的枚举来处理所有可能的消息(Win32就是这样做的),则可以使用“switch/case”语句代替所有IF。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接