package tf1703;
import java.awt.Color;
import java.awt.Graphics;
//import Tank.Direction;
public class Tank {
public int x,y,w,h;
Enum Direction{L,LU,U,RU,R,RD,D,LD,STOP};
public Direction dir=Direction.U;
public Tank(int x,int y,int w,int h,Direction dir) {
this.x=x;
this.y=y;
this.w=w;
this.h=h;
this.dir=dir;
}
public void draw(Graphics g) {
g.setColor(Color.green);
g.fillOval(x,y,w,h);
switch(this.dir) {
case U:
g.setColor(Color.black);
g.drawLine(x+w/2, y+h/2, x+w/2, y-10);
break;
case LU:
g.setColor(Color.black);
g.drawLine(x+w/2, y+h/2, x, y);
break;
case L:
g.setColor(Color.black);
g.drawLine(x+w/2, y+h/2, x-10, y+h/2);
break;
case LD:
g.setColor(Color.black);
g.drawLine(x+w/2, y+h/2, x, y+h);
break;
case D:
g.setColor(Color.black);
g.drawLine(x+w/2, y+h/2, x+w/2, y+h+10);
break;
case RD:
g.setColor(Color.black);
g.drawLine(x+w/2, y+h/2, x+w, y+h);
break;
case R:
g.setColor(Color.black);
g.drawLine(x+w/2, y+h/2, x+w+10, y+h/2);
break;
case RU:
g.setColor(Color.black);
g.drawLine(x+w/2, y+h/2, x+w, y);
break;
}
}
}
package tf1703;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import tf1703.Tank.Direction;
public class TFrame extends Frame{
public int frameX=100;
public int frameY=100;
public int frameH=800;
public int frameW=1200;
public Tank myTank=null;
public TFrame() {
this.setBounds(frameX,frameY,frameW,frameH);
this.setBackground(Color.pink);
this.myTank=new Tank(200,200,100,100,Tank.Direction.RU);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(-1);
}
});
this.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_A) {
System.out.println("你按了A");
myTank.dir=Direction.L;
myTank.x=myTank.x-10;
}else if(e.getKeyCode()==KeyEvent.VK_S) {
System.out.println("你按了S");
myTank.dir=Direction.D;
myTank.y=myTank.y+10;
}
else if(e.getKeyCode()==KeyEvent.VK_D) {
System.out.println("你按了D");
myTank.dir=Direction.R;
myTank.x=myTank.x+10;
}
else if(e.getKeyCode()==KeyEvent.VK_W) {
System.out.println("你按了W");
myTank.dir=Direction.U;
myTank.y=myTank.y-10;
}
repaint();
}
});
this.setVisible(true);
}
public void paint(Graphics g) {
this.myTank.draw(g);
}
public static void main(String[] args) {
new TFrame();
}
}
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The import tf1703.Tank.Direction cannot be resolved
Cannot make a static reference to the non-static field Tank.Direction
Direction cannot be resolved to a type
Direction cannot be resolved to a variable
Direction cannot be resolved to a type
Direction cannot be resolved to a variable
Direction cannot be resolved to a type
Direction cannot be resolved to a variable
Direction cannot be resolved to a type
Direction cannot be resolved to a variable
at tf1703.TFrame.<init>(TFrame.java:9)
at tf1703.TFrame.main(TFrame.java:86)