problem for Key Listener
i want to make image mov e i do this code but there is no error and the image doesn't move.How to make it move?
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.event.*;
publicclass Downstair{
Playscreen graphicComponent;
JPanel centerPanel;
private JPanel getCenterPanel(){
graphicComponent =new Playscreen();
CardLayout cards =new CardLayout();
centerPanel =new JPanel(cards);
centerPanel.add("Menu", getMenuPanel());
centerPanel.add("Game", graphicComponent);
return centerPanel;
}
private JPanel getMenuPanel(){
// Create components.
String[] gameMenulist ={"New Game","Abouts","how to play","Options","Exit"};
JList menulist=new JList(gameMenulist);
ListSelectionModel menulistModel = menulist.getSelectionModel();
//menulist setting
menulist.setLayoutOrientation(JList.HORIZONTAL_WRAP);
menulist.setVisibleRowCount(-1);
menulist.setBackground(Color.BLACK);
menulist.setForeground(Color.WHITE);
menulist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
menulist.addListSelectionListener(new ListListener());
JPanel panel =new JPanel(){
Image bg =new ImageIcon("image/bgimg.jpg").getImage();
protectedvoid paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(bg, 0, 0,this);
}
};
panel.setBackground(Color.white);
panel.setOpaque(true);// default value
panel.setLayout(new GridBagLayout());
GridBagConstraints a =new GridBagConstraints();
// The anchor constraint requires a non-zero weight
// constraint in the direction it is going, ie,
// weightx for horizontal values such as EAST,
// WEST, LINE_START, LINE_END.
a.anchor = GridBagConstraints.PAGE_END;
panel.add(menulist, a);
return panel;
}
publicstaticvoid main(String[] args){
Downstair app =new Downstair();
JFrame mainframe =new JFrame();
mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainframe.getContentPane().add(app.getCenterPanel());
mainframe.setSize(600, 600);
mainframe.setVisible(true);
}
privateclass ListListenerimplements ListSelectionListener{
publicvoid valueChanged(ListSelectionEvent e){
if(!e.getValueIsAdjusting()){
JList list = (JList)e.getSource();
int index = list.getSelectedIndex();
switch(index){
case 0:
CardLayout cards = (CardLayout)centerPanel.getLayout();
cards.show(centerPanel,"Game");
break;
case 4:
System.exit(0);
break;
default:
System.out.println("unexpected type: " + index);
}
}
}
}
}
class Playscreenextends JPanel{
ImageIcon playbg;
ImageIcon playerIcon;
ImageIcon stair;
int floor;
int live;
int randomX;
int randomY;
int randompic;
int playerX;
int playerY;
public Playscreen(){
playerX = 0;
playerY = 0;
playbg =new ImageIcon("image/bgimg.jpg");
playerIcon =new ImageIcon("image/ball.gif");
stair =new ImageIcon("image/ball.jpg");
randomX = (int)( ( Math.random() * 400 ) + 1 );
randomY = (int)( ( Math.random() * 400 ) + 1 );
randompic = (int)( ( Math.random() * 10 ) + 10 );
setBackground(Color.white);
setOpaque(true);
}
protectedvoid paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(playbg.getImage(), 0, 0,this);
g.drawImage(playerIcon.getImage(), playerX, playerY,this);
for(int numpic= 0; numpic < 10 ; numpic++){
g.drawImage(playerIcon.getImage(), randomX, randomY,this);
}
}
publicclass keycontrolimplements KeyListener{
Playscreen playsc =new Playscreen();
/** Handle the key typed event from the text field. */
publicvoid keyTyped(KeyEvent e){
}
/** Handle the key-pressed event from the text field. */
publicvoid keyPressed(KeyEvent e){
input(e);
}
/** Handle the key-released event from the text field. */
publicvoid keyReleased(KeyEvent e){
}
privatevoid input(KeyEvent e){
if(e.getKeyCode() == KeyEvent.VK_KP_LEFT){
playsc.playerX--;
repaint();
}elseif (e.getKeyCode() == KeyEvent.VK_KP_RIGHT){
playsc.playerX++;
repaint();
}
}
}
}

