Nothing Special   »   [go: up one dir, main page]

JF 2 11

Download as pdf or txt
Download as pdf or txt
You are on page 1of 38

Java Fundamentals

2-11
Keyboard Controls

Copyright © 2022, Oracle and/or its affiliates. Oracle, Java, and MySQL are registered
trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.
Objectives
• This lesson covers the following objectives:
−Create an opening sequence
−Use keyboard controls to manipulate an animation
−Save your Class file
−Using the starter tab
−Add an existing class file to an animation

JF 2-11 Copyright © 2022, Oracle and/or its affiliates. Oracle, Java, and MySQL are registered
Keyboard Controls trademarks of Oracle and/or its affiliates. Other names may be trademarks of their 3
respective owners.
Event Handling
• When an animation is playing, a computer program is
running
• Many computer programs request user interaction
• These interactive programs allow a user to influence
the order of actions that occur in the program
• To program this type of interactivity into an animation,
you create event listeners that look for and respond to
the interactivity (the user input events) of the user
• This is often referred to as event handling

JF 2-11 Copyright © 2022, Oracle and/or its affiliates. Oracle, Java, and MySQL are registered
Keyboard Controls trademarks of Oracle and/or its affiliates. Other names may be trademarks of their 4
respective owners.
What Is an Event?
• An event is any action initiated by the user that is
designed to influence the program's execution during
play
• Events may include:
−Pressing any key on the keyboard
−Clicking a mouse button
−Moving a joystick
−Touching the screen (on touch-enabled devices)

JF 2-11 Copyright © 2022, Oracle and/or its affiliates. Oracle, Java, and MySQL are registered
Keyboard Controls trademarks of Oracle and/or its affiliates. Other names may be trademarks of their 5
respective owners.
What Happens When an Event Occurs?
• Typically, an event triggers (fires, or sets in motion) the
execution of a procedure or function
• For example, when a user presses an up arrow key on
the keyboard (event), it triggers a method that makes
the object in the animation move up (event handling
method)

JF 2-11 Copyright © 2022, Oracle and/or its affiliates. Oracle, Java, and MySQL are registered
Keyboard Controls trademarks of Oracle and/or its affiliates. Other names may be trademarks of their 6
respective owners.
Keyboard Controls
• Inserting keyboard controls into a program allows the
user to control one or more objects while the
animation is running
• The user can press a key on the keyboard, or click the
mouse, to control the next programming action

JF 2-11 Copyright © 2022, Oracle and/or its affiliates. Oracle, Java, and MySQL are registered
Keyboard Controls trademarks of Oracle and/or its affiliates. Other names may be trademarks of their 7
respective owners.
Keyboard Controls
• With keyboard controls, you can:
−Create scenes where the user controls an object that
interacts with other objects
−Create animations that execute conditionally, based on a key
press or mouse click
−Create games where the user is required to control an object
to win the game

JF 2-11 Copyright © 2022, Oracle and/or its affiliates. Oracle, Java, and MySQL are registered
Keyboard Controls trademarks of Oracle and/or its affiliates. Other names may be trademarks of their 8
respective owners.
Keyboard Controls Example
• In Alice 3, you can assign procedures to keys on your
keyboard
• When the animation viewer clicks a certain keyboard
key, the procedure assigned to the keyboard key is
executed
• For example, clicking the right-arrow key on the
keyboard turns the teapot to the right
In programming, keystrokes and mouse clicks are events. Coding events to
handle each procedure is referred to as event handling.

JF 2-11 Copyright © 2022, Oracle and/or its affiliates. Oracle, Java, and MySQL are registered
Keyboard Controls trademarks of Oracle and/or its affiliates. Other names may be trademarks of their 9
respective owners.
Event Listeners
• Event listeners are procedures in the Scene class that
listen for keyboard input while the animation is
running
• Keyboard keys can be programmed to:
−Move an object up or down when certain keys are pressed
−Move an object forward, backward, left, and right using the
arrow keys
−Make an object perform an action, such as speak or
disappear

JF 2-11 Copyright © 2022, Oracle and/or its affiliates. Oracle, Java, and MySQL are registered
Keyboard Controls trademarks of Oracle and/or its affiliates. Other names may be trademarks of their 10
respective owners.
Types of Event Listeners
• There are four types of event listeners available in
Alice 3:
−Scene Activation/Time
−Keyboard
−Mouse
−Position/Orientation

JF 2-11 Copyright © 2022, Oracle and/or its affiliates. Oracle, Java, and MySQL are registered
Keyboard Controls trademarks of Oracle and/or its affiliates. Other names may be trademarks of their 11
respective owners.
Steps to Access Event Listeners
• In the Code editor, click the Scene tab
• Click the button next to initializeEventListeners and
choose Edit
• This opens the initializeEventListeners tab if it is not
already open

JF 2-11 Copyright © 2022, Oracle and/or its affiliates. Oracle, Java, and MySQL are registered
Keyboard Controls trademarks of Oracle and/or its affiliates. Other names may be trademarks of their 12
respective owners.
Event Listeners Tab
• The initializeEventListeners tab is where you can add
an event listener to your code

• The sceneActivated listener is where you can create an


animation that will play before the myFirstMethod
begins
• This can be used as an opening sequence to your code

JF 2-11 Copyright © 2022, Oracle and/or its affiliates. Oracle, Java, and MySQL are registered
Keyboard Controls trademarks of Oracle and/or its affiliates. Other names may be trademarks of their 13
respective owners.
Scene Activation Listener
• You can create your own procedure or use one of the
built in procedures to create an opening sequence
• This procedure named "appear" turns Alice around and
makes her visible in our scene

JF 2-11 Copyright © 2022, Oracle and/or its affiliates. Oracle, Java, and MySQL are registered
Keyboard Controls trademarks of Oracle and/or its affiliates. Other names may be trademarks of their 14
respective owners.
Scene Activation Listener
• To enable the procedure, drag it from the Alice
procedure list and place it before the myFirstMethod
call in the sceneActivated listener

• Test your opening sequence!


JF 2-11 Copyright © 2022, Oracle and/or its affiliates. Oracle, Java, and MySQL are registered
Keyboard Controls trademarks of Oracle and/or its affiliates. Other names may be trademarks of their 15
respective owners.
Keyboard Listeners
• Keyboard listeners:
−Are found in the Add Event Listener drop-down menu
−Listen for, and react to, a keyboard keystroke that you specify

JF 2-11 Copyright © 2022, Oracle and/or its affiliates. Oracle, Java, and MySQL are registered
Keyboard Controls trademarks of Oracle and/or its affiliates. Other names may be trademarks of their 16
respective owners.
Types of Keyboard Listeners

Data Type Description


• This listener lets you program procedures for the
addKeyPressListener()
keyboard key(s) you specify
• This listener lets you program procedures for the
addArrowKeyPressListener()
arrow key(s) you specify
• This listener lets you program procedures for the
addNumberKeyPressListener()
number key(s) you specify
• This listener lets you program the user-defined
addObjectMoverFor(???)
movement for a specified object

JF 2-11 Copyright © 2022, Oracle and/or its affiliates. Oracle, Java, and MySQL are registered
Keyboard Controls trademarks of Oracle and/or its affiliates. Other names may be trademarks of their 17
respective owners.
Program Keyboard Event Listeners
• For example, we will program keyboard event listeners
to command the teapot to move up and down using
the B and C keys, and move left, right, forward and
backward using the arrow keys
• We are in wonderland after all!

JF 2-11 Copyright © 2022, Oracle and/or its affiliates. Oracle, Java, and MySQL are registered
Keyboard Controls trademarks of Oracle and/or its affiliates. Other names may be trademarks of their 18
respective owners.
Steps to Add Keyboard Event Listener
• Select the Add Event Listener drop-down list
• Select Keyboard
• Select addKeyPressListener

JF 2-11 Copyright © 2022, Oracle and/or its affiliates. Oracle, Java, and MySQL are registered
Keyboard Controls trademarks of Oracle and/or its affiliates. Other names may be trademarks of their 19
respective owners.
Steps to Program the IF Control Structure
• Drag the IF control structure into addKeyPressListener
• Select the true condition

JF 2-11 Copyright © 2022, Oracle and/or its affiliates. Oracle, Java, and MySQL are registered
Keyboard Controls trademarks of Oracle and/or its affiliates. Other names may be trademarks of their 20
respective owners.
Steps to Select the Keyboard Key to Activate an
Object's Motion
• Drag the isKey: ??? tile onto the true condition
• A key menu appears
• From the drop-down menu, select the keyboard key
that you want to use to control the motion

JF 2-11 Copyright © 2022, Oracle and/or its affiliates. Oracle, Java, and MySQL are registered
Keyboard Controls trademarks of Oracle and/or its affiliates. Other names may be trademarks of their 21
respective owners.
Steps to Program Motions Activated by Key Press
• From the Instance drop-down menu, select the object
controlled by the keyboard key
• Drag the procedure that should be activated by the
keyboard key into the IF control structure and select
the arguments
• You could drag multiple procedures and control
structures into the IF control structure
• For example, when the B key is pressed, the teapot
moves and then
turns

JF 2-11 Copyright © 2022, Oracle and/or its affiliates. Oracle, Java, and MySQL are registered
Keyboard Controls trademarks of Oracle and/or its affiliates. Other names may be trademarks of their 22
respective owners.
Program Additional Listener Actions
• To program the keyPressListener to listen for more
than one keyboard key, add additional IF control
structures to the listener structure
• There are two ways to do this:
−Add a series of IF control structures one after another and
always leave the ELSE condition empty
−Nest additional IF control structures in the ELSE condition
• Both methods execute in the same manner
• The following steps use the second method, nesting IF
control structures in the ELSE condition, to save display
space
JF 2-11 Copyright © 2022, Oracle and/or its affiliates. Oracle, Java, and MySQL are registered
Keyboard Controls trademarks of Oracle and/or its affiliates. Other names may be trademarks of their 23
respective owners.
Steps to Program Additional Listener Actions
• Drag an IF control structure into the ELSE condition of
an existing IF control structure and select the true
condition
• Drag the isKey ??? variable onto the true argument
• Specify the keyboard key to listen for
• Specify the programming statements to execute

JF 2-11 Copyright © 2022, Oracle and/or its affiliates. Oracle, Java, and MySQL are registered
Keyboard Controls trademarks of Oracle and/or its affiliates. Other names may be trademarks of their 24
respective owners.
Completed Programming Instruction Example
• Below is an example of a teapot programmed to move
up and down using the B and C keyboard keys

JF 2-11 Copyright © 2022, Oracle and/or its affiliates. Oracle, Java, and MySQL are registered
Keyboard Controls trademarks of Oracle and/or its affiliates. Other names may be trademarks of their 25
respective owners.
Steps to Move Objects Using Arrow Keys
• Select the Add Event Listener drop-down menu
• Select Keyboard
• Select addObjectMoverFor
• Select the entity, or object, to control

• Creates the following line of code

JF 2-11 Copyright © 2022, Oracle and/or its affiliates. Oracle, Java, and MySQL are registered
Keyboard Controls trademarks of Oracle and/or its affiliates. Other names may be trademarks of their 26
respective owners.
Steps to Test Event Listeners
• Run the animation
• Click inside the animation window with your cursor
• Use the keyboard keys (specified in
addKeyPressListener) to make the object perform the
procedure (move up and down)
• Use the arrow keys on your keyboard to move the
object forward, backward, right, and left

JF 2-11 Copyright © 2022, Oracle and/or its affiliates. Oracle, Java, and MySQL are registered
Keyboard Controls trademarks of Oracle and/or its affiliates. Other names may be trademarks of their 27
respective owners.
Using an Existing Class in Another Animation
• It can be useful to transfer a class from one animation
to another
• If you wanted to create multiple animations featuring
Alice then you could use your existing Alice class file
with all of its associated procedures
−This would cut down on your work as you have already coded
the actions for Alice
−This would cut down on your testing because you have
already tested that the code works in the original animation
−You can use the class and its procedures as a basis for your
new animation adding additional procedures if required

JF 2-11 Copyright © 2022, Oracle and/or its affiliates. Oracle, Java, and MySQL are registered
Keyboard Controls trademarks of Oracle and/or its affiliates. Other names may be trademarks of their 28
respective owners.
Saving a Class File in Alice 3
• Using the class list button, choose Alice from the list
and then click on the Alice class

JF 2-11 Copyright © 2022, Oracle and/or its affiliates. Oracle, Java, and MySQL are registered
Keyboard Controls trademarks of Oracle and/or its affiliates. Other names may be trademarks of their 29
respective owners.
Saving a Class File in Alice 3
• Click on the Save to Class File button

• Save the file in the MyClasses folder for easy access


when adding it to other animations and name the
class with an appropriate name

JF 2-11 Copyright © 2022, Oracle and/or its affiliates. Oracle, Java, and MySQL are registered
Keyboard Controls trademarks of Oracle and/or its affiliates. Other names may be trademarks of their 30
respective owners.
Using the Starter Tab to Create a World
• Alice 3 has pre-built worlds that can be used to quickly
create a full and interesting animation
• Use the starters tab from the new project interface to
choose your world

JF 2-11 Copyright © 2022, Oracle and/or its affiliates. Oracle, Java, and MySQL are registered
Keyboard Controls trademarks of Oracle and/or its affiliates. Other names may be trademarks of their 31
respective owners.
Adding a Class File in Alice 3
• Create a new project in Alice and go to the scene editor
to access the gallery
• Choose the My Classes tab from the gallery

• This gives you access to the MyClasses folder where


your classes should be saved

JF 2-11 Copyright © 2022, Oracle and/or its affiliates. Oracle, Java, and MySQL are registered
Keyboard Controls trademarks of Oracle and/or its affiliates. Other names may be trademarks of their 32
respective owners.
Adding a Class File in Alice 3
• Add the class as you would any other class from the
gallery

• When you click OK you are presented with a list of the


procedures associated with the class file

JF 2-11 Copyright © 2022, Oracle and/or its affiliates. Oracle, Java, and MySQL are registered
Keyboard Controls trademarks of Oracle and/or its affiliates. Other names may be trademarks of their 33
respective owners.
Adding a Class File in Alice 3
• Placing the cursor over the green cross expands the
code for the procedure

• Clicking finish will add the Alice object into your new
animation

JF 2-11 Copyright © 2022, Oracle and/or its affiliates. Oracle, Java, and MySQL are registered
Keyboard Controls trademarks of Oracle and/or its affiliates. Other names may be trademarks of their 34
respective owners.
Adding a Class File in Alice 3
• You can now use the Alice character and her
procedures in your new animation
• You can add the appear method to the new
sceneActivated listener so that Alice appears in a
consistent way across all of your animations

JF 2-11 Copyright © 2022, Oracle and/or its affiliates. Oracle, Java, and MySQL are registered
Keyboard Controls trademarks of Oracle and/or its affiliates. Other names may be trademarks of their 35
respective owners.
Terminology
• Key terms used in this lesson included:
−Event
−Event handling
−Event listeners
−Keyboard controls
−Keyboard listeners
−Class file

JF 2-11 Copyright © 2022, Oracle and/or its affiliates. Oracle, Java, and MySQL are registered
Keyboard Controls trademarks of Oracle and/or its affiliates. Other names may be trademarks of their 36
respective owners.
Summary
• In this lesson, you should have learned how to:
−Create an opening Sequence
−Use keyboard controls to manipulate an animation
−Save your Class file
−Using the starter tab
−Add an existing class file to an animation

JF 2-11 Copyright © 2022, Oracle and/or its affiliates. Oracle, Java, and MySQL are registered
Keyboard Controls trademarks of Oracle and/or its affiliates. Other names may be trademarks of their 37
respective owners.

You might also like