Search This Blog

Sunday, 21 August 2022

B5. NEP Java Program to move any one shape according to the arrow key pressed.

/* B5. NEP Java Program to move any one  shape according to the arrow key pressed. */

 

import java.awt.*;

import java.applet.*;

import java.awt.event.*;

/*<applet code="ProgB55" width=500 height=500> </applet>*/

public class ProgB55 extends Applet implements KeyListener

{

        int X=150,Y=160, a=80,b=80;

        public void init()

        {

                addKeyListener(this);

        }

        public void keyPressed(KeyEvent ke)

        {

               

                int key=ke.getKeyCode();

                switch(key)

                {

        case KeyEvent.VK_RIGHT: X=200;

                            Y=160;

                            a=80;

                            b=80;

                            showStatus("Right Key");

                                break;

 

         case KeyEvent.VK_LEFT: X=100;

                            Y=160;

                            a=80;

                            b=80;

                            showStatus("Left Key");

                                break;

                       

        case KeyEvent.VK_UP:   X=150;

                            Y=110;

                            a=80;

                            b=80;

                            showStatus("Up Key");

                            break;

 

        case KeyEvent.VK_DOWN:  X=150;

                            Y=210;

                            a=80;

                            b=80;

                            showStatus("Down Key");

                            break;

               }

               repaint();

        }

        public void keyReleased(KeyEvent ke)

        {

                //showStatus("Key UP");

        }

        public void keyTyped(KeyEvent ke)

        {

                repaint();

        }

        public void paint(Graphics g)

        {

             g.setColor(Color.red);

             g.drawString("USE ARROW KEYS TO MOVE SHAPE",10,20);

             g.setColor(Color.blue);

             g.fillRect(X,Y,a,b);

 

        }

}

No comments :

Post a Comment

FREE Hit Counters