我在tank类里面有两个构造方法!
在tankclient类里面使用!我想new出两类tank我定义了一些变量来区分它们!
new出来之后通过draw 方法把它们显示在窗口里
但是new出来的却在一个位置
代码如下:import java.awt.*;
import java.util.List;
import java.awt.event.*;
import java.util.*;
import java.util.ArrayList;
public class TankClient {

Tank mytank = new Tank(400, 560, true, this);
Tank enemyTank = new Tank(100, 100, false, this);

public static void main(String[] args) {
TankClient tc = new TankClient();
tc.star();
}

public void star() {
WindowFrame wf =  new WindowFrame();
}

public class WindowFrame extends Frame {

Image backPaint = null; public static final int GAME_WIDTH = 800;
public static final int GAME_HEIGHT = 600;

WindowFrame() {
this.setLocation(200, 100);
this.setSize(GAME_WIDTH, GAME_HEIGHT);
this.setTitle("TankWar");
this.setBackground(new Color(70, 255, 100));
this.setResizable(false);

this.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

//this.addKeyListener(new KeyMonitor());
this.setVisible(true); }

public void paint(Graphics g) {
mytank.draw(g);
enemyTank.draw(g);

Color c = g.getColor();
g.setColor(new Color(100, 0, 100));
Font f = g.getFont();
g.setFont(new Font("Arial", 10, 15));
//g.drawString("missile count :" + " " + missile.size(), 10, 45);
g.drawString("EnemyTank :", 10, 65);
g.setColor(c);
g.setFont(f);

} public void update(Graphics g) {
if(backPaint==null) {
backPaint = this.createImage(GAME_WIDTH, GAME_HEIGHT);
}
Graphics gback = backPaint.getGraphics();
Color c = gback.getColor();
gback.setColor(new Color(70, 255, 100));
gback.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
gback.setColor(c);
paint(gback);
g.drawImage(backPaint, 0, 0, null);
}
 }
}import java.awt.*;
import java.awt.event.*;public class Tank {
private static int x ;
private static int y;

public static final int XSPEED = 15;
public static final int YSPEED = 15;

private boolean bL = false, bU = false, bD = false, bR = false; 
enum direction {L, LU, U, RU, R, RD, D, LD, STOP };
private direction dir = direction.STOP;
private direction ptdir = direction.D;

TankClient tc;
private boolean good;

public static final int WIDTH = 35;
public static final int HEIGHT = 35;


public Tank(int x, int y, boolean good) {
this.x = x;
this.y = y;
this.good = good;
}
    
public Tank(int x, int y, boolean good, TankClient tc) {
this(x, y, good);
this.tc = tc;
}

public void draw(Graphics g) {
Color c = g.getColor();
if(good) {
g.setColor(Color.RED);
} else {
g.setColor(new Color(100,200,100));
}
g.fillOval(x, y, WIDTH, HEIGHT);
if(good) {
g.setColor(Color.WHITE);
} else {
g.setColor(new Color(100, 0, 100));
}
g.setColor(c);
}

}

解决方案 »

  1.   

    import java.awt.*; 
    import java.awt.event.*; public class Tank { 
    private static int x ; ///<<<<----------------这里,你为什么要用static?static就是类变量,去掉static就可以了,就变成每个对象自己的了
    private static int y; ///<<<<----------------同上public static final int XSPEED = 15; 
    public static final int YSPEED = 15; private boolean bL = false, bU = false, bD = false, bR = false;  
    enum direction {L, LU, U, RU, R, RD, D, LD, STOP }; 
    private direction dir = direction.STOP; 
    private direction ptdir = direction.D; TankClient tc; 
    private boolean good; public static final int WIDTH = 35; 
    public static final int HEIGHT = 35;
      

  2.   

    Tank类中的x,y变量是静态的,所以mytank和enemytank对象的x,y是相同的,在一个位置是正常的。