感慨:
偶的坦克终于可以移动,可以发射子弹了~~
不过从昨天软件开发与实践的老师的角度去说~我这样做是不对的~开发一个程序之前需要做好构架,而我则是想到哪做哪..做错了再改!哈哈!..还有待学习..
代码:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class YouXi extends JFrame
{
Container c;
Draw_table d_table; //中间的画板
Mypanel panel_east; //右边的面板
private static Icon[] faces;
YouXi()
{
super("傻瓜坦克 版权所有:久城");
c = getContentPane();
c.setLayout(new BorderLayout());
panel_east = new Mypanel();
panel_east.setLayout(new GridLayout(6,1,20,20));
faces= new Icon[]{new ImageIcon("d:\11.jpg","")};
JButton button1 = new JButton("重新开始");
JButton button2 = new JButton(new String ("傻瓜坦克"),new ImageIcon(getClass().getResource("11.jpg")));
JTextField text1 = new JTextField("realsmy");
JTextField text2 = new JTextField("0");
JLabel lb1 = new JLabel("玩 家");
JLabel lb2 = new JLabel("杀人数");
text1.setEditable(false);
text2.setEditable(false);
panel_east.add(button2);
panel_east.add(lb1);
panel_east.add(text1);
panel_east.add(lb2);
panel_east.add(text2);
panel_east.add(button1);
//添加键盘监听,不知道加到哪好,随表找个按钮加上去了哈哈!
button2.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_ENTER)
{
if(d_table.fangxiang == 1)
{
d_table.zx = d_table.x+50;
d_table.zy = d_table.y+10;
d_table.key_fangxiang = 1;
}
if(d_table.fangxiang == 2)
{
d_table.zx = d_table.x+10;
d_table.zy = d_table.y+50;
d_table.key_fangxiang = 2;
}
if(d_table.fangxiang == 3)
{
d_table.zx = d_table.x-10;
d_table.zy = d_table.y+10;
d_table.key_fangxiang = 3;
}
if(d_table.fangxiang == 4)
{
d_table.zx = d_table.x+10;
d_table.zy = d_table.y-10;
d_table.key_fangxiang = 4;
}
d_table.key = true;
}
if(e.getKeyCode()==KeyEvent.VK_UP){
d_table.y-=10;
d_table.fangxiang = 4;
}
if(e.getKeyCode()==KeyEvent.VK_DOWN){
d_table.y += 10;
d_table.fangxiang = 2;
}
if(e.getKeyCode()==KeyEvent.VK_LEFT){
d_table.x -= 10;
d_table.fangxiang = 3;
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT){
d_table.x +=10;
d_table.fangxiang = 1;
}
}
}
);
d_table = new Draw_table();
d_table.setBackground(Color.red);
c.add(panel_east,BorderLayout.EAST);
c.add(d_table,BorderLayout.CENTER);
setSize(600,600);
setLocation(200,100);
setVisible(true);
}
public static void main(String args[])
{
YouXi yx = new YouXi();
yx.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
//定义画板类
class Draw_table extends Canvas// implements runnable
{
static boolean key = false;
static int x=20,y=20; //坦克坐标
static int zx,zy; //子弹起点坐标
static int fangxiang = 1; //定义车头方向
static int key_fangxiang = 1; //定义子弹方向
int k = 1;
public void paint(Graphics g)
{
if(fangxiang == 1)
{
g.setColor(Color.black);
g.fillRect(x,y,50,30); //车身
g.fillRect(x+50,y+10,10,10);//车头=子弹
g.setColor(Color.blue);
g.fillOval(x+15,y+5,20,20);
}
if(fangxiang == 2)
{
g.setColor(Color.black);
g.fillRect(x,y,30,50); //车身
g.fillRect(x+10,y+50,10,10);//车头=子弹
g.setColor(Color.blue);
g.fillOval(x+5,y+15,20,20);
}
if(fangxiang == 3)
{
g.setColor(Color.black);
g.fillRect(x,y,50,30); //车身
g.fillRect(x-10,y+10,10,10);//车头=子弹
g.setColor(Color.blue);
g.fillOval(x+15,y+5,20,20);
}
if(fangxiang == 4)
{
g.setColor(Color.black);
g.fillRect(x,y,30,50); //车身
g.fillRect(x+10,y-10,10,10);//车头=子弹
g.setColor(Color.blue);
g.fillOval(x+5,y+15,20,20);
}
if(k==1)
{
try{Thread.sleep(200);}catch(Exception e){}
}
if(key == true)
{
//new Thread(new draw_table()).start();
//key = false;
//new Zidan_thread().start();
k = 0;
zidan();
}
repaint();
}
public void zidan()
{
Graphics g = getGraphics();
g.setColor(Color.yellow);
g.fillRect(zx, zy, 10, 10);
if(key_fangxiang == 1)
{
zx+=10;
}
if(key_fangxiang == 2)
{
zy+=10;
}
if(key_fangxiang == 3)
{
zx-=10;
}
if(key_fangxiang == 4)
{
zy-=10;
}
try{Thread.sleep(200);}catch(Exception e){}
}
//public void run()
//{
// zidan();
//}
}
//面板类定义
class Mypanel extends JPanel
{
public Insets insets()
{
return new Insets(60,10,200,10);
}
}
欢迎来访!^.^!
本BLOG仅用于个人学习交流!
目的在于记录个人成长.
所有文字均属于个人理解.
如有错误,望多多指教!不胜感激!