-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTankOfGame.java
More file actions
128 lines (95 loc) · 1.76 KB
/
TankOfGame.java
File metadata and controls
128 lines (95 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* 功能:
* 1、画出一个坦克
*/
package com.bj;
import javax.swing.*;
import java.awt.*;
public class TankOfGame extends JFrame {
//定义变量
MyPaneles mp=null;
public static void main(String[] args) {
// TODO Auto-generated method stub
TankOfGame tank=new TankOfGame();
}
public TankOfGame(){
mp=new MyPaneles();
this.add(mp);
this.setSize(400,400);
this.setLocation(200,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
//我的面板
class MyPaneles extends JPanel{
//定义一个我的坦克
Hero hero=null;
public MyPaneles(){
hero=new Hero(10,10);
// System.out.println("Tank");
}
//重写paint
public void paint(Graphics g){
super.paint(g);
drawTank(hero.getX(), hero.getY(), g, 0, 1);
}
public void drawTank(int x,int y,Graphics g,int direction,int type){
//
switch(type){
case 0:
g.setColor(Color.cyan);
break;
case 1:
g.setColor(Color.yellow);
break;
}
//判断方向
switch(direction){
//up
case 0:
//g.setColor(Color.CYAN);
//画出我的坦克,(到时候再封装成一个函数)
//1、画出左边矩形
g.fill3DRect(x, y, 5, 30,false);
//2、画出右面矩形
g.fill3DRect(x+15, y, 5, 30,false);
//3、中间矩形
g.fill3DRect(x+5, y+5, 10, 20,false);
//4、画出圆形
g.fillOval(x+5, y+10, 10, 10);
//5、画线条
g.drawLine(x+10, y+5, x+10, y-2);
break;
}
}
}
//坦克类
class Tank{
//坦克的横坐标
int x=0;
//坦克纵坐标
int y=0;
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 Tank(int x,int y){
this.x=x;
this.y=y;
}
}
//我的坦克
class Hero extends Tank{
public Hero(int x,int y){
super(x, y);
}
}