坦克大战1(坦克的绘图实现)
2014-03-03 00:52
357 查看
坦克绘图的实现比较简单,可以分解为三个矩形、一个圆形以及一条直线,如图所示:
在本代码中,主要是创建了TANK类、MyTank类、MyPanel类,其作用如下:
Tank类主要实现坦克的定义,其成员变量为其的横轴和纵轴的位置;
MyTank类主要定义我军的坦克,继承Tank;MyPanel类主要实现坦克的绘图实现;
最后将其加入到JFrame中,设计窗口的属性,让其显示,创建对象即可。
程序代码如下:
/**
* 功能:绘画一个坦克
*/
package com.text3_2;
import java.awt.*;
import javax.swing.*;
public class MyTankGame_1 extends JFrame{
MyPanel mypanel=null;
public static void main(String[] args) {
MyTankGame_1 mytankgame_1=new MyTankGame_1();
}
public MyTankGame_1()
{
mypanel=new MyPanel();
this.add(mypanel);
this.setSize(400,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
class Tank
{
private int x;
private int y;
public Tank(int x,int y)
{
this.x=x;
this.y=y;
}
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;
}
}
class MyTank extends Tank
{
public MyTank(int x,int y){
super(x,y);
}
}
class MyPanel extends JPanel
{
MyTank hero=null;
public void paint(Graphics g)
{
super.paint(g);
g.fillRect(0, 0, 400,300);
this.drawTank(hero.getX(), hero.getY(),g,0 ,0);
}
public MyPanel()
{
hero=new MyTank(10,10);
}
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.BLUE);
break;
}
switch(type)
{
//向上
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+2);
break;
}
}
}
程序运行效果如下;
在本代码中,主要是创建了TANK类、MyTank类、MyPanel类,其作用如下:
Tank类主要实现坦克的定义,其成员变量为其的横轴和纵轴的位置;
MyTank类主要定义我军的坦克,继承Tank;MyPanel类主要实现坦克的绘图实现;
最后将其加入到JFrame中,设计窗口的属性,让其显示,创建对象即可。
程序代码如下:
/**
* 功能:绘画一个坦克
*/
package com.text3_2;
import java.awt.*;
import javax.swing.*;
public class MyTankGame_1 extends JFrame{
MyPanel mypanel=null;
public static void main(String[] args) {
MyTankGame_1 mytankgame_1=new MyTankGame_1();
}
public MyTankGame_1()
{
mypanel=new MyPanel();
this.add(mypanel);
this.setSize(400,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
class Tank
{
private int x;
private int y;
public Tank(int x,int y)
{
this.x=x;
this.y=y;
}
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;
}
}
class MyTank extends Tank
{
public MyTank(int x,int y){
super(x,y);
}
}
class MyPanel extends JPanel
{
MyTank hero=null;
public void paint(Graphics g)
{
super.paint(g);
g.fillRect(0, 0, 400,300);
this.drawTank(hero.getX(), hero.getY(),g,0 ,0);
}
public MyPanel()
{
hero=new MyTank(10,10);
}
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.BLUE);
break;
}
switch(type)
{
//向上
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+2);
break;
}
}
}
程序运行效果如下;
相关文章推荐
- 坦克大战 js实现
- java实现坦克大战1.0版 在面板上画出坦克
- java 实现坦克大战2.1版
- java 实现坦克大战 3.0版
- 用java做坦克大战--主要类和基本功能实现
- JAVA实现坦克大战1.0版本
- SDI文档模式下调用对话框实现在对话框上绘图操作,并且进行文档和对话框之间的数据传输
- 马士兵的坦克大战,以前做过,今天又找出来看了看
- 坦克大战之声音处理类(四)
- C# 实现完整功能的截图控件(1)-实现绘图工具栏控件 控件实现了截图、绘制矩形、圆形、箭头、线条、文字,还需要可以撤销绘制步骤、保存图形等功能,基本实现了跟QQ2008截图一样的功能,还可以更
- 谷歌地图控件,通过扩展实现GMAP的自定义绘图系统
- 坦克大战_坦克绘制
- 例子:web版坦克大战1.0
- 创建掩码位图来实现透明绘图
- 在Panel上绘图的实现
- NYOJ 284--坦克大战【BFS && 优先队列】
- Android通过onDraw实现在View中绘图操作
- reactjs+echarts实现绘图
- 玩转你画我猜(一):程序实现自动绘图
- VC的内存绘图防止屏幕闪烁技术的实现