in the following code you can move an object using keyBoard arrores
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class GUITest extends JFrame implements KeyListener {
//private Container container;
private Icon icon[] = { new ImageIcon("L1.gif"),//Left <--- set an image here
new ImageIcon("R1.gif"),//Right <--- set an image here
new ImageIcon("U1.gif"),//Up <--- set an image here
new ImageIcon("D1.gif")};//Down <--- set an image here
private Point ObjectLocation,LabelLocation,FrameSize;
private JLabel label;
int counter;
int KeyCode;
int IconNumber;
//constructor-----------------------------------------------------------------
public GUITest()
{
super("move the object");
// the fram size
FrameSize = new Point();
FrameSize.x = 300;
FrameSize.y = 200;
// the object location
LabelLocation = new Point();
ObjectLocation = new Point();
// set the default icon
label = new JLabel(icon[0]);
getContentPane().add(label);
IconNumber=0;
// add key lisitener
addKeyListener(this);
// the fram setting
setSize(FrameSize.x ,FrameSize.y);
setVisible(true);
getContentPane().setBackground(Color.white);
}// end constructor
//
// start the key listening
// keyPressed-----------------------------------------------------------------
public void keyPressed(KeyEvent event)
{
KeyCode = event.getKeyCode();
moveObject(KeyCode);
}
// keyReleased----------------------------------------------------------------
public void keyReleased(KeyEvent event)
{
}
// keyTyped-------------------------------------------------------------------
public void keyTyped(KeyEvent event)
{
}
// the method that will move the object---------------------------------------
public void moveObject(int KeyCode)
{
//the step size
int StepSize = 10;
int XStepSize=0, YStepSize=0;
LabelToFrame(ObjectLocation);
LabelLocation.setLocation(ObjectLocation);
// the index number of the icon
switch(KeyCode)
{
//left key
case 37:
XStepSize = -1;
YStepSize = 0;
IconNumber = 0;
break;
//right key
case 39:
XStepSize = 1;
YStepSize = 0;
IconNumber = 1;
break;
//Up key
case 38:
XStepSize = 0;
YStepSize = -1;
IconNumber = 2;
break;
//Down key
case 40:
XStepSize = 0;
YStepSize = 1;
IconNumber = 3;
break;
//defult
default:
break;
}//end switch
//withen the frame is resize
FrameSize.x = getContentPane().getWidth();
FrameSize.y = getContentPane().getHeight();
// inside the X-coordenat limit
LabelLocation.x += StepSize * XStepSize;
if( (LabelLocation.x >= 10 && LabelLocation.x <= FrameSize.x-10))
{ObjectLocation.x = LabelLocation.x;}
else if(LabelLocation.x >= FrameSize.x-10){LabelLocation.x = FrameSize.x-11;}
else {LabelLocation.x = 11;}
ObjectLocation.x = LabelLocation.x;
// inside the Y-coordenat limit
LabelLocation.y += StepSize * YStepSize;
if( (LabelLocation.y >= 10 && LabelLocation.y <= FrameSize.y-10))
{ObjectLocation.y = LabelLocation.y;}
else if(LabelLocation.y >= FrameSize.y-10){LabelLocation.y = FrameSize.y-11;}
else {LabelLocation.y = 11;}
ObjectLocation.y = LabelLocation.y;
frameToLabel(ObjectLocation);
label.setIcon(icon[IconNumber]);
label.setLocation(ObjectLocation);
}// end method moveObject
//chnage from Fram cordinate to Label cordinate
private void frameToLabel(Point ObjectLocation)
{
ObjectLocation.x -= FrameSize.x/2;
ObjectLocation.y -= FrameSize.y/2;
}
//chnage from Label cordinate to Fram cordinate
private void LabelToFrame(Point ObjectLocation)
{
ObjectLocation.x += FrameSize.x/2;
ObjectLocation.y += FrameSize.y/2;
}
public static void main(String[] args) {
System.out.println("\n\n\tStart the main Method:\n");
// the call for the classes start from hear ---VV--V--Vv ----vV-------------
//GUITest test = new GUITest();
GUITest test = new GUITest();
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// the call for the classes end hear ------------^^-^^^---^-^^^^^-----------
}//end the main method
}// end classand if you face some diffeculty in the code , i will be hape to answer...