0

此刻,当我击中F或时f

    private static final char FILL_POLYGON_LOWERCASE = 'f';
    private static final char FILL_POLYGON = 'F';

@Override
public void keyTyped(KeyEvent keyEvent) 
{
    PolygonFiller polyFiller = new PolygonFiller();
    char key = keyEvent.getKeyChar();

    switch(key)
    {
        /**
         *  Fill the polygons 
         */
        case FILL_POLYGON:      
        {
            if (greenLightForFilling == true)
            {
                fillPolygon(polyFiller);

                System.out.print("called");
            }

            break;
        }  // end FILL_POLYGON


        case FILL_POLYGON_LOWERCASE:
        {
            if (greenLightForFilling == true)
            {
                fillPolygon(polyFiller);
            }
            break;  
        }
...

}

程序进入fillPolygon(polyFiller);.

意思是,当我第一次打的时候f,我就进去了fillPolygon()

我怎样才能进入其他方法,例如other(),当我击球fF 再次击球时?

谢谢

4

3 回答 3

1

所以问题是,如果你点击 f/F 你去填充多边形,然后再次按 f/F 将调用 other()。

这可以是有状态类的经典案例。

在类级别有一个属性。

并在输入 f/F 时检查该值并将其加一。再次输入 f/F 时,检查该值并将其加一。

在每次增量之前,您应该检查是否,

//Am assuming that there are more than two functions, else could use boolean

if (value == 1) {
    fillpolygon();
}

else if (value == 2) {
    other();
}
else if (value == 2) {
    some_other();
}

请记住,入口点将是一个函数,从那里根据与此类似的检查来委派流程。

希望这可以帮助。

于 2013-04-02T06:35:40.153 回答
0

我怎样才能进入其他方法,例如other(),当我击球fF再次击球时?

您需要引入一个布尔标志。

这是一个非常简单的状态机示例。

于 2013-04-02T06:38:00.387 回答
0

将先前使用的按钮存储在变量中,例如“currentCommand”和“previousCommand”。每次检测到新输入时,您都会将当前输入放在上一个并将新输入存储在当前成员中。或者,如果您想要超过最后两个按键,请使用堆栈。

于 2013-04-02T06:35:58.907 回答