感慨:
偶的坦克終于可以移動,可以發射子彈了~~
不過從昨天軟件開發與實踐的老師的角度去說~我這樣做是不對的~開發一個程序之前需要做好構架,而我則是想到哪做哪..做錯了再改!哈哈!..還有待學習..
代碼:
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僅用于個人學習交流!
目的在于記錄個人成長.
所有文字均屬于個人理解.
如有錯誤,望多多指教!不勝感激!