下面的程序哪里出错?为什么我移动W,A,S,D键的时候坦克的方向不改变呢?

liang545621 2011-12-05 09:21:30
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class Demo extends JFrame {

Mypanel mp = null;

public static void main(String[] args) {
Demo mtg = new Demo();

}
public Demo() {
mp = new Mypanel();

Thread t=new Thread(mp);
t.start();
this.add(mp);

this.addKeyListener(mp);
this.setSize(800,700);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
class Mypanel extends JPanel implements KeyListener ,Runnable{

int x=100;
int y=100;

int direct=0;
int speed=1;
int color;
public void paint(Graphics g){
super.paint(g);

g.setColor(Color.yellow);
g.fill3DRect(x+11,y+12,50,5,false);
g.fill3DRect(x+11,y+44,50,5,false);
g.setColor(Color.red);
g.fillRect(x+19,y+15,30,30);
g.setColor(Color.BLUE);
g.fillOval(x+23, y+18, 23,23);
g.setColor(Color.black);
g.drawLine(x+34,y+30,x+71,y+30);

}
public void move(){
Graphics g = null;
switch(direct){
case 0:
g.setColor(Color.yellow);
g.fill3DRect(x+17,y+5,5,50,false);
g.fill3DRect(x+48,y+5,5,50,false);
g.setColor(Color.red);
g.fillRect(x+19,y+15,30,30);
g.setColor(Color.BLUE);
g.fillOval(x+23, y+18, 23,23);
g.setColor(Color.green);
g.drawLine(x+34,y+34,x+34,y-3);
break;
case 1:
g.setColor(Color.yellow);
g.fill3DRect(x+11,y+12,50,5,false);
g.fill3DRect(x+11,y+44,50,5,false);
g.setColor(Color.red);
g.fillRect(x+19,y+15,30,30);
g.setColor(Color.BLUE);
g.fillOval(x+23, y+18, 23,23);
g.setColor(Color.green);
g.drawLine(x+34,y+30,x-4,y+30);
break;

case 2:
g.setColor(Color.yellow);
g.fill3DRect(x+17,y+5,5,50,false);
g.fill3DRect(x+48,y+5,5,50,false);
g.setColor(Color.red);
g.fillRect(x+19,y+15,30,30);
g.setColor(Color.BLUE);
g.fillOval(x+23, y+18, 23,23);
g.setColor(Color.green);
g.drawLine(x+34,y+34,x+34,y+71);
break;
case 3:
g.setColor(Color.yellow);
g.fill3DRect(x+11,y+12,50,5,false);
g.fill3DRect(x+11,y+44,50,5,false);
g.setColor(Color.red);
g.fillRect(x+19,y+15,30,30);
g.setColor(Color.BLUE);
g.fillOval(x+23, y+18, 23,23);
g.setColor(Color.green);
g.drawLine(x+34,y+30,x+71,y+30);
break;
}

}


public void keyPressed(KeyEvent e) {


if (e.getKeyCode() == KeyEvent.VK_W)
{
this.setdirect(0);
y-=4;

} else if (e.getKeyCode() == KeyEvent.VK_D)
{
this.setdirect(1);
x+=4;


} else if (e.getKeyCode() == KeyEvent.VK_S)
{
this.setdirect(2);
y+=4;


} else if (e.getKeyCode() == KeyEvent.VK_A)
{
this.setdirect(3);
x-=4;

}
this.repaint();
}


private void setdirect(int i) {

}
public void keyReleased(KeyEvent arg0) {
}

public void keyTyped(KeyEvent arg0) {
}

public void run() {

}

}
...全文
63 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
富兰克陈 2011-12-05
  • 打赏
  • 举报
回复
帮你改了一下,调试过可以了,你对比一下有什么不同吧:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class Demo extends JFrame {

Mypanel mp = null;

public static void main(String[] args) {
Demo mtg = new Demo();

}
public Demo() {
mp = new Mypanel();

Thread t=new Thread(mp);
t.start();
this.add(mp);

this.addKeyListener(mp);
this.setSize(800,700);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
class Mypanel extends JPanel implements KeyListener ,Runnable{

int x=100;
int y=100;

int direct=0;
int speed=1;
int color;
public void paint(Graphics g){
super.paint(g);
/*
g.setColor(Color.yellow);
g.fill3DRect(x+11,y+12,50,5,false);
g.fill3DRect(x+11,y+44,50,5,false);
g.setColor(Color.red);
g.fillRect(x+19,y+15,30,30);
g.setColor(Color.BLUE);
g.fillOval(x+23, y+18, 23,23);
g.setColor(Color.black);
g.drawLine(x+34,y+30,x+71,y+30);
*/
move(g);
}
public void move(Graphics g){
//Graphics g = null;
switch(direct){
case 0:
g.setColor(Color.yellow);
g.fill3DRect(x+17,y+5,5,50,false);
g.fill3DRect(x+48,y+5,5,50,false);
g.setColor(Color.red);
g.fillRect(x+19,y+15,30,30);
g.setColor(Color.BLUE);
g.fillOval(x+23, y+18, 23,23);
g.setColor(Color.green);
g.drawLine(x+34,y+34,x+34,y-3);
break;
case 1:
g.setColor(Color.yellow);
g.fill3DRect(x+11,y+12,50,5,false);
g.fill3DRect(x+11,y+44,50,5,false);
g.setColor(Color.red);
g.fillRect(x+19,y+15,30,30);
g.setColor(Color.BLUE);
g.fillOval(x+23, y+18, 23,23);
g.setColor(Color.green);
//g.drawLine(x+34,y+30,x-4,y+30);
g.drawLine(x+34,y+30,x+71,y+30);
break;

case 2:
g.setColor(Color.yellow);
g.fill3DRect(x+17,y+5,5,50,false);
g.fill3DRect(x+48,y+5,5,50,false);
g.setColor(Color.red);
g.fillRect(x+19,y+15,30,30);
g.setColor(Color.BLUE);
g.fillOval(x+23, y+18, 23,23);
g.setColor(Color.green);
g.drawLine(x+34,y+34,x+34,y+71);
break;
case 3:
g.setColor(Color.yellow);
g.fill3DRect(x+11,y+12,50,5,false);
g.fill3DRect(x+11,y+44,50,5,false);
g.setColor(Color.red);
g.fillRect(x+19,y+15,30,30);
g.setColor(Color.BLUE);
g.fillOval(x+23, y+18, 23,23);
g.setColor(Color.green);
//g.drawLine(x+34,y+30,x+71,y+30);
g.drawLine(x+34,y+30,x-4,y+30);
break;
}

}


public void keyPressed(KeyEvent e) {


if (e.getKeyCode() == KeyEvent.VK_W)
{
this.setdirect(0);
y-=4;

} else if (e.getKeyCode() == KeyEvent.VK_D)
{
this.setdirect(1);
x+=4;


} else if (e.getKeyCode() == KeyEvent.VK_S)
{
this.setdirect(2);
y+=4;


} else if (e.getKeyCode() == KeyEvent.VK_A)
{
this.setdirect(3);
x-=4;

}
this.repaint();
}


private void setdirect(int i) {
this.direct=i;
}
public void keyReleased(KeyEvent arg0) {
}

public void keyTyped(KeyEvent arg0) {
}

public void run() {

}

}

23,404

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧