2013-02-21 49 views
3

目前,我將介紹給我們一個名爲PaintPot的Android程序的代碼,該程序允許用戶在他們的Android設備上進行手指繪畫。如何在Android/Java程序中執行同一級別的If語句?

該代碼處理時,點擊屏幕,按鈕被點擊會發生的事件等

// 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語句中。不應該是當用戶在屏幕上點擊或拖動他的手指時,相同的代碼會確定製作的線條或點是否也應該詢問線條或點應該是什麼顏色,而不僅僅是尺寸點,取決於選擇了哪種顏色的按鈕?

爲了更完整的參考,這裏是整個代碼,如果有人好奇:

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

dispatchEvent被稱爲應用程序觸發事件。單擊每個按鈕並與應用程序進行交互時會引發單獨的事件。每個if語句都確定如何處理不同類型的事件。

不應該是當用戶在屏幕上點擊或拖動他的手指時,確定製作的線或點是否也應該詢問線或點應該是什麼顏色的相同代碼,而不僅僅是點的大小,取決於選擇什麼顏色的按鈕

在這種情況下,事件之間的狀態被保存在myCanvas - 點擊btnBlue設置油漆顏色爲藍色畫布(myCanvas.PaintColor(COLOR_BLUE);)上,然後等待另一個事件發生。如果用戶拖過畫布,則繪製一條線(最終爲myCanvas.DrawLine(prevX, prevY, touchX, touchY);)。由於myCanvas記得該狀態,因此它將以藍色繪製線條。

1

如果語句如果用戶點擊或跨 拖動他或她的手指在畫布上同級別

好吧,我可能是錯的,但如果由the same level你的意思是某種平等權重的用戶的行動,那麼答案是肯定的。

它不應該是當用戶點擊或拖動他的整個 屏幕上的手指,決定是否行或由點 也應該問什麼顏色的線或點應在同一代碼而不是 只是大小的點,取決於選擇了哪種顏色的按鈕

是的,它應該。爲此,您將不得不相應編程。

+0

該代碼的工作原理,我只是想知道爲什麼它這樣做。 – user1768884 2013-02-21 19:07:57

1

這是事件處理的常見構造。這可能有助於將「同級別」IF視爲用戶可以選擇的選項。當您在同一個對話框上有兩個按鈕時,通常不會嵌套它們,因爲您無法知道哪個按鈕會被點擊。

該方法的名稱'dispatchEvent'是一個很大的線索,它所做的只是接收事件並選擇適當的代碼路徑。通常,每個IF只是轉過身來調用一個函數或方法,否則調度方法可能會變得很大。

如果你對所有可能的消息都有足夠大的枚舉(Win32做到這一點),你可以用'switch/case'語句而不是所有的IF來做同樣的事情。