CrazyEngineers
  • Shifting of Focus in Applet

    Rohanz21

    Member

    Updated: Oct 27, 2024
    Views: 1.7K
    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.*;
    /*
    <applet code=a.class align=right width=800 height=800>
    </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 + "<up arrow>";
                    r=r+6;
                    iy++;
                    break;
                case KeyEvent.VK_DOWN:
                    //showStatus("Move to Down");
                   
                    audio.play();
                    msg += "<DOWN arrow>";
                    r=r-6;
                    iy--;
                    break;
                case KeyEvent.VK_RIGHT:
                    //showStatus("Move to Down");
                    //msg += "<DOWN arrow>";
                   
                    ix++;
                    break;
                case KeyEvent.VK_LEFT:
                   
                    //showStatus("Move to Down");
                    //msg += "<DOWN arrow>";
                    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);
           
           
                     
        }
     
    }
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Rohanz21

    MemberSep 20, 2012

    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.*;
    /*
    <applet code=a.class align=right width=800 height=800>
    </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 + "<up arrow>";
                    r=r+6;
                    iy++;
                    break;
                case KeyEvent.VK_DOWN:
                    //showStatus("Move to Down");
                   
                    audio.play();
                    msg += "<DOWN arrow>";
                    r=r-6;
                    iy--;
                    break;
                case KeyEvent.VK_RIGHT:
                    //showStatus("Move to Down");
                    //msg += "<DOWN arrow>";
                   
                    ix++;
                    break;
                case KeyEvent.VK_LEFT:
                   
                    //showStatus("Move to Down");
                    //msg += "<DOWN arrow>";
                    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);
           
           
                     
        }
     
    }
    
    Are you sure? This action cannot be undone.
    Cancel
  • xero

    MemberSep 20, 2012

    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.
    Are you sure? This action cannot be undone.
    Cancel
  • Rohanz21

    MemberSep 22, 2012

    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!!
    Are you sure? This action cannot be undone.
    Cancel
  • xero

    MemberSep 23, 2012

    welcome 😀
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register