BFOIT - Introduction to Computer Programming

Java User Interface Events

Introduction

Mouse and Key Events


    class DrawLines extends TurtleGraphicsWindow    
    {

        public void keypressed(int keyNum)
        {
            switch ( keyNum )
            {
                case TGKeyHandler.DOWN:
                    pendown();
                    break;
                case TGKeyHandler.UP:
                    penup();
                    break;
                case '0':
                    setpencolor( Turtle.BLACK );
                    break;
                case '1':
                    setpencolor( Turtle.BLUE );
                    break;
                case '2':
                    setpencolor( Turtle.GREEN );
                    break;
                case '4':
                    setpencolor( Turtle.RED );
                    break;
            }

        } // end keypressed()


        public void mouseClicked()
        {
            setxy( mousex(), mousey() );
    
        } // end mouseClicked()
    

        public static void main(String[] args)
        {
            DrawLines me = new DrawLines();
        }


    } // end class DrawLines
    
Example 24.1

Summary


Back to Control Flow Go to the Table of Contents