package com.zdyn.test;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Iterator;
public class MyForm extends Frame {
static int k2 = 30;
static int k3 = 30;
/*
* 定义一个蛇身体
*/
static ArrayList list = new ArrayList();
public void lauchFrame() {
this.setLocation(100, 100);
this.setSize(800, 600);
this.setTitle("TankWar");
this.addKeyListener(new FrmKey_this_keyAdapter(this));
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
// 增加匿名类用来响应窗体关闭事件,使用Window适配器的好处是,我可以在里面只实现我所感兴趣的方法.
// 而如果要使用WindowListener接口的话,必须实现里面所有的方法.
this.setBackground(Color.GREEN);
this.setResizable(false);// 禁止对窗体进入缩放
setVisible(true);
}
// 窗体里如果有Graphics方法的话,会自动调用,没有的话,就不调用.
public MyForm() throws HeadlessException {
super();
Location t1 = new Location();
t1.x = 50;
t1.y = 50;
Location t2 = new Location();
t2.x = 100;
t2.y = 50;
Location t3 = new Location();
t3.x = 150;
t3.y = 50;
Location t4 = new Location();
t4.x = 200;
t4.y = 50;
list.add(t1);
list.add(t2);
list.add(t3);
list.add(t4);
}
public void keyPressed(KeyEvent e) // 按下键盘方向键
{
if (e.getKeyCode() == KeyEvent.VK_RIGHT)// 右键
{
System.out.println("hhh");
}
}
public void paint(Graphics g) {
// Thread.sleep(100);
// System.out.println("ll");
Color c = g.getColor();// 保存原来的颜色
g.setColor(Color.RED);// 设置红色
// g.fillOval(MyForm.k, MyForm.k1, k2, k3);// 画个实心圆,代表的坦克
// g.fillOval(MyForm.k+100, MyForm.k1, k2, k3);// 画个实心圆,代表的坦克
Iterator it = list.iterator();
while (it.hasNext()) {
Location t = (Location) (it.next());
g.fillOval(t.x, t.y, k2, k3);
// System.out.println(t.x);
}
g.setColor(c);// 恢复原来的颜色
}
public void this_keyPressed(KeyEvent e) throws InterruptedException {
// 判断是否右向箭头键按下
if (e.getKeyCode() == 39) {
System.out.println("右");
Iterator it = list.iterator();
// Location t=(Location)it.next();
// t.x=t.x+50;
// list.remove(t);
// list.add(t);
// this.repaint();
int count = list.size();
System.out.println(count);
Location[] arr = new Location[count];
int st = 0;
while (it.hasNext()) {
arr[st] = (Location) it.next();
st++;
}
Location brr[] = new Location[arr.length];
for (int j = 0; j < arr.length; j++) {
if (j < arr.length-1) {
brr[j] = arr[j + 1];
} else {
// arr[j].x = arr[j].x + 50;
// brr[j] = arr[j];
brr[j]=new Location();
brr[j].y=brr[j-1].y;
brr[j].x=brr[j-1].x+50;
}
}
list = new ArrayList();
for (int i = 0; i < brr.length; i++) {
System.out.println(brr[i].x);
list.add(brr[i]);
}
this.repaint();
}
if (e.getKeyCode() == 38) {
Iterator it = list.iterator();
// Location t=(Location)it.next();
// t.x=t.x+50;
// list.remove(t);
// list.add(t);
// this.repaint();
int count = list.size();
System.out.println(count);
Location[] arr = new Location[count];
int st = 0;
while (it.hasNext()) {
arr[st] = (Location) it.next();
st++;
}
Location brr[] = new Location[arr.length];
for (int j = 0; j < arr.length; j++) {
if (j < arr.length-1) {
brr[j] = arr[j + 1];
} else {
// arr[j].x = arr[j].x + 50;
// brr[j] = arr[j];
brr[j]=new Location();
brr[j].x=brr[j-1].x;
brr[j].y=brr[j-1].y-50;
}
}
list = new ArrayList();
for (int i = 0; i < brr.length; i++) {
System.out.println(brr[i].x);
list.add(brr[i]);
}
this.repaint();
}
if (e.getKeyCode() == 37) {
Iterator it = list.iterator();
// Location t=(Location)it.next();
// t.x=t.x+50;
// list.remove(t);
// list.add(t);
// this.repaint();
int count = list.size();
System.out.println(count);
Location[] arr = new Location[count];
int st = 0;
while (it.hasNext()) {
arr[st] = (Location) it.next();
st++;
}
Location brr[] = new Location[arr.length];
for (int j = 0; j < arr.length; j++) {
if (j < arr.length-1) {
brr[j] = arr[j + 1];
} else {
// arr[j].x = arr[j].x + 50;
// brr[j] = arr[j];
brr[j]=new Location();
brr[j].y=brr[j-1].y;
brr[j].x=brr[j-1].x-50;
}
}
list = new ArrayList();
for (int i = 0; i < brr.length; i++) {
System.out.println(brr[i].x);
list.add(brr[i]);
}
this.repaint();
}
if(e.getKeyCode() == 40) {
System.out.println("下");
Iterator it = list.iterator();
// Location t=(Location)it.next();
// t.x=t.x+50;
// list.remove(t);
// list.add(t);
// this.repaint();
int count = list.size();
System.out.println(count);
Location[] arr = new Location[count];
int st = 0;
while (it.hasNext()) {
arr[st] = (Location) it.next();
st++;
}
Location brr[] = new Location[arr.length];
for (int j = 0; j < arr.length; j++) {
if (j < arr.length-1) {
brr[j] = arr[j + 1];
} else {
// arr[j].x = arr[j].x + 50;
// brr[j] = arr[j];
brr[j]=new Location();
brr[j].x=brr[j-1].x;
brr[j].y=brr[j-1].y+50;
}
}
list = new ArrayList();
for (int i = 0; i < brr.length; i++) {
System.out.println(brr[i].x);
list.add(brr[i]);
}
//k1 = k1 + 50;
this.repaint(); }
}
/*
* 蛇身跟随蛇头走,蛇头跟随指令走
*/
public static void main(String[] args) {
MyForm tc = new MyForm();
tc.lauchFrame();
}
}
class FrmKey_this_keyAdapter extends KeyAdapter {
private MyForm adaptee;
FrmKey_this_keyAdapter(MyForm adaptee) {
this.adaptee = adaptee;
}
// 重写父类中的方法
public void keyPressed(KeyEvent e) {
try {
adaptee.this_keyPressed(e);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
class Location {
int x;
int y;
}
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Iterator;
public class MyForm extends Frame {
static int k2 = 30;
static int k3 = 30;
/*
* 定义一个蛇身体
*/
static ArrayList list = new ArrayList();
public void lauchFrame() {
this.setLocation(100, 100);
this.setSize(800, 600);
this.setTitle("TankWar");
this.addKeyListener(new FrmKey_this_keyAdapter(this));
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
// 增加匿名类用来响应窗体关闭事件,使用Window适配器的好处是,我可以在里面只实现我所感兴趣的方法.
// 而如果要使用WindowListener接口的话,必须实现里面所有的方法.
this.setBackground(Color.GREEN);
this.setResizable(false);// 禁止对窗体进入缩放
setVisible(true);
}
// 窗体里如果有Graphics方法的话,会自动调用,没有的话,就不调用.
public MyForm() throws HeadlessException {
super();
Location t1 = new Location();
t1.x = 50;
t1.y = 50;
Location t2 = new Location();
t2.x = 100;
t2.y = 50;
Location t3 = new Location();
t3.x = 150;
t3.y = 50;
Location t4 = new Location();
t4.x = 200;
t4.y = 50;
list.add(t1);
list.add(t2);
list.add(t3);
list.add(t4);
}
public void keyPressed(KeyEvent e) // 按下键盘方向键
{
if (e.getKeyCode() == KeyEvent.VK_RIGHT)// 右键
{
System.out.println("hhh");
}
}
public void paint(Graphics g) {
// Thread.sleep(100);
// System.out.println("ll");
Color c = g.getColor();// 保存原来的颜色
g.setColor(Color.RED);// 设置红色
// g.fillOval(MyForm.k, MyForm.k1, k2, k3);// 画个实心圆,代表的坦克
// g.fillOval(MyForm.k+100, MyForm.k1, k2, k3);// 画个实心圆,代表的坦克
Iterator it = list.iterator();
while (it.hasNext()) {
Location t = (Location) (it.next());
g.fillOval(t.x, t.y, k2, k3);
// System.out.println(t.x);
}
g.setColor(c);// 恢复原来的颜色
}
public void this_keyPressed(KeyEvent e) throws InterruptedException {
// 判断是否右向箭头键按下
if (e.getKeyCode() == 39) {
System.out.println("右");
Iterator it = list.iterator();
// Location t=(Location)it.next();
// t.x=t.x+50;
// list.remove(t);
// list.add(t);
// this.repaint();
int count = list.size();
System.out.println(count);
Location[] arr = new Location[count];
int st = 0;
while (it.hasNext()) {
arr[st] = (Location) it.next();
st++;
}
Location brr[] = new Location[arr.length];
for (int j = 0; j < arr.length; j++) {
if (j < arr.length-1) {
brr[j] = arr[j + 1];
} else {
// arr[j].x = arr[j].x + 50;
// brr[j] = arr[j];
brr[j]=new Location();
brr[j].y=brr[j-1].y;
brr[j].x=brr[j-1].x+50;
}
}
list = new ArrayList();
for (int i = 0; i < brr.length; i++) {
System.out.println(brr[i].x);
list.add(brr[i]);
}
this.repaint();
}
if (e.getKeyCode() == 38) {
Iterator it = list.iterator();
// Location t=(Location)it.next();
// t.x=t.x+50;
// list.remove(t);
// list.add(t);
// this.repaint();
int count = list.size();
System.out.println(count);
Location[] arr = new Location[count];
int st = 0;
while (it.hasNext()) {
arr[st] = (Location) it.next();
st++;
}
Location brr[] = new Location[arr.length];
for (int j = 0; j < arr.length; j++) {
if (j < arr.length-1) {
brr[j] = arr[j + 1];
} else {
// arr[j].x = arr[j].x + 50;
// brr[j] = arr[j];
brr[j]=new Location();
brr[j].x=brr[j-1].x;
brr[j].y=brr[j-1].y-50;
}
}
list = new ArrayList();
for (int i = 0; i < brr.length; i++) {
System.out.println(brr[i].x);
list.add(brr[i]);
}
this.repaint();
}
if (e.getKeyCode() == 37) {
Iterator it = list.iterator();
// Location t=(Location)it.next();
// t.x=t.x+50;
// list.remove(t);
// list.add(t);
// this.repaint();
int count = list.size();
System.out.println(count);
Location[] arr = new Location[count];
int st = 0;
while (it.hasNext()) {
arr[st] = (Location) it.next();
st++;
}
Location brr[] = new Location[arr.length];
for (int j = 0; j < arr.length; j++) {
if (j < arr.length-1) {
brr[j] = arr[j + 1];
} else {
// arr[j].x = arr[j].x + 50;
// brr[j] = arr[j];
brr[j]=new Location();
brr[j].y=brr[j-1].y;
brr[j].x=brr[j-1].x-50;
}
}
list = new ArrayList();
for (int i = 0; i < brr.length; i++) {
System.out.println(brr[i].x);
list.add(brr[i]);
}
this.repaint();
}
if(e.getKeyCode() == 40) {
System.out.println("下");
Iterator it = list.iterator();
// Location t=(Location)it.next();
// t.x=t.x+50;
// list.remove(t);
// list.add(t);
// this.repaint();
int count = list.size();
System.out.println(count);
Location[] arr = new Location[count];
int st = 0;
while (it.hasNext()) {
arr[st] = (Location) it.next();
st++;
}
Location brr[] = new Location[arr.length];
for (int j = 0; j < arr.length; j++) {
if (j < arr.length-1) {
brr[j] = arr[j + 1];
} else {
// arr[j].x = arr[j].x + 50;
// brr[j] = arr[j];
brr[j]=new Location();
brr[j].x=brr[j-1].x;
brr[j].y=brr[j-1].y+50;
}
}
list = new ArrayList();
for (int i = 0; i < brr.length; i++) {
System.out.println(brr[i].x);
list.add(brr[i]);
}
//k1 = k1 + 50;
this.repaint(); }
}
/*
* 蛇身跟随蛇头走,蛇头跟随指令走
*/
public static void main(String[] args) {
MyForm tc = new MyForm();
tc.lauchFrame();
}
}
class FrmKey_this_keyAdapter extends KeyAdapter {
private MyForm adaptee;
FrmKey_this_keyAdapter(MyForm adaptee) {
this.adaptee = adaptee;
}
// 重写父类中的方法
public void keyPressed(KeyEvent e) {
try {
adaptee.this_keyPressed(e);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
class Location {
int x;
int y;
}