Objectives:
The following code can be used for key presses of the up, down, left, and right arrows.
private class KeyResponder extends
KeyAdapter
{
public void
keyPressed(KeyEvent e){
switch(e.getKeyCode()){
case
KeyEvent.VK_LEFT:
trans[0] = trans[0] - .5f;
break;
case
KeyEvent.VK_UP:
trans[1] = trans[1] + .5f;
break;
case
KeyEvent.VK_RIGHT:
trans[0] = trans[0] + .5f;
break;
case
KeyEvent.VK_DOWN:
trans[1] = trans[1] - .5f;
break;
}
}
}
Add the appropriate code so that when the arrow keys are
pressed, our rotating box will be translated either up, down, right,
or left. |
Just to see what will happen. Try placing the translation code
AFTER the code to do the rotation.
This actually causes the translation to be performed BEFORE the rotation.
Why doesn't this work?
Fix it so that it works now.
Now add code so that the cube is scaled in all three dimensions
by either .5 if you hit the 's' key or by 2.0 if you hit the 'S'
key. You want to scale about the origin so be sure that the
scaling works with the translation. Does this mean that the
scaling needs to be done before or after the translation? You
figure it out. Hint: In keyPressed you can add another switch that tests e.getKeyChar() so that you don't need the key codes for 's' and 'S'. |