您的位置:首页 > 编程语言 > Java开发

java基础 绘图技术.坦克大战 之java绘图坐标体系(二)

2016-10-19 09:00 477 查看
    功能:在坐标系上绘制坦克

1 /*
2  * 功能:坦克游戏的1.0
3  * 1. 画出坦克
4  * */
5 package com.tank;
6
7 import javax.swing.*;
8
9 import java.awt.*;
10 public class demo2 extends JFrame{
11
12     /**
13      * @param args
14      */
15     Mypanel mp =null;
16     public static void main(String[] args) {
17         // TODO Auto-generated method stub
18         demo2 demo2= new demo2();
19     }
20
21     //构造函数
22     public demo2()
23     {
24         mp =new Mypanel();
25
26         this.add(mp);
27         this.setSize(400,300);
28         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
29         this.setVisible(true);
30     }
31
32 }
33
34 //我的面板
35 class Mypanel extends JPanel
36 {
37     //定义一个我的坦克
38     Hero hero =null;
39
40     //构造函数
41     public Mypanel()
42     {
43         hero = new Hero(10,10);
44     }
45
46     //重写paint函数
47     public void paint(Graphics g)
48     {
49         super.paint(g);
50         g.fillRect(0,0,400, 300);
51         this.drawTank(hero.getX(), hero.getY(), g, 0, 1);
52     }
53     //画出坦克的函数
54     public void drawTank(int x,int y,Graphics g,int direct,int type)
55     {
56         //判断是什么类型的坦克(敌人的还是自己的)
57         switch(type)
58         {
59         case 0:
60             g.setColor(Color.cyan);
61             break;
62         case 1:
63             g.setColor(Color.yellow);
64             break;
65         }
66
67         //判断方向
68         switch(direct)
69         {
70             //向上
71             case 0:
72
73                 //画出我的坦克(到时再封装成一个函数 )
74                 //1. 画出左边的矩形
75                 g.fill3DRect(x, y, 5,30,false);
76                 //2. 画出右边矩形
77                 g.fill3DRect(x+15, y, 5, 30,false);
78                 //3. 画出中间矩形
79                 g.fill3DRect(x+5, y+5, 10, 20,false);
80                 //4. 画出圆形
81                 g.fillOval(x+5, y+10, 10, 10);
82                 //5. 画出线
83                 g.drawLine(x+10, y+15, x+10, y);
84                 break;
85         }
86     }
87 }
88
89 //坦克类
90 class Tank
91 {
92     //表示坦克的横坐标
93     int x=0;
94
95     //坦克的从纵坐标
96     int y=0;
97     public int getX() {
98         return x;
99     }
100     public void setX(int x) {
101         this.x = x;
102     }
103     public int getY() {
104         return y;
105     }
106     public void setY(int y) {
107         this.y = y;
108     }
109     public Tank(int x,int y)
110     {
111         this.x=x;
112         this.y=y;
113     }
114 }
115
116 //我的坦克
117 class Hero  extends Tank
118 {
119     public Hero(int x,int y)
120     {
121         super(x, y);
122     }
123 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: