Shifting of Focus in Applet

Hello!!

Below is the code of an applet where i have mapped the directional keys to increase and decrease the radius of an oval.

The applet is running well but whenever i am adding any component like textbox,button or label the key events under which the radius is changed are not working and hence oval radius is not changing.Any Solution ?



import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import java.applet.*;
/*


*/
public class a extends Applet
implements KeyListener
{
   
    int X=20,Y=30,xr=100,xy=100,r=70,r1=r,ix=200,iy=200,i;
    Image image;
    AudioClip audio;
    String msg="key pressed";
    TextField t;
    Button b;
    public void init()
    {
       
        //requestFocusInWindow();
        //setLayout(null);
        addKeyListener(this);
       
        //requestFocus();
        setBackground(Color.green);
        setForeground(Color.blue);
        image = getImage(getDocumentBase(), "image22.jpg");
        audio = getAudioClip(getDocumentBase(), "chicken.au");
        /*t=new TextField();
        add(t);
        t.setBounds(500, 500, 100, 100);
        b= new Button();
        add(b);*/
     
     
    }
   
   
   
   
    public void keyPressed(KeyEvent k)
    {
         
        showStatus("KeyDown");
        int key=k.getKeyCode();
        switch(key)
        {
            case KeyEvent.VK_UP:
                //showStatus("Move to Up");
               
               
                audio.play();
                msg = msg + "";
                r=r+6;
                iy++;
                break;
            case KeyEvent.VK_DOWN:
                //showStatus("Move to Down");
               
                audio.play();
                msg += "";
                r=r-6;
                iy--;
                break;
            case KeyEvent.VK_RIGHT:
                //showStatus("Move to Down");
                //msg += "";
               
                ix++;
                break;
            case KeyEvent.VK_LEFT:
               
                //showStatus("Move to Down");
                //msg += "";
                ix--;
                break;
           
        }
        repaint();
    }
   
   
    public void keyReleased(KeyEvent k)
    {
        requestFocusInWindow();     
        //audio.stop();
        showStatus("Key Up");
    }
    public void keyTyped(KeyEvent k)
    {
        requestFocusInWindow();     
        //t.setFocusable(false);
    }
    public void paint(Graphics g)
    {
        //g.drawString(msg,X,Y);
        // g.drawOval(xr-(r/2), xr-(r/2), r, r);
       
        g.drawImage(image, ix, iy, this);
        g.fillOval(xr-(r/2), xr-(r/2), r, r);
        g.drawOval(xr-(r1/2+5), xr-(r1/2+5), r1+10, r1+10);
       
       
                 
    }
 
}

Replies

  • Rohanz21
    Rohanz21
    Here's the code after adding components and here the keyPressed method is not working.

    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.Ellipse2D;
    import java.applet.*;
    /*
    
    
    */
    public class a extends Applet
    implements KeyListener
    {
       
        int X=20,Y=30,xr=100,xy=100,r=70,r1=r,ix=200,iy=200,i;
        Image image;
        AudioClip audio;
        String msg="key pressed";
        TextField t;
        Button b;
        public void init()
        {
           
            //requestFocusInWindow();
            //setLayout(null);
            addKeyListener(this);
           
            //requestFocus();
            setBackground(Color.green);
            setForeground(Color.blue);
            image = getImage(getDocumentBase(), "image22.jpg");
            audio = getAudioClip(getDocumentBase(), "chicken.au");
            t=new TextField();
            add(t);
            t.setBounds(500, 500, 100, 100);
            b= new Button();
            add(b);
         
         
        }
       
       
       
       
        public void keyPressed(KeyEvent k)
        {
             
            showStatus("KeyDown");
            int key=k.getKeyCode();
            switch(key)
            {
                case KeyEvent.VK_UP:
                    //showStatus("Move to Up");
                   
                   
                    audio.play();
                    msg = msg + "";
                    r=r+6;
                    iy++;
                    break;
                case KeyEvent.VK_DOWN:
                    //showStatus("Move to Down");
                   
                    audio.play();
                    msg += "";
                    r=r-6;
                    iy--;
                    break;
                case KeyEvent.VK_RIGHT:
                    //showStatus("Move to Down");
                    //msg += "";
                   
                    ix++;
                    break;
                case KeyEvent.VK_LEFT:
                   
                    //showStatus("Move to Down");
                    //msg += "";
                    ix--;
                    break;
               
            }
            repaint();
        }
       
       
        public void keyReleased(KeyEvent k)
        {
            requestFocusInWindow();     
            //audio.stop();
            showStatus("Key Up");
        }
        public void keyTyped(KeyEvent k)
        {
            requestFocusInWindow();     
            //t.setFocusable(false);
        }
        public void paint(Graphics g)
        {
            //g.drawString(msg,X,Y);
            // g.drawOval(xr-(r/2), xr-(r/2), r, r);
           
            g.drawImage(image, ix, iy, this);
            g.fillOval(xr-(r/2), xr-(r/2), r, r);
            g.drawOval(xr-(r1/2+5), xr-(r1/2+5), r1+10, r1+10);
           
           
                     
        }
     
    }
    
  • xero
    xero
    its just a matter of focus; when you don't have any components added in the applet the focus is applet itself, thus, the applet responds to the keylisteners as you have implemented. Now, once you add widgets like textbox. button etc. the focus shifts to either of these components, the focus either goes to textbox or button, but not the applet, thus the failing of keylisteners.

    If you want the keylisteners to work at all times then you have register the components to keylistener.
    t.addKeyListener(this);
    b.addKeyListener(this);

    If you want the keylisteners to work only while focusing on applet
    this.setFocusable(true),
    This will also enable the applet to include itself during tabbed focusing.

    Let me know in case you have any questions.

    Regards.
  • Rohanz21
    Rohanz21
    Thanks a lot xero for the help.

    Yes the trouble was that i couldn't find a way to shift the focus on the applet like in case of components whom i could focus by using the component name with dot operator using the requestFocus() or requestFocusInWindow() methods.

    Your second method solved the problem.

    Thanks again!!
  • xero
    xero
    welcome 😀

You are reading an archived discussion.

Related Posts

I'm studying automobiles and have a very basic question - What is the function of 'gear' in a vehicle?
Hello Friends I am working on a mobile application that will be compatible on all the major mobile platforms Such as Iphone, Black berry , Android I am using phone...
hey guys can you give me the names of some cheap and easily available ARM7 processors. my project does not require ARM but iam forced to use it. so just...
i have to install windows 7 with usb for my 32 bit system...so for this i have to make iso image file....i have power iso 047 ....so how can make...
ramp up function is preferred to start pumps over quick start. i have read that this is to avoid the danger of water hammer. exactly how does this cause water...