Shifting of Focus in Applet
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); } }