The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Java Help
|
Shifting Focus Problem
Discuss Shifting Focus Problem in the Java Help forum on Dev Shed. Shifting Focus Problem Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

September 20th, 2012, 03:05 AM
|
|
Registered User
|
|
Join Date: Aug 2012
Posts: 10
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);
}
}
|

September 20th, 2012, 07:04 AM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
|
Please edit your post and wrap the code in code tags.
|

September 20th, 2012, 12:17 PM
|
|
Registered User
|
|
Join Date: Aug 2012
Posts: 10
Time spent in forums: 1 h 33 m 55 sec
Reputation Power: 0
|
|
|
Anyone with solution?
|

September 20th, 2012, 12:39 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
|
The posted code does NOT compile without errors. Can you post code that compiles, executes and shows the problem?
|

September 20th, 2012, 12:49 PM
|
|
Registered User
|
|
Join Date: Aug 2012
Posts: 10
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
|

September 20th, 2012, 12:51 PM
|
|
Registered User
|
|
Join Date: Aug 2012
Posts: 10
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);
}
}
|

September 20th, 2012, 12:55 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
|
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.
|

September 20th, 2012, 12:58 PM
|
|
Registered User
|
|
Join Date: Aug 2012
Posts: 10
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.
|

September 20th, 2012, 01:04 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
|
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.
|

September 20th, 2012, 01:06 PM
|
|
Registered User
|
|
Join Date: Aug 2012
Posts: 10
Time spent in forums: 1 h 33 m 55 sec
Reputation Power: 0
|
|
|
Sure.Will elucidate the coding with comments and post it.
|

September 20th, 2012, 01:11 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
|
After adding components that will want the focus, request that the focus be where you want it. Also calling the setfocusable method may help
|

September 20th, 2012, 01:16 PM
|
|
Registered User
|
|
Join Date: Aug 2012
Posts: 10
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.
|

September 20th, 2012, 01:17 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
|
I don't know where it is. Add the focus listener back and see if it ever get focus.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|