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();

}

}

}

}

[9166 byte] By [nickgoldgodla] at [2007-11-27 4:12:12]
# 1

public Playscreen(){

addKeyListener(new keycontrol());

You can add a KeyListener or override the processKeyEvent method for the graphic

component. A better way that avoids focus problems is to use key bindings. This is explained

in [url=http://java.sun.com/docs/books/tutorial/uiswing/misc/keybinding.html]How to Use Key Bindings[/url].

import java.awt.*;

import java.awt.event.*;

import java.awt.image.BufferedImage;

import java.io.*;

import javax.imageio.ImageIO;

import javax.swing.*;

public class ImageKeys extends JPanel {

BufferedImage image;

Point loc = new Point(100,100);

int dx = 3;

int dy = 3;

public ImageKeys(BufferedImage image) {

this.image = image;

registerKeys();

setFocusable(true);

}

protected void paintComponent(Graphics g) {

super.paintComponent(g);

g.drawImage(image, loc.x, loc.y, this);

}

private void registerKeys() {

int c = JComponent.WHEN_IN_FOCUSED_WINDOW;

getInputMap(c).put(KeyStroke.getKeyStroke("UP"), "UP");

getActionMap().put("UP", upAction);

getInputMap(c).put(KeyStroke.getKeyStroke("LEFT"), "LEFT");

getActionMap().put("LEFT", leftAction);

getInputMap(c).put(KeyStroke.getKeyStroke("DOWN"), "DOWN");

getActionMap().put("DOWN", downAction);

getInputMap(c).put(KeyStroke.getKeyStroke("RIGHT"), "RIGHT");

getActionMap().put("RIGHT", rightAction);

}

public static void main(String[] args) throws IOException {

String path = "images/Bird.gif";

BufferedImage image = ImageIO.read(new File(path));

JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().add(new ImageKeys(image));

f.setSize(400,400);

f.setLocation(200,200);

f.setVisible(true);

}

private Action upAction = new AbstractAction() {

public void actionPerformed(ActionEvent e) {

loc.y -= dy;

repaint();

}

};

private Action leftAction = new AbstractAction() {

public void actionPerformed(ActionEvent e) {

loc.x -= dx;

repaint();

}

};

private Action downAction = new AbstractAction() {

public void actionPerformed(ActionEvent e) {

loc.y += dy;

repaint();

}

};

private Action rightAction = new AbstractAction() {

public void actionPerformed(ActionEvent e) {

loc.x += dx;

repaint();

}

};

}

crwooda at 2007-7-12 9:18:09 > top of Java-index,Security,Cryptography...
# 2
thanks it work s
nickgoldgodla at 2007-7-12 9:18:09 > top of Java-index,Security,Cryptography...