[java]代码库
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package tank;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
*
* @author Dell-
*/
public class Tank1 extends JFrame {
MyPanel mp = null;
// GuanQia gqmb =null;
public static void main(String[] args) {
Tank1 t = new Tank1();
}
public Tank1() {
mp = new MyPanel();
Thread th = new Thread(mp);
th.start();
// gqmb = new GuanQia();
// this.add(gqmb);
this.add(mp);
this.addKeyListener(mp);
this.setSize(600,500);
this.setLocation(300, 280);
this.setDefaultCloseOperation(3);
this.setVisible(true);
}
}
class MyPanel extends JPanel implements KeyListener, Runnable {
MyTank mt = null;
Vector<DiTank> dtk = new Vector<>();
Vector<BaoZha> baozha = new Vector<>();
int tks = 5;
ImageIcon tp1 = new ImageIcon("e:\\b1.gif");
ImageIcon tp2 = new ImageIcon("e:\\b2.gif");
ImageIcon tp3 = new ImageIcon("e:\\b3.gif");
public MyPanel() {
//给定坦克左上角的坐标,也就是坦克出现的地方!
mt = new MyTank(250,300);
for (int i = 0; i < tks; i++) {
DiTank dt = new DiTank((i) * 100 + 5, 0);
Thread t = new Thread(dt);
t.start();
zidan zd = new zidan(dt.x + 10, dt.y + 30, 2);
dt.dzd.add(zd);
Thread t2 = new Thread(zd);
t2.start();
dtk.add(dt);
}
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.fillRect(0, 0, 500, 400);
if (this.mt.shengming) {
this.drawTank(mt.getX(), mt.getY(), g, mt.fangxiang, 0);
}
for (int i = 0; i < dtk.size(); i++) {
if (dtk.get(i).shengming) {
this.drawTank(dtk.get(i).getX(), dtk.get(i).getY(), g, dtk.get(i).fangxiang, 1);
}
}
for (int i = 0; i < mt.aa.size(); i++) {
zidan zd = mt.aa.get(i);
if (zd != null && zd.shengming == true) {
g.setColor(Color.red);
g.fill3DRect(zd.x, zd.y, 3, 3, false);
}
if (zd.shengming == false) {
mt.aa.remove(zd);
}
}
for (int i = 0; i < dtk.size(); i++) {
DiTank dt = dtk.get(i);
for (int j = 0; j < dt.dzd.size(); j++) {
zidan zd1 = dt.dzd.get(j);
if (zd1 != null && zd1.shengming == true) {
g.setColor(Color.white);
g.fill3DRect(zd1.x, zd1.y, 3, 3, false);
}
if (zd1.shengming == false) {
mt.aa.remove(zd1);
}
}
}
for (int i = 0; i < baozha.size(); i++) {
System.out.println("!!!");
BaoZha bz = baozha.get(i);
if (bz.shengcunqi > 6) {
g.drawImage(tp1.getImage(), bz.x, bz.y, this);
} else if (bz.shengcunqi > 3) {
g.drawImage(tp2.getImage(), bz.x, bz.y, this);
} else if (bz.shengcunqi > 0) {
g.drawImage(tp3.getImage(), bz.x, bz.y, this);
}
bz.suqsd();
if (bz.shengcunqi == 0) {
baozha.remove(bz);
}
}
}
public void jw(){
for(int i = 0 ;i<this.dtk.size();i++ ){
DiTank dt = dtk.get(i);
for(int j =0;j<dt.dzd.size();j++){
zidan zd = dt.dzd.get(j);
this.jzdf(zd, mt);
}
}
}
public void jd(){
for(int i = 0 ;i<mt.aa.size();i++){
zidan zd = mt.aa.get(i);
if(zd.shengming){
for(int j=0 ; j<dtk.size();j++){
DiTank dt = dtk.get(j);
if(dt.shengming){
this.jzdf(zd, dt);
}
}
}
}
}
public void jzdf(zidan zd, Tank dt) {
switch (dt.fangxiang) {
case 0:
case 2:
if (zd.x > dt.x && zd.x < dt.x + 20 && zd.y > dt.y && zd.y < dt.y + 30) {
zd.shengming = false;
dt.shengming = false;
BaoZha b = new BaoZha(dt.x, dt.y);
baozha.add(b);
}
break;
case 1:
case 3:
if (zd.x > dt.x && zd.x < dt.x + 30 && zd.y > dt.y && zd.y < dt.y + 20) {
zd.shengming = false;
dt.shengming = false;
BaoZha b = new BaoZha(dt.x, dt.y);
baozha.add(b);
}
}
}
public void drawTank(int x, int y, Graphics g, int fangxiang, int leixing) {
switch (leixing) {
case 0:
g.setColor(Color.ORANGE);
break;
case 1:
g.setColor(Color.red);
break;
}
switch (fangxiang) {
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 - 5);
break;
case 1:
g.fill3DRect(x, y, 30, 5, false);
g.fill3DRect(x, y + 15, 30, 5, false);
g.fill3DRect(x + 5, y + 5, 20, 10, true);
g.fillOval(x + 10, y + 5, 10, 10);
g.drawLine(x + 15, y + 10, x - 6, y + 10);
break;
case 2:
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 + 33);
break;
case 3:
g.fill3DRect(x, y, 30, 5, false);
g.fill3DRect(x, y + 15, 30, 5, false);
g.fill3DRect(x + 5, y + 5, 20, 10, true);
g.fillOval(x + 10, y + 5, 10, 10);
g.drawLine(x + 15, y + 10, x + 33, y + 10);
break;
}
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_W) {
this.mt.setFangxiang(0);
this.mt.xiangshang();
} else if (e.getKeyCode() == KeyEvent.VK_A) {
this.mt.setFangxiang(1);
this.mt.xiangzuo();
} else if (e.getKeyCode() == KeyEvent.VK_S) {
this.mt.setFangxiang(2);
this.mt.xiangxia();
} else if (e.getKeyCode() == KeyEvent.VK_D) {
this.mt.setFangxiang(3);
this.mt.xiangyou();
}
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
if (this.mt.aa.size() < 8) {
this.mt.fszd();
}
}
this.repaint();
}
@Override
public void run() {
while (true) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
System.out.println("ERROR:" + e);
}
jd();
jw();
this.repaint();
}
}
}
初级程序员
by: 145664 发表于:2017-12-17 15:59:08 顶(0) | 踩(0) 回复
on
回复评论