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();
}
} 展开
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();
}
} 展开
1个回答
展开全部
你好,按照你的要求代码如下,修改了三处
简单说明一下,就是加了一个标识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();
}
}
简单说明一下,就是加了一个标识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();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询