Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 20th, 2012, 03:05 AM
Rohanz21 Rohanz21 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 10 Rohanz21 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 33 m 55 sec
Reputation 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);
		 
		 
	}

}

Reply With Quote
  #2  
Old September 20th, 2012, 07:04 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
Please edit your post and wrap the code in code tags.

Reply With Quote
  #3  
Old September 20th, 2012, 12:17 PM
Rohanz21 Rohanz21 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 10 Rohanz21 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 33 m 55 sec
Reputation Power: 0
Anyone with solution?

Reply With Quote
  #4  
Old September 20th, 2012, 12:39 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
The posted code does NOT compile without errors. Can you post code that compiles, executes and shows the problem?

Reply With Quote
  #5  
Old September 20th, 2012, 12:49 PM
Rohanz21 Rohanz21 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 10 Rohanz21 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 33 m 55 sec
Reputation 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

Reply With Quote
  #6  
Old September 20th, 2012, 12:51 PM
Rohanz21 Rohanz21 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 10 Rohanz21 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 33 m 55 sec
Reputation 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);
		 
		
		 		 
	}
 
}

Reply With Quote
  #7  
Old September 20th, 2012, 12:55 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
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.

Reply With Quote
  #8  
Old September 20th, 2012, 12:58 PM
Rohanz21 Rohanz21 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 10 Rohanz21 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 33 m 55 sec
Reputation 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.

Reply With Quote
  #9  
Old September 20th, 2012, 01:04 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
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.

Reply With Quote
  #10  
Old September 20th, 2012, 01:06 PM
Rohanz21 Rohanz21 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 10 Rohanz21 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 33 m 55 sec
Reputation Power: 0
Sure.Will elucidate the coding with comments and post it.

Reply With Quote
  #11  
Old September 20th, 2012, 01:11 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
After adding components that will want the focus, request that the focus be where you want it. Also calling the setfocusable method may help

Reply With Quote
  #12  
Old September 20th, 2012, 01:16 PM
Rohanz21 Rohanz21 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 10 Rohanz21 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 33 m 55 sec
Reputation 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.

Reply With Quote
  #13  
Old September 20th, 2012, 01:17 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
I don't know where it is. Add the focus listener back and see if it ever get focus.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Shifting Focus Problem

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap