在写韩顺平坦克大战的时候出现了问题,坦克无法移动,下面是代码!
importjava.awt.Color;importjava.awt.Graphics;importjava.awt.event.KeyEvent;importjava...
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import com.sun.java_cup.internal.internal_error;
public class MyTankWar extends JFrame {
MyTankWar() {
add(new Mypanel());
addKeyListener(new Mypanel());
setSize(400, 300);
setLocation(300, 150);
setVisible(true);
}
public static void main(String[] args) {
MyTankWar mtw = new MyTankWar();
}
}
class Mypanel extends JPanel implements KeyListener {
Tank tank = new Tank(100,100);
public void paint(Graphics g) {
g.fillRect(0, 0, 400, 300);
tank.draw(tank.getX(), tank.getY(), g, tank.getDirect(), 0);
}
public void keyPressed(KeyEvent e) {
tank.move(e);
this.repaint();
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
}
class Tank {
int x,y;
int direct=0;
public Tank(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public void draw(int x, int y, Graphics g, int direct, int type) {
switch (type) {
case 0:
g.setColor(Color.CYAN);
break;
case 1:
g.setColor(Color.YELLOW);
break;
default:
break;
}
switch (direct) {
case 0:
g.fill3DRect(x, y, 5, 30, false);
g.fill3DRect(x + 15, y, 5, 30, false);
g.fill3DRect(x + 5, y + 5, 10, 20, false);
g.fillOval(x + 5, y + 10, 10, 10);
g.drawLine(x + 10, y + 15, x + 10, y);
break;
default:
break;
}
}
void move(KeyEvent e) {
int key = e.getKeyCode();
switch (key) {
case KeyEvent.VK_DOWN:
y=y+5;
break;
case KeyEvent.VK_LEFT:
x--;
break;
case KeyEvent.VK_RIGHT:
x++;
break;
case KeyEvent.VK_UP:
y--;
break;
default:
break;
}
}
public int getDirect() {
return direct;
}
public void setDirect(int direct) {
this.direct = direct;
}
}
可能有点长 这个问题搞了我几天了,头好晕,请帮帮忙,我dbug调试是我修改了x和y的值在repainl的时候没有改边painl的值。难道真的不能改变吗,按照Java的语法,是可以改变的啊,我没有按照韩顺平老师的写法写,按照自己的思维来写的!跪求高手解答
我发现问题在哪了 repaint在业没有执行过paint了 展开
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import com.sun.java_cup.internal.internal_error;
public class MyTankWar extends JFrame {
MyTankWar() {
add(new Mypanel());
addKeyListener(new Mypanel());
setSize(400, 300);
setLocation(300, 150);
setVisible(true);
}
public static void main(String[] args) {
MyTankWar mtw = new MyTankWar();
}
}
class Mypanel extends JPanel implements KeyListener {
Tank tank = new Tank(100,100);
public void paint(Graphics g) {
g.fillRect(0, 0, 400, 300);
tank.draw(tank.getX(), tank.getY(), g, tank.getDirect(), 0);
}
public void keyPressed(KeyEvent e) {
tank.move(e);
this.repaint();
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
}
class Tank {
int x,y;
int direct=0;
public Tank(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public void draw(int x, int y, Graphics g, int direct, int type) {
switch (type) {
case 0:
g.setColor(Color.CYAN);
break;
case 1:
g.setColor(Color.YELLOW);
break;
default:
break;
}
switch (direct) {
case 0:
g.fill3DRect(x, y, 5, 30, false);
g.fill3DRect(x + 15, y, 5, 30, false);
g.fill3DRect(x + 5, y + 5, 10, 20, false);
g.fillOval(x + 5, y + 10, 10, 10);
g.drawLine(x + 10, y + 15, x + 10, y);
break;
default:
break;
}
}
void move(KeyEvent e) {
int key = e.getKeyCode();
switch (key) {
case KeyEvent.VK_DOWN:
y=y+5;
break;
case KeyEvent.VK_LEFT:
x--;
break;
case KeyEvent.VK_RIGHT:
x++;
break;
case KeyEvent.VK_UP:
y--;
break;
default:
break;
}
}
public int getDirect() {
return direct;
}
public void setDirect(int direct) {
this.direct = direct;
}
}
可能有点长 这个问题搞了我几天了,头好晕,请帮帮忙,我dbug调试是我修改了x和y的值在repainl的时候没有改边painl的值。难道真的不能改变吗,按照Java的语法,是可以改变的啊,我没有按照韩顺平老师的写法写,按照自己的思维来写的!跪求高手解答
我发现问题在哪了 repaint在业没有执行过paint了 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询