java 中移动图片,并且在按键盘的“回车”时,切换到另外一张图片!!请在我下面的代码改进!!!谢谢!!

importjava.awt.*;importjava.awt.event.*;importjava.awt.event.KeyEvent;importjava.awt.... import java.awt.*;
import java.awt.event.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;

public class zhandou extends JFrame implements KeyListener
{

Image roleImage,Image1;
int x,y;

public zhandou()
{
super("MOVE");
Container c=getContentPane();
setSize(320,240);
setVisible(true);
loadImage();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//setFocusable(true);
addKeyListener(this);
}
public void paint(Graphics g)
{
super.paint(g);
//drawRole(g);
g.drawImage(roleImage,x,y,this);
g.drawImage(Image1,x,y,this);
}
public void loadImage()
{
ImageIcon icon=new ImageIcon("tupian/草地.jpg");
roleImage=icon.getImage();
ImageIcon ic=new ImageIcon("tupian/right.gif");
Image1=ic.getImage();
}
class Thread1 extends Thread
{

}
/*private void drawRole(Graphics g)
{
g.drawImage(roleImage,100,100,this);
}*/
public void keyPressed(KeyEvent e)
{

if(e.getKeyCode()==KeyEvent.VK_UP)
y=y-5;
else if(e.getKeyCode()==KeyEvent.VK_DOWN)
y=y+5;
else if(e.getKeyCode()==KeyEvent.VK_RIGHT)
x=x+5;
else if(e.getKeyCode()==KeyEvent.VK_LEFT)
x=x-5;
else if(e.getKeyCode()==KeyEvent.VK_ENTER)
x=x+10;
repaint();

}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
}
public static void main(String args[])
{
new zhandou();
}
}
展开
 我来答
lylegend13
2011-12-10 · TA获得超过4696个赞
知道大有可为答主
回答量:1701
采纳率:0%
帮助的人:1725万
展开全部
你好,按照你的要求代码如下,修改了三处
简单说明一下,就是加了一个标识boolean,用true/false来表示显示第一张/第二张图片

import java.awt.*;
import java.awt.event.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;

public class zhandou extends JFrame implements KeyListener {

Image roleImage, Image1;
int x, y;

public zhandou() {
super("MOVE");
Container c = getContentPane();
setSize(320, 240);
setVisible(true);
loadImage();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// setFocusable(true);
addKeyListener(this);
}

boolean key = true;// 加这样一个标识,true是第一张图片,false为第二张图片

public void paint(Graphics g) {
super.paint(g);
// drawRole(g);
if (key) {// 根据标识判断需要显示的图片
g.drawImage(roleImage, x, y, this);
} else {
g.drawImage(Image1, x, y, this);
}
}

public void loadImage() {
ImageIcon icon = new ImageIcon("tupian/草地.jpg");
roleImage = icon.getImage();
ImageIcon ic = new ImageIcon("tupian/right.gif");
Image1 = ic.getImage();
}

class Thread1 extends Thread {

}

/*
* private void drawRole(Graphics g) { g.drawImage(roleImage,100,100,this);
* }
*/
public void keyPressed(KeyEvent e) {

if (e.getKeyCode() == KeyEvent.VK_UP)
y = y - 5;
else if (e.getKeyCode() == KeyEvent.VK_DOWN)
y = y + 5;
else if (e.getKeyCode() == KeyEvent.VK_RIGHT)
x = x + 5;
else if (e.getKeyCode() == KeyEvent.VK_LEFT)
x = x - 5;
else if (e.getKeyCode() == KeyEvent.VK_ENTER)
key = !key;// 切换标识状态
repaint();

}

public void keyReleased(KeyEvent e) {
}

public void keyTyped(KeyEvent e) {
}

public static void main(String args[]) {
new zhandou();
}
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式