#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    10
    Rep Power
    0

    Shifting Focus Problem


    Hello!!

    Below is the code of an applet where i have mapped the directional keys to increase and decrease the circles and image position and play a sound clip.

    The applet ran well but whenever i am adding any component like textbox,button or label the key events are not working.Th focus remains on the component and even if i try to disable the focus using setFocusable(false) it still doesn't work. How to make it work?

    Code :

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    /*
    <applet code=a.class align=right width=800 height=800>
    </applet>
    */
    public class a extends Applet
    implements KeyListener,FocusListener
    {
    	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);
    		
    		b.setFocusable(false);
    		
    	}
    	
    	
    	
    	public void keyPressed(KeyEvent k)
    	{
    		
    		showStatus("KeyDown");
    		int key=k.getKeyCode();
    		switch(key) 
    		{
    			case KeyEvent.VK_UP:
    				//showStatus("Move to Up");
    				//t.setFocusable(false);
    				
    				audio.play();
    				msg = msg + "<up arrow>";
    				r=r+6;
    				iy++;
    				break;
    			case KeyEvent.VK_DOWN:
    				//showStatus("Move to Down");
    				//t.setFocusable(false);
    				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)
    	{
    		//audio.stop();
    		showStatus("Key Up");
    	}
    	public void keyTyped(KeyEvent k)
    	{
    		//t.setFocusable(false);
    	}
    	public void paint(Graphics g)
    	{
    		//g.drawString(msg,X,Y);
    		// g.drawOval(xr-(r/2), xr-(r/2), r, r);
    		 g.fillOval(xr-(r/2), xr-(r/2), r, r);
    		 g.drawOval(xr-(r1/2+5), xr-(r1/2+5), r1+10, r1+10);
    		 g.drawImage(image, ix, iy, this);
    		 
    		 
    	}
    
    }
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,717
    Rep Power
    348
    Please edit your post and wrap the code in code tags.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    10
    Rep Power
    0
    Anyone with solution?
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,717
    Rep Power
    348
    The posted code does NOT compile without errors. Can you post code that compiles, executes and shows the problem?
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    10
    Rep Power
    0
    Code:
    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);
    		 
    		
    		 		 
    	}
     
    }
    Here's the corrected one:

    What it should do is that when one presses direction keys up,down the size of the circle changes. But when i added components like button or textfield the keytyped event is not triggered hence circle size is not changing
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    10
    Rep Power
    0
    Here's the code without the components and its working fine :

    Code:
    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);
    		 
    		
    		 		 
    	}
     
    }
  12. #7
  13. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,717
    Rep Power
    348
    What's supposed to happen when the code is executed? I see a green background, a blue circle in the upper left and a text field and button in the top center.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    10
    Rep Power
    0
    Run the second program and it will be clear.

    The objective is to increase the radius of the circle on key value VK.UP and decrease it on VK.DOWN and accordingly the size of the circle changes. The 2nd code works fine without any buttons or any other components but as soon as i add any component the keytyped or keypress event is not working hence the circle radius not changing.
  16. #9
  17. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,717
    Rep Power
    348
    If there were comments in the code describing how it worked and what the problem was, this would have been a lot faster. Having to keep coming back here for more input is making this a lot harder than it needed to be.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    10
    Rep Power
    0
    Sure.Will elucidate the coding with comments and post it.
  20. #11
  21. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,717
    Rep Power
    348
    After adding components that will want the focus, request that the focus be where you want it. Also calling the setfocusable method may help
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    10
    Rep Power
    0
    One query : Where is the focus on the 2nd code which is devoid of any components.even if i try to unfocus the components like buttons and textfield the key press event is not triggered.
  24. #13
  25. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,717
    Rep Power
    348
    I don't know where it is. Add the focus listener back and see if it ever get focus.

IMN logo majestic logo threadwatch logo seochat tools logo